Class DoubleValue
This class extends Value<Double> and provides a convenient way to pass mutable
double references to lambdas, inner classes, or methods.
Notes:
- This class is not thread-safe.
Example:
See Also:
-
Constructor Summary
Constructors -
Method Summary
Modifier and TypeMethodDescriptionstatic DoubleValuecreate()Creates a new double value initialized to0.0 .booleanis(double other, double precision) Checks if the current value equals the specified value within the given precision.booleanisAny(double precision, double... values) Checks if the current value matches any of the specified values within the given precision.static DoubleValueCreates a new double value with the specified initial value.
-
Constructor Details
-
DoubleValue
Constructor.- Parameters:
value- The initial value.
-
-
Method Details
-
create
Creates a new double value initialized to0.0 .Example:
DoubleValue
value = DoubleValue.create ();assertEquals (0.0,value .get());- Returns:
- A new double value.
-
of
Creates a new double value with the specified initial value.Example:
DoubleValue
value = DoubleValue.of (3.14159);assertEquals (3.14159,value .get());- Parameters:
value- The initial value.- Returns:
- A new double value.
-
is
Checks if the current value equals the specified value within the given precision.This method handles
null values safely and performs precision-based equality comparison using absolute difference. The comparison returnstrue if the absolute difference between the two values is less than or equal to the specified precision.Example:
DoubleValue
value = DoubleValue.of (3.14159);assertTrue (value .is(3.14, 0.01));// Within 0.01 assertFalse (value .is(3.14, 0.001));// Not within 0.001 // Handles null values DoubleValueempty = DoubleValue.create ();empty .set(null );assertFalse (empty .is(3.14, 0.01));// Null doesn't match any value // Floating-point precision example DoubleValuecalc = DoubleValue.of (0.1 + 0.2);// Actually 0.30000000000000004 assertTrue (calc .is(0.3, 0.000001));// Handles rounding errors - Parameters:
other- The value to compare with.precision- The maximum allowed difference for equality. Must be non-negative.- Returns:
true if the absolute difference between the values is less than or equal to precision.- Throws:
IllegalArgumentException- if precision is negative.
-
isAny
Checks if the current value matches any of the specified values within the given precision.This method handles
null values safely and performs precision-based equality comparison for each value using absolute difference.Example:
DoubleValue
value = DoubleValue.of (3.14159);assertTrue (value .isAny(0.01, 2.5, 3.15, 5.0));// Matches 3.15 within 0.01 assertFalse (value .isAny(0.001, 1.0, 2.0, 5.0));// No match within 0.001 // Empty array returns false assertFalse (value .isAny(0.01));- Parameters:
precision- The maximum allowed difference for equality. Must be non-negative and is the first parameter.values- The values to compare to.- Returns:
true if the current value matches any of the specified values within the precision.- Throws:
IllegalArgumentException- if precision is negative.
-