Class IntegerHolder

java.lang.Object
org.apache.juneau.commons.lang.Holder<Integer>
org.apache.juneau.commons.lang.IntegerHolder

public class IntegerHolder extends Holder<Integer>
A simple mutable integer value.

This class extends Holder<Integer> and adds a convenience method for incrementing the value, which is useful for counting operations in lambdas and loops.

Notes:
  • This class is not thread-safe. For concurrent access, use AtomicInteger instead.
Example:

// Create a counter IntegerHolder counter = IntegerHolder.create(); // Use in a lambda to count valid items list.forEach(x -> { if (x.isValid()) { counter.getAndIncrement(); } }); log("Valid items: " + counter.get());

See Also:
  • Constructor Details

    • IntegerHolder

      public IntegerHolder()
      Constructor.
    • IntegerHolder

      public IntegerHolder(Integer value)
      Constructor.
      Parameters:
      value - The initial value. Can be null (treated as 0).
  • Method Details

    • create

      public static IntegerHolder create()
      Creates a new integer value initialized to 0.
      Example:

      IntegerHolder counter = IntegerHolder.create(); assertEquals(0, counter.get());

      Returns:
      A new integer value.
    • of

      public static IntegerHolder of(Integer value)
      Creates a new integer value with the specified initial value.
      Example:

      IntegerHolder counter = IntegerHolder.of(42); assertEquals(42, counter.get());

      Parameters:
      value - The initial value. Can be null (treated as 0).
      Returns:
      A new integer value.
    • add

      Adds the specified value to the current value.
      Example:

      IntegerHolder value = IntegerHolder.of(10); value.add(5); assertEquals(15, value.get());

      Parameters:
      x - The value to add. Can be null (treated as 0).
      Returns:
      This object.
    • addAndGet

      Adds the specified value to the current value and returns the new value.
      Example:

      IntegerHolder value = IntegerHolder.of(10); int result = value.addAndGet(5); // Returns 15 assertEquals(15, value.get());

      Parameters:
      x - The value to add. Can be null (treated as 0).
      Returns:
      The new value after addition.
    • decrement

      Decrements the value by 1.
      Example:

      IntegerHolder counter = IntegerHolder.of(5); counter.decrement(); assertEquals(4, counter.get());

      Returns:
      This object.
    • decrementAndGet

      Decrements the value by 1 and returns the new value.
      Example:

      IntegerHolder counter = IntegerHolder.of(5); int result = counter.decrementAndGet(); // Returns 4 assertEquals(4, counter.get());

      Returns:
      The decremented value.
    • getAndIncrement

      Returns the current value and then increments it.

      This is a convenience method commonly used for counting operations in lambdas and loops.

      Example:

      IntegerHolder counter = IntegerHolder.of(5); int current = counter.getAndIncrement(); // Returns 5 int next = counter.get(); // Returns 6

      Returns:
      The value before it was incremented.
    • increment

      Increments the value by 1.
      Example:

      IntegerHolder counter = IntegerHolder.of(5); counter.increment(); assertEquals(6, counter.get());

      Returns:
      This object.
    • incrementAndGet

      Increments the value by 1 and returns the new value.
      Example:

      IntegerHolder counter = IntegerHolder.of(5); int result = counter.incrementAndGet(); // Returns 6 assertEquals(6, counter.get());

      Returns:
      The incremented value.
    • is

      public boolean is(Integer value)
      Checks if the current value is equal to the specified value.

      Uses Shorts.eq(Object, Object) for deep equality comparison, which handles nulls safely.

      Example:

      IntegerHolder value = IntegerHolder.of(42); assertTrue(value.is(42)); assertFalse(value.is(43));

      Overrides:
      is in class Holder<Integer>
      Parameters:
      value - The value to compare to. Can be null.
      Returns:
      true if the current value is equal to the specified value.
    • isAny

      public boolean isAny(Integer... values)
      Checks if the current value matches any of the specified values.

      Uses Shorts.eq(Object, Object) for deep equality comparison of each value.

      Example:

      IntegerHolder value = IntegerHolder.of(5); assertTrue(value.isAny(3, 5, 7)); assertFalse(value.isAny(1, 2));

      Parameters:
      values - The values to compare to. Must not be null.
      Returns:
      true if the current value matches any of the specified values.