public class ObjectUtil extends Object
| Constructor and Description |
|---|
ObjectUtil() |
| Modifier and Type | Method and Description |
|---|---|
static <T,U extends T> |
eq(T o1,
U o2)
Most of the time, we want to use
o1.equals(o2) with objects of the same type. |
public static <T,U extends T> boolean eq(T o1,
U o2)
o1.equals(o2) with objects of the same type. For instance:
return aDate.equals(foo.getTime());
In our example, foo.getTime() returns a Date, which is fine. Lets say we refactor the code and decide getTime() will return a Long (millis) instead. The
code above is now broken (it always return false), but it will compile without error and it will also execute without throwing exception at runtime. That
kind of bug can be very costly to find and fix. It would be great if the compiler could tell us that we are using incompatible types. The function below
does just that.
Static typing is a great feature of the Java language. However, IMHO the function Object.equals(Object) was badly designed from the very beginning of
Java and we now have to live with it. We do not need to have the method equals() and hascode() in the Object class. Classes that support that feature
should implement the interface Equalable (just like Comparable).T - an objectU - an object extends To1 - objecto2 - objectCopyright © 2018–2019 Butor Inc.. All rights reserved.