I'm writing universal compare utility, it should be type safe as possible. Idea is to prevent comparing of different class Objects. For example this works OK:
UtilityClazz.<String>compare("dd", 1); //OK - compile time error
Is there more elegant way to call Utility class, that has type parameter. This below is not OK because it will compare Date and int
compare(SomeObject.getDate(), 1); //NOT OK - compiles OK no type is passed
Method Code:
public static<T> int compare(T value1, T value2) {
if (value1 instanceof Time && value2 instanceof Time) {
return DateComparator.compareTimes((Time) value1, (Time) value2);
} else if (value1 instanceof Date && value2 instanceof Date) {
return DateComparator.compareDates((Date) value1, (Date) value2);
} else if (value1 instanceof Number && value2 instanceof Number) {
return NumberUtility.compare((Number) value1, (Number) value2);
} else {
//....
return 0;
}
}
}
How to make method call elegant as possible ?