Class ByteHolder

java.lang.Object
org.apache.juneau.commons.lang.Holder<Byte>
org.apache.juneau.commons.lang.ByteHolder

public class ByteHolder extends Holder<Byte>
A simple mutable byte value.

This class extends Holder<Byte> and adds convenience methods for incrementing, decrementing, and testing byte values, which are useful in lambdas and byte manipulation operations.

Notes:
  • This class is not thread-safe. For concurrent access, use synchronization or atomic classes.
Example:

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

See Also:
  • Constructor Details

    • ByteHolder

      public ByteHolder()
      Constructor.
    • ByteHolder

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

    • create

      public static ByteHolder create()
      Creates a new byte value initialized to 0.
      Example:

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

      Returns:
      A new byte value.
    • of

      public static ByteHolder of(Byte value)
      Creates a new byte value with the specified initial value.
      Example:

      ByteHolder value = ByteHolder.of((byte)42); assertEquals(42, value.get());

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

      public ByteHolder add(Byte x)
      Adds the specified value to the current value.
      Example:

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

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

      public Byte addAndGet(Byte x)
      Adds the specified value to the current value and returns the new value.
      Example:

      ByteHolder value = ByteHolder.of((byte)10); byte result = value.addAndGet((byte)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:

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

      Returns:
      This object.
    • decrementAndGet

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

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

      Returns:
      The decremented value.
    • increment

      Increments the value by 1.
      Example:

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

      Returns:
      This object.
    • incrementAndGet

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

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

      Returns:
      The incremented value.
    • is

      public boolean is(Byte 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:

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

      Overrides:
      is in class Holder<Byte>
      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(Byte... 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:

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

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