Class LongValue

java.lang.Object
org.apache.juneau.commons.lang.Value<Long>
org.apache.juneau.commons.lang.LongValue

public class LongValue extends Value<Long>
A simple mutable long value.

This class extends Value<Long> 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 AtomicLong instead.
Example:

// Create a counter LongValue counter = LongValue.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

    • LongValue

      public LongValue(Long value)
      Constructor.
      Parameters:
      value - The initial value.
  • Method Details

    • create

      public static LongValue create()
      Creates a new long value initialized to 0.
      Example:

      LongValue counter = LongValue.create(); assertEquals(0L, counter.get());

      Returns:
      A new long value.
    • of

      public static LongValue of(Long value)
      Creates a new long value with the specified initial value.
      Example:

      LongValue counter = LongValue.of(42L); assertEquals(42L, counter.get());

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

      public LongValue add(Long x)
      Adds the specified value to the current value.
      Example:

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

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

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

      LongValue value = LongValue.of(10L); long result = value.addAndGet(5L); // Returns 15L assertEquals(15L, value.get());

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

      public LongValue decrement()
      Decrements the value by 1.
      Example:

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

      Returns:
      This object.
    • decrementAndGet

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

      LongValue counter = LongValue.of(5L); long result = counter.decrementAndGet(); // Returns 4L assertEquals(4L, counter.get());

      Returns:
      The decremented value.
    • getAndIncrement

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

      LongValue counter = LongValue.of(5L); long current = counter.getAndIncrement(); // Returns 5L long next = counter.get(); // Returns 6L

      Returns:
      The value before it was incremented.
    • increment

      public LongValue increment()
      Increments the value by 1.
      Example:

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

      Returns:
      This object.
    • incrementAndGet

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

      LongValue counter = LongValue.of(5L); long result = counter.incrementAndGet(); // Returns 6L assertEquals(6L, counter.get());

      Returns:
      The incremented value.
    • is

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

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

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

      public boolean isAny(Long... 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:

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

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