All Downloads are FREE. Search and download functionalities are using the official Maven repository.

org.javers.core.diff.custom.CustomBigDecimalComparator Maven / Gradle / Ivy

There is a newer version: 7.6.1
Show newest version
package org.javers.core.diff.custom;

import org.javers.core.diff.changetype.ValueChange;
import org.javers.core.metamodel.object.GlobalId;
import org.javers.core.metamodel.property.Property;
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()
 *     .registerCustomComparator(new CustomBigDecimalComparator(2), BigDecimal).build();
 * 
* * @author bartosz walacik */ public class CustomBigDecimalComparator implements CustomPropertyComparator{ private int significantDecimalPlaces; public CustomBigDecimalComparator(int significantDecimalPlaces) { this.significantDecimalPlaces = significantDecimalPlaces; } @Override public ValueChange compare(BigDecimal left, BigDecimal right, GlobalId affectedId, Property property) { BigDecimal leftRounded = left.setScale(significantDecimalPlaces, ROUND_HALF_UP); BigDecimal rightRounded = right.setScale(significantDecimalPlaces, ROUND_HALF_UP); if (leftRounded.equals(rightRounded)){ return null; } return new ValueChange(affectedId, property.getName(), left, right); } }




© 2015 - 2024 Weber Informatics LLC | Privacy Policy