Class LazyReference<T>

java.lang.Object
de.marcely.bedwars.tools.LazyReference<T>
Type Parameters:
T - The type of the reference
All Implemented Interfaces:
Supplier<T>

public class LazyReference<T> extends Object implements Supplier<T>
Represents a lazy reference that will only be initialized when it's being accessed.

After the initial access, the reference will be cached and returned on every subsequent access.

  • Constructor Details

    • LazyReference

      public LazyReference(Supplier<T> factory)
      Creates a new lazy reference.
      Parameters:
      factory - The factory that will be called when the reference is being accessed
  • Method Details

    • get

      public T get()
      Gets the reference.
      Specified by:
      get in interface Supplier<T>
      Returns:
      The reference
      Throws:
      IllegalStateException - If the factory returns null
    • clear

      public void clear()
      Clears the cached reference.
    • of

      public static <T> Supplier<T> of(Supplier<T> factory)
      Creates a new lazy reference.
      Type Parameters:
      T - The type of the reference
      Parameters:
      factory - The factory that will be called when the reference is being accessed
      Returns:
      The new lazy reference