Class XReLazy<T,X extends Exception>

java.lang.Object
de.team33.patterns.lazy.narvi.XReLazy<T,X>
All Implemented Interfaces:
XSupplier<T,X>

public final class XReLazy<T,X extends Exception> extends Object implements XSupplier<T,X>
Implements a kind of supplier that provides a virtually fixed value(1). That value is only actually determined when it is accessed for the first time(2).

This implementation ensures that the originally defined initialization code is called at most once(1), even if there is concurrent access from multiple threads, unless the initialization attempt causes an exception.

Once the value is established, unnecessary effort to synchronize competing(3) read accesses is avoided.

(1) until reset().
(2) after initialization or after latest reset().
(3) Pure read accesses are of course not really competing.

See Also:
  • Method Details

    • init

      public static <T, X extends Exception> XReLazy<T,X> init(XSupplier<? extends T,? extends X> initial)
      Returns a new instance giving a Supplier that defines the intended initialization of the represented value.
      Type Parameters:
      T - The result type of the initialisation code.
      X - The exception type that may occur within initialisation.
    • get

      public final T get() throws X

      This implementation executes the originally defined initialization code once on the first call(1) and returns its result on that and every subsequent call without executing the initialization code again(2).

      This implementation is thread safe.

      (1) after creation or after latest reset().
      (2) until next reset().

      Specified by:
      get in interface XSupplier<T,X extends Exception>
      Throws:
      X extends Exception
    • reset

      public final XReLazy<T,X> reset()
      Resets this to its initial state and returns this.