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

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

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

import java.util.function.DoubleSupplier;
import java.util.function.DoubleUnaryOperator;

import functionalj.function.ObjectDoubleToDoubleFunctionPrimitive;
import lombok.NonNull;
import lombok.val;


public class DoubleAccessEqual implements BooleanAccessPrimitive {
    
    final boolean isNegate;
    final DoubleAccess access;
    final ObjectDoubleToDoubleFunctionPrimitive anotherValueFunction;
    
    DoubleAccessEqual(
            boolean isNegate,
            @NonNull DoubleAccess access,
            @NonNull ObjectDoubleToDoubleFunctionPrimitive anotherValueFunction) {
        this.isNegate             = isNegate;
        this.access               = access;
        this.anotherValueFunction = anotherValueFunction;
    }
    
    public boolean test(HOST host) {
        val value        = access.applyAsDouble(host);
        val anotherValue = anotherValueFunction.applyAsDouble(host, value);
        val error        = Math.abs(value - anotherValue);
        val precision    = DoubleAccess.equalPrecisionToUse.get().getAsDouble();
        return isNegate != (error <= precision);
    }
    
    public DoubleAccessEqualPrecision withIn(double precision) {
        return new DoubleAccessEqualPrecision<>(this, error -> precision);
    }
    
    public DoubleAccessEqualPrecision withPrecision(double precision) {
        return new DoubleAccessEqualPrecision<>(this, error -> precision);
    }
    
    public DoubleAccessEqualPrecision withPrecision(@NonNull DoubleSupplier precisionSupplier) {
        return new DoubleAccessEqualPrecision<>(this, error -> precisionSupplier.getAsDouble());
    }
    
    public DoubleAccessEqualPrecision withPrecision(@NonNull DoubleUnaryOperator precisionFunction) {
        return new DoubleAccessEqualPrecision<>(this, precisionFunction);
    }
    
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy