I know that if you compare a boxed primitive Integer with a constant such as:
Integer a = 4;
if (a < 5)
a
will automaticall
In my case I had to compare two Integer
s for equality where both of them could be null
. Searched similar topic, didn't found anything elegant for this. Came up with a simple utility functions.
public static boolean integersEqual(Integer i1, Integer i2) {
if (i1 == null && i2 == null) {
return true;
}
if (i1 == null && i2 != null) {
return false;
}
if (i1 != null && i2 == null) {
return false;
}
return i1.intValue() == i2.intValue();
}
//considering null is less than not-null
public static int integersCompare(Integer i1, Integer i2) {
if (i1 == null && i2 == null) {
return 0;
}
if (i1 == null && i2 != null) {
return -1;
}
return i1.compareTo(i2);
}