Class ConcurrentLazy<T>

java.lang.Object
org.litebridge.commons.type.ConcurrentLazy<T>
Type Parameters:
T - the type of the lazily initialized value

public final class ConcurrentLazy<T> extends Object
A thread-safe, lazily initialized value holder that allows resetting the value.

This class ensures that the value is initialized only once, using the provided Supplier, and that subsequent calls to get() or getOrNull() return the cached value.

The value can be reset using reset(), which clears the cached value and allows it to be recomputed on the next access.

  • Field Summary

    Fields
    Modifier and Type
    Field
    Description
    protected static final Object
    Sentinel object indicating that the lazy value has not yet been initialised.
    protected @Nullable Object
    The cached value, or UNINITIALISED if not yet initialised.
  • Constructor Summary

    Constructors
    Constructor
    Description
    ConcurrentLazy(Supplier<T> initializer)
    Constructs a new ResettableLazy with the given initializer.
  • Method Summary

    Modifier and Type
    Method
    Description
    get()
    The primary accessor.
    @Nullable T
    Returns the lazily initialized value.
    Returns the value, or throws a NoSuchElementException if the value is null.
    <X extends Exception>
    T
    getOrThrow(Supplier<X> exceptionSupplier)
    Returns the value, or throws an exception provided by the supplier if the value is null.
    final boolean
    Returns true if the value has already been initialised, or false otherwise.
    final T
    Returns the current value without triggering initialisation.
    final void
    Resets the cached value.

    Methods inherited from class java.lang.Object

    clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
  • Field Details

    • UNINITIALISED

      protected static final Object UNINITIALISED
      Sentinel object indicating that the lazy value has not yet been initialised.
    • value

      protected volatile @Nullable Object value
      The cached value, or UNINITIALISED if not yet initialised.
  • Constructor Details

    • ConcurrentLazy

      public ConcurrentLazy(Supplier<T> initializer)
      Constructs a new ResettableLazy with the given initializer.
      Parameters:
      initializer - a Supplier that provides the value when it is first needed
      Throws:
      NullPointerException - if the initializer is null
  • Method Details

    • get

      public Optional<T> get()
      The primary accessor. Returns the value wrapped in an Optional.
      Returns:
      the value wrapped in an Optional
    • getOrThrow

      public T getOrThrow()
      Returns the value, or throws a NoSuchElementException if the value is null.
      Returns:
      the value
      Throws:
      NoSuchElementException - if the value is null
    • getOrThrow

      public <X extends Exception> T getOrThrow(Supplier<X> exceptionSupplier) throws X
      Returns the value, or throws an exception provided by the supplier if the value is null.
      Type Parameters:
      X - the type of the exception to be thrown
      Parameters:
      exceptionSupplier - the supplier of the exception to be thrown
      Returns:
      the value
      Throws:
      X - if the value is null
    • getOrNull

      public @Nullable T getOrNull()
      Returns the lazily initialized value. If the value has not yet been initialized, it will be computed using the provided initializer in a thread-safe manner.
      Returns:
      the initialized value
    • peek

      public final T peek()
      Returns the current value without triggering initialisation.

      Useful for logging or debugging without forcing a side effect.

      Returns:
      the current value, or null if not yet initialised
    • reset

      public final void reset()
      Resets the cached value.

      This allows the cache to be recomputed on the next call to get() or getOrNull().

    • isInitialised

      public final boolean isInitialised()
      Returns true if the value has already been initialised, or false otherwise.
      Returns:
      true if the value is initialised; false if it is still uninitialised