Class ConcurrentLazyFunction<T,R>

java.lang.Object
org.litebridge.commons.type.ConcurrentLazyFunction<T,R>
Type Parameters:
T - the type of the input required to compute the value
R - the type of the lazily initialised value

public final class ConcurrentLazyFunction<T,R> extends Object
A thread-safe, lazily initialized value holder for values of type R that require an input of type T to compute the value.

Allows resetting the cached value and ensures initialisation is performed only once in a concurrent environment.

  • 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
    Constructs a new ConcurrentLazyFunction with the given initialiser.
  • Method Summary

    Modifier and Type
    Method
    Description
    get(T input)
    The primary accessor.
    @Nullable R
    getOrNull(T input)
    Returns the lazily initialised value.
    getOrThrow(T input)
    Returns the value, or throws a NoSuchElementException if the value is null.
    <X extends Exception>
    R
    getOrThrow(T input, 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 R
    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

    • ConcurrentLazyFunction

      public ConcurrentLazyFunction(Function<T,R> initialiser)
      Constructs a new ConcurrentLazyFunction with the given initialiser.
      Parameters:
      initialiser - a Function that computes the value from the given input when it is first needed
  • Method Details

    • get

      public Optional<R> get(T input)
      The primary accessor. Returns the value wrapped in an Optional.
      Parameters:
      input - the input required to compute the value
      Returns:
      the value wrapped in an Optional
    • getOrThrow

      public R getOrThrow(T input)
      Returns the value, or throws a NoSuchElementException if the value is null.
      Parameters:
      input - the input required to compute the value
      Returns:
      the value
      Throws:
      NoSuchElementException - if the value is null
    • getOrThrow

      public <X extends Exception> R getOrThrow(T input, 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:
      input - the input required to compute the value
      exceptionSupplier - the supplier of the exception to be thrown
      Returns:
      the value
      Throws:
      X - if the value is null
    • getOrNull

      public @Nullable R getOrNull(T input)
      Returns the lazily initialised value. If the value has not yet been initialised, it will be computed using the provided initialiser in a thread-safe manner.
      Parameters:
      input - the input required to compute the value
      Returns:
      the initialised value
    • peek

      public final R 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