so I'm doing some generics stuff and I'm not sure why this issue occurs, hope someone could explain this to me. I'm hazarding a guess and thinking that some precision (for doubles, float) is lost, but I can't be sure.
SomeX<Double> a = new SomeX<>(4.4);
SomeX<Double> b = new SomeX<>(4.4);
a.getValue() == b.getValue(); // returns false
a.getValue().equals(b.getValue()); // returns true
SomeX<Integer> x = new SomeX<>(4);
SomeX<Integer> y = new SomeX<>(4);
x.getValue() == y.getValue(); // returns true
x.getValue().equals(y.getValue()); // also returns true
As reference, this is the code for my equals:
@Override
// Type conversion of all variables in this method is safe
// because originally the values are all initially SomeX of type T.
@SuppressWarnings("unchecked")
public boolean equals(Object obj) {
// if obj is a SomeX, then we'll need to check
if (obj instanceof SomeX) {
SomeX<T> objCast = (SomeX<T>) obj;
// if obj is wrapped up in another SomeX
if (this.getValue() instanceof SomeX && objCast.getValue() instanceof SomeX) {
// then we simply unwrap it
SomeX<T> temp1 = (SomeX<T>) this.getValue();
SomeX<T> temp2 = (SomeX<T>) objCast.getValue();
while (temp1.getValue() instanceof SomeX && temp2.getValue() instanceof SomeX) {
temp1 = (SomeX<T>) temp1.getValue();
temp2 = (SomeX<T>) temp2.getValue();
}
return temp1.getValue() == temp2.getValue();
} else {
// otherwise, we compare the contents of each SomeX.
if (this.getValue() == null || objCast.getValue() == null) {
// to handle null case because equals() cannot be used
return this.getValue() == objCast.getValue();
} else {
// to handle all other instances
T temp3 = (T) this.getValue();
T temp4 = (T) objCast.getValue();
return temp3.equals(temp4);
}
}
} else {
// if object is not a SomeX, then return false, no need to check
return false;
}
}