Initially code was :
if(!Objects.equals(src.getApplicationItemCost(), dest.getApplicationItemCost())){
log.info("Difference")
}
Output:
getApplicationItemCost: src:0.0| dest:0
Difference
ApplicationItemCost is of type BigDecimal.
If I use compareTo
then I have to explicitly check nulls like :
LOG.info("getApplicationItemCost: src:" + src.getApplicationItemCost() + "| dest:" + dest.getApplicationItemCost());
if((src.getApplicationItemCost()==null && dest.getApplicationItemCost()!=null)
|| (src.getApplicationItemCost()!=null && dest.getApplicationItemCost()==null)
|| !Objects.equals(src.getApplicationItemCost(), dest.getApplicationItemCost())
|| src.getApplicationItemCost().compareTo(dest.getApplicationItemCost())!=0 )
Any suggestion to compare 0.0 and 0. Why is this difference? (May be database has Number field and when converted to big decimal it does not show 0.0?)
Using the compareTo Method Two BigDecimal objects that are equal in value but have a different scale (like 2.0 and 2.00) are considered equal by this method. Therefore, we can check BigDecimal. ZERO. compareTo(givenBdNumber) == 0 to decide if givenBdNumber has the value zero.
You either never allow null values in database, application or view and initialize everything with new BigDecimal(0) or perform null checks on every usage for nullable values.
equals() method compares this BigDecimal with the specified Object for equality. Unlike compareTo, this method considers two BigDecimal objects equal only if they are equal in value and scale (thus 2.0 is not equal to 2.00 when compared by this method).
Build a comparator:
Comparator<BigDecimal> c = Comparator.nullsFirst(Comparator.naturalOrder());
(Or nullsLast
, it doesn't matter if you're only ever comparing to zero).
Then:
if (c.compare(first, second) != 0) {
// ...
}
Try using
CompareToBuilder
class provided by
org.apache.commons.lang3
as it make null safe comparison. Sample code is:
public static <T, U> int nullSafeComparison(T t, U u) {
return new CompareToBuilder().append(t, u).toComparison();
}
public static void main(String[] args) {
BigDecimal zero = BigDecimal.ZERO;
BigDecimal zeroPzero = new BigDecimal("0.0");
System.out.println( zero + " " + zeroPzero);
System.out.println(nullSafeComparison(zero, zeroPzero));
}
If both numbers are same it will return 0, if 1st is greater than 2nd the result will be 1 and -1 if 1st number is less than 2nd.
Custom BigDecimal null safe Comparison (without any exteranal library):
{
BigDecimal big1, big2;
big1 = new BigDecimal(0);
big2 = new BigDecimal(0.0);
int compareResult1 = compareTo(b1,null);
int compareResult2 = compareTo(null,b2);
}
public static <T extends Comparable<T>> int compareTo(final T c1, final T c2) {
final boolean f1, f2;
return (f1 = c1 == null) ^ (f2 = c2 == null) ? f1 ? -1 : 1 : f1 && f2 ? 0 : c1.compareTo(c2);
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With