Class FloatHolder

java.lang.Object
org.apache.juneau.commons.lang.Holder<Float>
org.apache.juneau.commons.lang.FloatHolder

public class FloatHolder extends Holder<Float>
A simple mutable float value.

This class extends Holder<Float> and provides a convenient way to pass mutable float references to lambdas, inner classes, or methods.

Notes:
  • This class is not thread-safe.
Example:

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

See Also:
  • Constructor Details

    • FloatHolder

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

    • create

      public static FloatHolder create()
      Creates a new float value initialized to 0.0f.
      Example:

      FloatHolder value = FloatHolder.create(); assertEquals(0.0f, value.get());

      Returns:
      A new float value.
    • of

      public static FloatHolder of(Float value)
      Creates a new float value with the specified initial value.
      Example:

      FloatHolder value = FloatHolder.of(3.14f); assertEquals(3.14f, value.get());

      Parameters:
      value - The initial value. Can be null.
      Returns:
      A new float value.
    • is

      public boolean is(float other, float 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:

      FloatHolder value = FloatHolder.of(3.14f); assertTrue(value.is(3.14f, 0.01f)); // Exact match within 0.01 assertTrue(value.is(3.15f, 0.02f)); // Within 0.02 assertFalse(value.is(3.15f, 0.001f)); // Not within 0.001 // Handles null values FloatHolder empty = FloatHolder.create(); empty.set(null); assertFalse(empty.is(3.14f, 0.01f)); // Null doesn't match any value // Precision-based comparison FloatHolder pi = FloatHolder.of(3.14159f); assertTrue(pi.is(3.14f, 0.002f)); // Within 0.002 precision

      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(float precision, float... 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:

      FloatHolder value = FloatHolder.of(3.14f); assertTrue(value.isAny(0.01f, 2.5f, 3.15f, 5.0f)); // Matches 3.15f within 0.01 assertFalse(value.isAny(0.001f, 1.0f, 2.0f, 5.0f)); // No match within 0.001 // Empty array returns false assertFalse(value.isAny(0.01f));

      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.