Class LazyFinal<T>

java.lang.Object
com.portingdeadmods.portingdeadlibs.utils.LazyFinal<T>
Type Parameters:
T - the type of the lazy value
All Implemented Interfaces:
Supplier<T>

public class LazyFinal<T> extends Object implements Supplier<T>
A thread-safe write-once lazy container that starts with null value and can be initialized exactly once.
  • Constructor Summary

    Constructors
    Constructor
    Description
    Creates a new LazyFinal instance initialized to null
  • Method Summary

    Modifier and Type
    Method
    Description
    static <T> LazyFinal<T>
    Factory method to create a new LazyFinal instance
    get()
    Gets the cached value.
    getOrDefault(T defaultValue)
    Gets the value if initialized, otherwise returns the default value.
    Gets the cached value, throwing an exception if not initialized.
    void
    Executes the given consumer with the cached value if initialized.
    void
    initialize(Supplier<T> supplier)
    Initializes the lazy value using a supplier exactly once.
    void
    initialize(T value)
    Initializes the lazy value exactly once.
    boolean
    Checks if this lazy container has been initialized.
     

    Methods inherited from class java.lang.Object

    clone, equals, finalize, getClass, hashCode, notify, notifyAll, wait, wait, wait
  • Constructor Details

    • LazyFinal

      public LazyFinal()
      Creates a new LazyFinal instance initialized to null
  • Method Details

    • create

      public static <T> LazyFinal<T> create()
      Factory method to create a new LazyFinal instance
    • initialize

      public void initialize(T value)
      Initializes the lazy value exactly once. Subsequent calls will throw IllegalStateException.
      Parameters:
      value - the value to initialize with (can be null if you want to explicitly set null)
      Throws:
      IllegalStateException - if already initialized
    • initialize

      public void initialize(Supplier<T> supplier)
      Initializes the lazy value using a supplier exactly once. Subsequent calls will throw IllegalStateException.
      Parameters:
      supplier - the supplier to get the value from
      Throws:
      IllegalStateException - if already initialized
      IllegalArgumentException - if supplier returns null
    • ifInitialized

      public void ifInitialized(Consumer<T> consumer)
      Executes the given consumer with the cached value if initialized. Does nothing if not yet initialized.
      Parameters:
      consumer - the consumer to execute with the cached value
    • get

      @Nullable public T get()
      Gets the cached value.
      Specified by:
      get in interface Supplier<T>
      Returns:
      the cached value, or null if not yet initialized
    • getOrThrow

      public T getOrThrow()
      Gets the cached value, throwing an exception if not initialized.
      Returns:
      the cached value
      Throws:
      IllegalStateException - if not yet initialized
    • isInitialized

      public boolean isInitialized()
      Checks if this lazy container has been initialized.
      Returns:
      true if initialized, false otherwise
    • getOrDefault

      public T getOrDefault(T defaultValue)
      Gets the value if initialized, otherwise returns the default value.
      Parameters:
      defaultValue - the value to return if not initialized
      Returns:
      the cached value if initialized, otherwise the default value
    • toString

      public String toString()
      Overrides:
      toString in class Object