Interface Lazy<T>

Type Parameters:
T - type of wrapped value
All Superinterfaces:
Supplier<T>
All Known Implementing Classes:
Lazy.DoubleCheckedLazy, Lazy.LockingWeakLazy, Lazy.SimpleLazy, Lazy.SimpleWeakLazy, Lazy.ThreadLocalLazy

public interface Lazy<T> extends Supplier<T>
A wrapper for a value initialized once needed.
  • Nested Class Summary

    Nested Classes
    Modifier and Type
    Interface
    Description
    static final class 
    Thread-safe (using double-checked locking) lazy getting its value from the specified value supplier.
    static final class 
    Thread-safe (using read-write-lock) weak lazy getting its value from the specified value supplier.
    static final class 
    Non-thread-safe lazy getting its value from the specified value supplier.
    static final class 
    Non-thread-safe (using double-checked locking) weak lazy getting its value from the specified value supplier.
    static class 
    Lazy which stores its values thread-locally.
  • Method Summary

    Modifier and Type
    Method
    Description
    static <T> Lazy<T>
    create(@NonNull Supplier<? extends T> valueSupplier)
    Creates a new lazy creating its value using the given supplier.
    static <T> Lazy<T>
    createThreadLocal(@NonNull Supplier<? extends T> valueSupplier)
    Creates a new thread-local lazy creating its value using the given supplier.
    static <T> Lazy<T>
    createThreadSafe(@NonNull Supplier<? extends T> valueSupplier)
    Creates a new thread-safe lazy creating its value using the given supplier.
    static <T> Lazy<@NotNull T>
    createWeak(@NonNull Supplier<@NotNull ? extends T> valueSupplier)
    Creates a new weak lazy creating its value using the given supplier.
    static <T> Lazy<T>
    createWeakThreadSafe(@NonNull Supplier<? extends T> valueSupplier)
    Creates a new weak thread-safe lazy creating its value using the given supplier.
    get()
    Gets the wrapped value initializing it once requested.
    default @NotNull Optional<T>
    Gets the wrapped value wrapped in Optional if it is already initialized or an empty optional otherwise.
    @NotNull Result<T,Void>
    Gets the wrapped value wrapped in Result if it is already initialized or a Result.nullError() otherwise.
    Gets the wrapped value if it is already initialized or null otherwise.
    boolean
    Tests if the value of this lazy was initialized.
  • Method Details

    • get

      T get()
      Gets the wrapped value initializing it once requested.
      Specified by:
      get in interface Supplier<T>
      Returns:
      wrapped value
    • isInitialized

      boolean isInitialized()
      Tests if the value of this lazy was initialized.
      Returns:
      true if this lazy's value was initialize and false otherwise
    • getInitializedOrNull

      @Nullable T getInitializedOrNull()
      Gets the wrapped value if it is already initialized or null otherwise.
      Returns:
      wrapped value if it is already initialized or null otherwise
      See Also:
      API note
      both null value and uninitialized state will be represented by null
    • getAsOptional

      @NotNull default @NotNull Optional<T> getAsOptional()
      Gets the wrapped value wrapped in Optional if it is already initialized or an empty optional otherwise.
      Returns:
      wrapped value wrapped in Optional if it is already initialized or an empty optional otherwise
      See Also:
      API note
      both null value and uninitialized state will be represented by Optional.empty()
    • getAsResult

      @NotNull @NotNull Result<T,Void> getAsResult()
      Gets the wrapped value wrapped in Result if it is already initialized or a Result.nullError() otherwise.
      Returns:
      wrapped value wrapped in Result if it is already initialized or Result.nullError() otherwise
    • create

      static <T> Lazy<T> create(@NonNull @NonNull Supplier<? extends T> valueSupplier)
      Creates a new lazy creating its value using the given supplier.
      Type Parameters:
      T - type of value wrapped
      Parameters:
      valueSupplier - supplier of the value to be called once needed
      Returns:
      created lazy
      API note
      might be thread-unsafe
    • createThreadSafe

      static <T> Lazy<T> createThreadSafe(@NonNull @NonNull Supplier<? extends T> valueSupplier)
      Creates a new thread-safe lazy creating its value using the given supplier.
      Type Parameters:
      T - type of value wrapped
      Parameters:
      valueSupplier - supplier of the value to be called once needed
      Returns:
      created lazy
    • createWeak

      static <T> Lazy<@NotNull T> createWeak(@NonNull @NonNull Supplier<@NotNull ? extends T> valueSupplier)
      Creates a new weak lazy creating its value using the given supplier.
      Type Parameters:
      T - type of value wrapped
      Parameters:
      valueSupplier - supplier of the value to be called once needed
      Returns:
      created lazy
      API note
      might be thread-unsafe, weak lazy stores the value wrapped in weak reference and so it may be GCed and so the new one might be recomputed using the value supplier
    • createWeakThreadSafe

      static <T> Lazy<T> createWeakThreadSafe(@NonNull @NonNull Supplier<? extends T> valueSupplier)
      Creates a new weak thread-safe lazy creating its value using the given supplier.
      Type Parameters:
      T - type of value wrapped
      Parameters:
      valueSupplier - supplier of the value to be called once needed
      Returns:
      created lazy
      API note
      weak lazy stores the value wrapped in weak reference and so it may be GCed and so the new one might be recomputed using the value supplier
    • createThreadLocal

      static <T> Lazy<T> createThreadLocal(@NonNull @NonNull Supplier<? extends T> valueSupplier)
      Creates a new thread-local lazy creating its value using the given supplier.
      Type Parameters:
      T - type of value wrapped
      Parameters:
      valueSupplier - supplier of the value to be called once needed
      Returns:
      created lazy