Basically i don't want to compare elements, but elements that thy contain. It would be cool if following would work, but it does not.
public boolean equals(Ie<T> that) {
T This = this.value;
T That = that.value;
boolean x = That.compareTo(This) == 0;
return x;
}
What i have done is :
public boolean equals(Object obj) {
if (obj == null)
return false;
String tmp = obj.toString();
if (this.value instanceof Integer)
return equalsInt(new Ie<Integer>(Integer.parseInt(tmp)));
// etc.
return false;
}
public boolean equalsInt(Ie<Integer> ie) {
Integer This = this.value.intValue();
Integer That = ie.value;
boolean x = That.compareTo(This) == 0;
return x;
}
This is a obvious brute-force solution. Can it be done without obj.toString(); + new element?
Edit:
Class Ie is defined as:
public static class Ie<T extends Number & Comparable<? super T>>
extends Number implements Comparable<Ie<T>> {
Class has rather simple compare to method:
public int compareTo(Ie<T> that) {
T This = this.value;
T That = that.value;
return (This.compareTo(That) < 0 ? -1 : (This
.compareTo(That) > 0 ? 1 : 0));
}
I am using the class in Collection addall method, where contains is calling equals.
Erikson solution worked fine:
@Override
public boolean equals(Object that) {
if (that == null)
return false;
if (this == that)
return true;
if (that instanceof Ie<?>) {
Object ThisValue = this.value;
Object ThatValue = ((Ie<?>) that).value;
return ThisValue.equals(ThatValue);
}
return false;
}