Class LazyReference<T>
- Type Parameters:
T- the type of object supplied by this reference
- All Implemented Interfaces:
Supplier<T>
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
ConstructorsConstructorDescriptionLazyReference(String name) Constructs a newLazyReferencewith a descriptive name. -
Method Summary
-
Constructor Details
-
LazyReference
Constructs a newLazyReferencewith 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 toget().- Parameters:
name- a descriptive name for the reference (cannot be null)- Throws:
NullPointerException- ifnameis null
-
-
Method Details
-
getName
Returns the name associated with this lazy reference.- Returns:
- the name (never null)
-
bind
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- ifdelegateis null
-
isBound
public boolean isBound()Checks whether this reference has been bound to a supplier.- Returns:
trueifbind(Supplier)has been called at least once,falseotherwise
-
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'sget()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:
getin interfaceSupplier<T>- Returns:
- the value provided by the bound supplier
- Throws:
IllegalStateException- if this reference has not yet been bound to a supplier
-