Class LazyReference<T>

java.lang.Object
net.xun.armory.impl.util.LazyReference<T>
Type Parameters:
T - the type of object supplied by this reference
All Implemented Interfaces:
Supplier<T>

@Internal public class LazyReference<T> extends Object implements Supplier<T>
Internal utility class for a late‑bound reference to a Supplier.

Note: This class is part of the internal API and is not intended for use by external mods. It may change or be removed without notice in future versions.

A LazyReference holds a name and a volatile reference to a Supplier<T>. The supplier can be set once via bind(Supplier). Every call to get() invokes the supplier's Supplier.get() method.

This pattern is useful for breaking circular dependencies during initialization: an object can be declared early and its actual provider bound later, after the provider itself has been fully constructed.

Important: This class does not memoize the result of the supplier. If a singleton behavior is desired, the bound supplier itself must implement caching (e.g. by returning a pre‑initialized instance or using its own lazy initialization).

This is an internal implementation detail of the Armory API. Use the public API classes (ToolSet, ArmorSet) instead of interacting with this class directly.

Since:
1.0.0
  • Constructor Summary

    Constructors
    Constructor
    Description
    Constructs a new LazyReference with a descriptive name.
  • Method Summary

    Modifier and Type
    Method
    Description
    void
    bind(Supplier<T> delegate)
    Binds this reference to a concrete supplier.
    get()
    Returns the value obtained from the bound supplier.
    Returns the name associated with this lazy reference.
    boolean
    Checks whether this reference has been bound to a supplier.

    Methods inherited from class java.lang.Object

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

    • LazyReference

      public LazyReference(String name)
      Constructs a new LazyReference with a descriptive name.

      The name is typically used for debugging, logging, or generating registry keys. The supplier must be set later using bind(Supplier) before any call to get().

      Parameters:
      name - a descriptive name for the reference (cannot be null)
      Throws:
      NullPointerException - if name is null
  • Method Details

    • getName

      public String getName()
      Returns the name associated with this lazy reference.
      Returns:
      the name (never null)
    • bind

      public void bind(Supplier<T> delegate)
      Binds this reference to a concrete supplier.

      After binding, every call to get() will delegate to this supplier. The supplier may be set only once; subsequent calls to this method are not prevented but may lead to unpredictable behavior across threads.

      Parameters:
      delegate - the supplier that will provide the value (cannot be null)
      Throws:
      NullPointerException - if delegate is null
    • isBound

      public boolean isBound()
      Checks whether this reference has been bound to a supplier.
      Returns:
      true if bind(Supplier) has been called at least once, false otherwise
    • get

      public T get()
      Returns the value obtained from the bound supplier.

      This method must only be called after bind(Supplier) has been invoked. Each call invokes the supplier's get() method – no result is cached by this class. The method is thread‑safe in the sense that the volatile read of the delegate is safe, but the supplier itself must be thread‑safe if called concurrently.

      Specified by:
      get in interface Supplier<T>
      Returns:
      the value provided by the bound supplier
      Throws:
      IllegalStateException - if this reference has not yet been bound to a supplier