How to handle the lack of operator overload ?

M

Mr Smith

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
 
A

Andrea Desole

Mr said:
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.
 
M

Mr Smith

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 :)
 
A

Andrea Desole

Mr said:
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.
 
M

Mr Smith

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
 
D

Dotty

Mr Smith said:
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++;
}
 
S

Steve Horsley

Mr said:

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
 
M

Mr Smith


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.
 

Ask a Question

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

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,769
Messages
2,569,581
Members
45,056
Latest member
GlycogenSupporthealth

Latest Threads

Top