Class FloatValue

java.lang.Object
org.apache.juneau.commons.lang.Value<Float>
org.apache.juneau.commons.lang.FloatValue

public class FloatValue extends Value<Float>
A simple mutable float value.

This class extends Value<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 FloatValue sum = FloatValue.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

    • FloatValue

      public FloatValue(Float value)
      Constructor.
      Parameters:
      value - The initial value.
  • Method Details

    • create

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

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

      Returns:
      A new float value.
    • of

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

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

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

      FloatValue value = FloatValue.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 FloatValue empty = FloatValue.create(); empty.set(null); assertFalse(empty.is(3.14f, 0.01f)); // Null doesn't match any value // Precision-based comparison FloatValue pi = FloatValue.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:

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