Skip to content

Commit

Permalink
add tolerance param
Browse files Browse the repository at this point in the history
Signed-off-by: Mathieu DEHARBE <mathieu.deharbe@rte-france.com>
  • Loading branch information
Mathieu-Deharbe committed Nov 19, 2024
1 parent 2da48d8 commit 711ded8
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 10 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -15,9 +15,13 @@
* @param type the type of filter (contains, startsWith...)
* @param value the value of the filter
* @param column the column / field on which the filter will be applied
* @param tolerance precision/tolerance used for the comparisons (simulates the rounding of the database values) Only useful for numbers.
* @author Kevin Le Saulnier <kevin.lesaulnier at rte-france.com>
*/
public record ResourceFilterDTO(DataType dataType, Type type, Object value, String column) {
public record ResourceFilterDTO(DataType dataType, Type type, Object value, String column, Double tolerance) {
public ResourceFilterDTO(DataType dataType, Type type, Object value, String column) {
this(dataType, type, value, column, null);
}

public enum DataType {
@JsonProperty("text")
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -150,16 +150,21 @@ private static <X> Specification<X> appendNumberFilterToSpecification(Specificat
}

private static <X> Specification<X> createNumberPredicate(Specification<X> specification, ResourceFilterDTO resourceFilter, String filterValue) {
// the reference for the comparison is the number of digits after the decimal point in filterValue
// extra digits are ignored, but the user may add '0's after the decimal point in order to get a better precision
String[] splitValue = filterValue.split("\\.");
int numberOfDecimalAfterDot = 0;
if (splitValue.length > 1) {
numberOfDecimalAfterDot = splitValue[1].length();
double tolerance;
if (resourceFilter.tolerance() != null) {
tolerance = resourceFilter.tolerance();
} else {
// the reference for the comparison is the number of digits after the decimal point in filterValue
// extra digits are ignored, but the user may add '0's after the decimal point in order to get a better precision
String[] splitValue = filterValue.split("\\.");
int numberOfDecimalAfterDot = 0;
if (splitValue.length > 1) {
numberOfDecimalAfterDot = splitValue[1].length();
}
// tolerance is multiplied by 0.5 to simulate the fact that the database value is rounded (in the front, from the user viewpoint)
// more than 13 decimal after dot will likely cause rounding errors due to double precision
tolerance = Math.pow(10, -numberOfDecimalAfterDot) * 0.5;
}
// tolerance is multiplied by 0.5 to simulate the fact that the database value is rounded (in the front, from the user viewpoint)
// more than 13 decimal after dot will likely cause rounding errors due to double precision
final double tolerance = Math.pow(10, -numberOfDecimalAfterDot) * 0.5;
Double valueDouble = Double.valueOf(filterValue);
return switch (resourceFilter.type()) {
case NOT_EQUAL -> specification.and(notEqual(resourceFilter.column(), valueDouble, tolerance));
Expand Down

0 comments on commit 711ded8

Please sign in to comment.