I have this:
// returns true if both are equal (independent of scale) and also checks against null
public static boolean isEqual(BigDecimal val1, BigDecimal val2) {
// 1. check: both will be null or both will be non-null.
if (val1 != null ^ val2 != null) return false;
// 2. check: if not null, then compare if both are equal
return !(val2 != null && val1.compareTo(val2) != 0);
}
I want to merge the boolean expressions into one. So I use this:
public static boolean isEqual(BigDecimal val1, BigDecimal val2) {
return !(val1 != null ^ val2 != null) && !(val2 != null && val1.compareTo(val2) != 0);
}
However, I am worry if this is correct. Is this correct? Could this be simplified/shorten?
With the help of the answer the shorten solution is:
// returns true, if both are null or both are equal
// (independent of their numbers scales)
public static boolean isEqual(BigDecimal val1, BigDecimal val2) {
return val1 == null ? val2 == null : val2 != null && val1.compareTo(val2) == 0;
}