Class DoubleHolder

java.lang.Object
org.apache.juneau.commons.lang.Holder<Double>
org.apache.juneau.commons.lang.DoubleHolder

public class DoubleHolder extends Holder<Double>
A simple mutable double value.

This class extends Holder<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 DoubleHolder sum = DoubleHolder.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

    • DoubleHolder

      public DoubleHolder(Double value)
      Constructor.
      Parameters:
      value - The initial value. Can be null.
  • Method Details

    • create

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

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

      Returns:
      A new double value.
    • of

      public static DoubleHolder of(Double value)
      Creates a new double value with the specified initial value.
      Example:

      DoubleHolder value = DoubleHolder.of(3.14159); assertEquals(3.14159, value.get());

      Parameters:
      value - The initial value. Can be null.
      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:

      DoubleHolder value = DoubleHolder.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 DoubleHolder empty = DoubleHolder.create(); empty.set(null); assertFalse(empty.is(3.14, 0.01)); // Null doesn't match any value // Floating-point precision example DoubleHolder calc = DoubleHolder.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:

      DoubleHolder value = DoubleHolder.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. Must not be null.
      Returns:
      true if the current value matches any of the specified values within the precision.
      Throws:
      IllegalArgumentException - if precision is negative.