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

functionalj.lens.lenses.DoubleAccessEqualPrecision Maven / Gradle / Ivy

There is a newer version: 1.0.17
Show newest version
package functionalj.lens.lenses;

import static functionalj.lens.lenses.DoubleAccess.equalPrecisionToUse;
import static java.util.Objects.requireNonNull;
import static nullablej.nullable.Nullable.nullable;

import java.util.function.DoubleUnaryOperator;

import lombok.NonNull;
import lombok.val;


public class DoubleAccessEqualPrecision implements BooleanAccessPrimitive {
    
    final DoubleAccessEqual equals;
    final DoubleUnaryOperator     precisionFromErrorFunction;
    
    public DoubleAccessEqualPrecision(
            @NonNull DoubleAccessEqual equals,
            @NonNull DoubleUnaryOperator        precisionFromErrorFunction) {
        this.equals
                = requireNonNull(equals);
        this.precisionFromErrorFunction
                = nullable(precisionFromErrorFunction)
                .orElse((error) -> equalPrecisionToUse.get().getAsDouble());
    }
    
    @Override
    public boolean test(HOST host) {
        val value        = equals.access.applyAsDouble(host);
        val anotherValue = equals.anotherValueFunction.applyAsDouble(host, value);
        val error        = Math.abs(value - anotherValue);
        val precision    = precisionFromErrorFunction.applyAsDouble(error);
        return equals.isNegate != (error <= precision);
    }
    
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy