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.2
Show newest version
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); } }




© 2015 - 2024 Weber Informatics LLC | Privacy Policy