Class DoubleValue

java.lang.Object
org.apache.juneau.commons.lang.Value<Double>
org.apache.juneau.commons.lang.DoubleValue

public class DoubleValue extends Value<Double>
A simple mutable double value.

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:

// Create a double value to track a running sum DoubleValue sum = DoubleValue.create(); // Use in a lambda to accumulate values list.forEach(x -> { sum.set(sum.get() + x.doubleValue()); }); log("Total: " + sum.get());

See Also:
  • Constructor Details

    • DoubleValue

      public DoubleValue(Double value)
      Constructor.
      Parameters:
      value - The initial value.
  • Method Details

    • create

      public static DoubleValue create()
      Creates a new double value initialized to 0.0.
      Example:

      DoubleValue value = DoubleValue.create(); assertEquals(0.0, value.get());

      Returns:
      A new double value.
    • of

      public static DoubleValue of(Double value)
      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

      public boolean is(double other, double precision)
      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 returns true 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 DoubleValue empty = DoubleValue.create(); empty.set(null); assertFalse(empty.is(3.14, 0.01)); // Null doesn't match any value // Floating-point precision example DoubleValue calc = 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

      public boolean isAny(double precision, double... values)
      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.