How to handle the lack of operator overload ?

Discussion in 'Java' started by Mr Smith, Feb 9, 2005.

  1. Mr Smith

    Mr Smith Guest

    Hello,

    In a table, i have various object. Right now, i'm writing the Double object code. But in Java (AFAIK) you can't overload operator, so, what is the best on a dummy operation: [i, j] += [i, j + 1]

    1/
    set(i, j, new Double(((Double)get(i, j)).doubleValue() + ((Double)get(i, j + 1)).doubleValue()))

    Simply awful to read, it's just an addition here...


    2/
    setdouble(i, j, getdouble(i, j) + getdouble(i, j + 1))

    Could be ok, but it needs to put the casts in a lot of getters/setters, heavy work.


    3/
    set(i, j, Op.addDouble(get(i, j), get(i, j + 1)))
    with
    static addDouble(Object a, Object b) {
    double c = ((Double)a).doubleValue();
    double d = ((Double)b).doubleValue();
    return new Double(c + d);
    }


    What is you rpersonnal choice? What are the pros and cons?

    Thanx for your advices
     
    Mr Smith, Feb 9, 2005
    #1
    1. Advertising

  2. Mr Smith wrote:
    > Hello,
    >
    > In a table, i have various object. Right now, i'm writing the Double object code. But in Java (AFAIK) you can't overload operator, so, what is the best on a dummy operation: [i, j] += [i, j + 1]
    >
    > 1/
    > set(i, j, new Double(((Double)get(i, j)).doubleValue() + ((Double)get(i, j + 1)).doubleValue()))
    >
    > Simply awful to read, it's just an addition here...


    agreed

    >
    >
    > 2/
    > setdouble(i, j, getdouble(i, j) + getdouble(i, j + 1))
    >
    > Could be ok, but it needs to put the casts in a lot of getters/setters, heavy work.
    >
    >
    > 3/
    > set(i, j, Op.addDouble(get(i, j), get(i, j + 1)))
    > with
    > static addDouble(Object a, Object b) {
    > double c = ((Double)a).doubleValue();
    > double d = ((Double)b).doubleValue();
    > return new Double(c + d);
    > }
    >
    >
    > What is you rpersonnal choice? What are the pros and cons?


    I would go for number 2, because it makes clear that it's a table of
    doubles. I think most of the times the getters and setters where you
    have to cast are not that many. If it's not the case maybe you can
    implement a couple of extra setters and getters in your table to do the
    conversion for you; that would still keep the type safety.
    I don't really like 3, it allows clients of your class to pass whatever
    they want without even thinking about it.
     
    Andrea Desole, Feb 9, 2005
    #2
    1. Advertising

  3. Mr Smith

    Mr Smith Guest

    On Wed, 09 Feb 2005 14:59:39 +0100
    Andrea Desole <_SPAM_PLEASE.nl> wrote:

    >> [...]

    > I would go for number 2, because it makes clear that it's a table of
    > doubles. I think most of the times the getters and setters where you


    it's not a table of Double, it's a table with some column of Double :)

    > have to cast are not that many. If it's not the case maybe you can
    > implement a couple of extra setters and getters in your table to do the
    > conversion for you; that would still keep the type safety.
    > I don't really like 3, it allows clients of your class to pass whatever
    > they want without even thinking about it.


    thx for your comments :)
     
    Mr Smith, Feb 9, 2005
    #3
  4. Mr Smith wrote:
    >
    > it's not a table of Double, it's a table with some column of Double :)


    you have a table with mixed objects? Mmmm, that sounds dangerous. I
    would try to restrict it somehow.
     
    Andrea Desole, Feb 9, 2005
    #4
  5. Mr Smith

    Mr Smith Guest

    On Wed, 09 Feb 2005 16:32:38 +0100
    Andrea Desole <_SPAM_PLEASE.nl> wrote:

    > Mr Smith wrote:
    > >
    > > it's not a table of Double, it's a table with some column of Double :)

    >
    > you have a table with mixed objects? Mmmm, that sounds dangerous. I
    > would try to restrict it somehow.


    well it's not my choice ;) my boss want a database/spreadsheet with mixed column type
     
    Mr Smith, Feb 9, 2005
    #5
  6. Mr Smith

    Dotty Guest

    "Mr Smith" <> wrote in message
    news:20050209172946.7d59cd74@recif...
    > On Wed, 09 Feb 2005 16:32:38 +0100
    > Andrea Desole <_SPAM_PLEASE.nl> wrote:
    >
    > > Mr Smith wrote:
    > > >
    > > > it's not a table of Double, it's a table with some column of Double :)

    > >
    > > you have a table with mixed objects? Mmmm, that sounds dangerous. I
    > > would try to restrict it somehow.

    >
    > well it's not my choice ;) my boss want a database/spreadsheet with mixed

    column type

    I do this, just use "Object[][] data;"
    then put a different type in each column. Works well with database.
    Then pass this array to the JTable constructor.

    int k = 0; // count records
    while (rec.next()) {
    for (int i = 0; i < numCols; i++) {
    data[k] = rec.getObject(i + 1);
    }
    k++;
    }
     
    Dotty, Feb 9, 2005
    #6
  7. Mr Smith wrote:
    > On Wed, 09 Feb 2005 14:59:39 +0100
    > Andrea Desole <_SPAM_PLEASE.nl> wrote:
    >
    >
    >>>[...]

    >>
    >>I would go for number 2, because it makes clear that it's a table of
    >>doubles. I think most of the times the getters and setters where you

    >
    >
    > it's not a table of Double, it's a table with some column of Double :)
    >
    >
    >>have to cast are not that many. If it's not the case maybe you can
    >>implement a couple of extra setters and getters in your table to do the
    >>conversion for you; that would still keep the type safety.
    >>I don't really like 3, it allows clients of your class to pass whatever
    >>they want without even thinking about it.

    >
    >
    > thx for your comments :)


    That sounds like a List of SomeClass where each instance of SomeClass
    represents one row from the spreadsheet to me. So that each line in the
    spreadsheet represents one item of some sort. It may well simplify
    things to deal with a one-dimentional list of some more complex object
    instead.

    Steve
     
    Steve Horsley, Feb 9, 2005
    #7
  8. Mr Smith

    Mr Smith Guest

    On Wed, 09 Feb 2005 22:41:06 +0000
    Steve Horsley <> wrote:

    > > [...]

    >
    > That sounds like a List of SomeClass where each instance of SomeClass
    > represents one row from the spreadsheet to me. So that each line in the
    > spreadsheet represents one item of some sort. It may well simplify
    > things to deal with a one-dimentional list of some more complex object
    > instead.


    Sounds like a good idea, but i don't know if at some point we will not be limited by this approach. We do data analysis, and i'm pretty sure that in the future the column will be used as complex objects. So i'd rather not choose to see rows or columns as complex object, to keep it simple.
     
    Mr Smith, Feb 10, 2005
    #8
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Matthew Monopole

    overload operator -

    Matthew Monopole, Aug 8, 2003, in forum: C++
    Replies:
    1
    Views:
    494
    Jakob Bieling
    Aug 8, 2003
  2. Piotre Ugrumov
    Replies:
    3
    Views:
    395
    Nick Hounsome
    Jan 25, 2004
  3. Tim Clacy
    Replies:
    15
    Views:
    2,753
    Kanenas
    May 30, 2005
  4. exponentiation operator (lack of)

    , Dec 22, 2005, in forum: C Programming
    Replies:
    67
    Views:
    1,535
    Dave Thompson
    Jan 4, 2006
  5. Ying-Chieh Liao

    function overload (not operator overload)

    Ying-Chieh Liao, Oct 11, 2004, in forum: Perl Misc
    Replies:
    3
    Views:
    281
    Sherm Pendley
    Oct 11, 2004
Loading...

Share This Page