Class ShortValue

java.lang.Object
org.apache.juneau.commons.lang.Value<Short>
org.apache.juneau.commons.lang.ShortValue

public class ShortValue extends Value<Short>
A simple mutable short value.

This class extends Value<Short> 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 ShortValue counter = ShortValue.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

    • ShortValue

      public ShortValue(Short value)
      Constructor.
      Parameters:
      value - The initial value.
  • Method Details

    • create

      public static ShortValue create()
      Creates a new short value initialized to 0.
      Example:

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

      Returns:
      A new short value.
    • of

      public static ShortValue of(Short value)
      Creates a new short value with the specified initial value.
      Example:

      ShortValue counter = ShortValue.of((short)42); assertEquals(42, counter.get());

      Parameters:
      value - The initial value.
      Returns:
      A new short value.
    • add

      public ShortValue add(Short x)
      Adds the specified value to the current value.
      Example:

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

      Parameters:
      x - The value to add.
      Returns:
      This object.
    • addAndGet

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

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

      Parameters:
      x - The value to add.
      Returns:
      The new value after addition.
    • decrement

      Decrements the value by 1.
      Example:

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

      Returns:
      This object.
    • decrementAndGet

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

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

      Returns:
      The decremented value.
    • getAndIncrement

      public short getAndIncrement()
      Returns the current value and then increments it.
      Example:

      ShortValue counter = ShortValue.of((short)5); short current = counter.getAndIncrement(); // Returns 5 short next = counter.get(); // Returns 6

      Returns:
      The value before it was incremented.
    • increment

      Increments the value by 1.
      Example:

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

      Returns:
      This object.
    • incrementAndGet

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

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

      Returns:
      The incremented value.
    • is

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

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

      Example:

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

      Overrides:
      is in class Value<Short>
      Parameters:
      value - The value to compare to.
      Returns:
      true if the current value is equal to the specified value.
    • isAny

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

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

      Example:

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

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