org.javers.core.diff.custom.CustomBigDecimalComparator Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of javers-core Show documentation
Show all versions of javers-core Show documentation
JaVers - object auditing and diff framework for Java
package org.javers.core.diff.custom;
import org.javers.core.JaversBuilder;
import java.math.BigDecimal;
import static java.math.BigDecimal.ROUND_HALF_UP;
/**
* Compares BigDecimals with custom precision.
* Before comparing, values are rounded (HALF_UP) to required scale.
*
*
* Usage example:
*
* JaversBuilder.javers()
* .registerValue(BigDecimal.class, new CustomBigDecimalComparator(2))
* .build();
*
*
* @see JaversBuilder#registerValue(Class, CustomValueComparator)
* @author bartosz walacik
*/
public class CustomBigDecimalComparator implements CustomValueComparator{
private int significantDecimalPlaces;
public CustomBigDecimalComparator(int significantDecimalPlaces) {
this.significantDecimalPlaces = significantDecimalPlaces;
}
@Override
public boolean equals(BigDecimal a, BigDecimal b) {
return round(a).equals(round(b));
}
@Override
public String toString(BigDecimal value) {
return round(value).toString();
}
private BigDecimal round(BigDecimal val) {
return val.setScale(significantDecimalPlaces, ROUND_HALF_UP);
}
}