package at.aztec.dispatcher.base.session.core.diff.service;

import static java.math.BigDecimal.ROUND_HALF_UP;

import java.math.BigDecimal;
import java.util.Optional;

import org.javers.core.diff.changetype.ValueChange;
import org.javers.core.diff.custom.CustomPropertyComparator;
import org.javers.core.metamodel.object.GlobalId;
import org.javers.core.metamodel.property.Property;

/**
 * Compares BigDecimals with custom precision.
 * Before comparing, values are rounded (HALF_UP) to required scale.
 * <br/><br/>
 *
 * Usage example:
 * <pre>
 * JaversBuilder.javers()
 *     .registerCustomComparator(new CustomBigDecimalComparator(2), BigDecimal.class).build();
 * </pre>
 *
 * @author bartosz walacik
 */
public class CustomBigDecimalComparator implements CustomPropertyComparator<BigDecimal, ValueChange> {

    private int significantDecimalPlaces;

    public CustomBigDecimalComparator(int significantDecimalPlaces) {
        this.significantDecimalPlaces = significantDecimalPlaces;
    }

    @Override
    public Optional<ValueChange> compare(BigDecimal left, BigDecimal right, GlobalId affectedId, Property property) {
        if (equals(left, right)) {
            return Optional.empty();
        }

        return Optional.of(new ValueChange(affectedId, property.getName(), left, right));
    }

    @Override
    public boolean equals(BigDecimal a, BigDecimal b) {
        if (a == null) {
            return b == null;
        }

        if (b == null) {
            return false;
        }

        BigDecimal aRounded = a.setScale(significantDecimalPlaces, ROUND_HALF_UP);
        BigDecimal bRounded = b.setScale(significantDecimalPlaces, ROUND_HALF_UP);

        return aRounded.equals(bRounded);
    }
}
@@ -5,166 +5,35 @@
 age 
-at.aztec.dispatcher.base.session.core.diff.service;%0A%0Aimport static java.math.BigDecimal.ROUND_HALF_UP;%0A%0Aimport java.math.BigDecimal;%0Aimport java.util.Optional
+org.javers.core.diff.custom
 ;%0A%0Ai
@@ -87,69 +87,8 @@
 ge;%0A
-import org.javers.core.diff.custom.CustomPropertyComparator;%0A
 impo
@@ -184,16 +184,122 @@
 roperty;
+%0Aimport java.math.BigDecimal;%0Aimport java.util.Optional;%0Aimport static java.math.BigDecimal.ROUND_HALF_UP;
 %0A%0A/**%0A *
@@ -721,19 +721,17 @@
 eChange%3E
- %7B%0A
+%7B
 %0A    pri
@@ -1038,16 +1038,20 @@
 roperty)
+%0A   
  %7B%0A     
@@ -1077,17 +1077,16 @@
  right))
- 
 %7B%0A      
@@ -1638,8 +1638,9 @@
 %0A    %7D%0A%7D
+%0A

Add null checks to the CustomBigDecimalComparator