Equals and compareTo slow application

I have a custom object that seems to be slowing my application. I have profiled the code and it seems to be the equals and compareTo methods that are to blame. Can anyone suggest a way to improve these two methods to make them faster? Does overriding these methods always cause things to slow down?

publicclass TableValueimplements Comparable{

private Double dValue;

private String sValue;

public TableValue(String sValue){

this.sValue=sValue;

}

public TableValue(Double dValue){

this.dValue=dValue;

}

public Double getDoubleValue(){

return dValue;

}

public String getStringValue(){

return sValue;

}

publicboolean isDouble(){

return sValue==null;

}

publicboolean equals(Object o){

if(o==null)returnfalse;

if(this==o)returntrue;

return o.equals(sValue) || o.equals(dValue);

}

publicint hashCode(){

int hash=0;

hash += (null == dValue ? 0 : dValue.hashCode());

hash += (null == sValue ? 0 : sValue.hashCode());

return hash;

}

publicint compareTo(Object anotherTableValue)throws ClassCastException{

if(!(anotherTableValueinstanceof TableValue))

thrownew ClassCastException("TableValue object expected.");

//compareTo for two Doubles

if(((TableValue)anotherTableValue).getDoubleValue()!=null && this.getDoubleValue()!=null){

Double anotherDouble=((TableValue)anotherTableValue).getDoubleValue();

return this.dValue.compareTo(anotherDouble);

}

//compareTo for two Strings

if(((TableValue)anotherTableValue).getStringValue()!=null && this.getStringValue()!=null){

String anotherString=((TableValue)anotherTableValue).getStringValue();

return this.sValue.compareTo(anotherString);

}

//sort Strings before Doubles

if(((TableValue)anotherTableValue).getStringValue()!=null &&

this.getStringValue()==null){

return 1;

}

//sort Strings before Doubles

return -1;

}

public String toString(){

if(dValue==null){

return sValue;

}

return dValue.toString();

}

}

[4981 byte] By [Jstatsa] at [2007-11-27 9:50:49]
# 1

They could both do with a rewrite.

Your equals() method isn't consistent with your compareTo() method. It assumes that the incoming Object is a double or a String, not a TableValue. In fact equals() could just return compareTo(o) == 0.

You could make the class implement Comparable<TableValue> and get rid of the first test in compareTo().

You could get rid of all the duplicate calls of getDoubleValue() and getStringValue().

You could stop calling accessor methods on the current object inside the compareTo() method (e.g. this.getStringValue()) and just use 'sValue' (etc) instead.

You could fix the end of compareTo() so that the comment

//sort Strings before Doubles

is true, which it's not at the moment.

The comment isn't even relevant, as TableValue can apparently only hold either a double or a String, not both. In fact you could adjust TableValue so it is parameterized on T where T is either Double or String, and contains a single T value, so that you didn't have any of this business of it having to decide which value is which. T could be declared as <T extends Comparable><T> > so that the entire compareTo() method just resolves to calling compareTo on 'value'.

ejpa at 2007-7-13 0:19:41 > top of Java-index,Java Essentials,Java Programming...
# 2

Thanks for all of the recommendations.

> The comment isn't even relevant, as TableValue can

> apparently only hold either a double or a String, not

> both. In fact you could adjust TableValue so it is

> parameterized on T where T is either Double or

> String, and contains a single T value, so that you

> didn't have any of this business of it having to

> decide which value is which. T could be declared as

> <T extends Comparable><T> > so that the entire

> compareTo() method just resolves to calling compareTo

> on 'value'.

Could you show me an example of how to do this? How do I parameratize to two different possible classes? I think this is what I am trying to do, but I can't figure it out.

Jstatsa at 2007-7-13 0:19:41 > top of Java-index,Java Essentials,Java Programming...
# 3

Something like this, module typos and bugs:

public class TableValue<T extends Comparable><T> > implements Comparable<TableValue><T> >

{

private T value;

public TableValue(T value)

{

this.value = value;

}

public T getValue() { return value; }

public boolean equals(Object that)

{

if (!(that instanceof TableValue))

return false;

TableValue<T> tvThat = (TableValue<T>))that;

return this.value.equals(tvThat.value);

}

public int compareTo(TableValue<T> that)

{

if (that == null) return 1; // or whatever it should be

return this.value.compareTo(that.value);

}

}

and now it will work for T= String, Double, Integer, Float, Short, Long, Byte, Character, Date, ...

ejpa at 2007-7-13 0:19:41 > top of Java-index,Java Essentials,Java Programming...