functionalj.lens.lenses.DoubleAccessEqual Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of functionalj-core Show documentation
Show all versions of functionalj-core Show documentation
The module for FunctionalJ Core.
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);
}
}