- Type Parameters:
T- The type of the value for this instance ofLazy.
- All Implemented Interfaces:
Lazy<T>
The implementation of the interface
Lazy.
An instance of this class holds a value that will be initialised by a
call to the supplier (provided with the constructor
LazyImpl(Supplier))
on a first call to
get().
For special purposes, the constructor
LazyImpl(Object)
creates an already initialised instance of Lazy.
Use
isPresent()
to avoid unnecessary initialisation.
As a lazy initialisation makes the value unpredictable, it is necessary
that the implementations of
equals(Object)
and
hashCode()
force the initialisation.
toString() do not force the initialisation.
- Author:
- Thomas Thrien (thomas.thrien@tquadrat.org)
- Version:
- $Id: LazyImpl.java 1060 2023-09-24 19:21:40Z tquadrat $
- Since:
- 0.0.5
- UML Diagram
-
UML Diagram for "org.tquadrat.foundation.lang.internal.LazyImpl"
-
Field Summary
Fields -
Constructor Summary
Constructors -
Method Summary
Modifier and TypeMethodDescriptionfinal booleanfinal Tget()Returns the value for this instance ofLazy.final inthashCode()final booleanChecks whether thisLazyinstance has been initialised already.orElseThrow(Supplier<? extends X> exceptionSupplier) Returns the value or throws the exception that is created by the givenSupplierwhen not yet initialised.final StringtoString()Methods inherited from class java.lang.Object
clone, finalize, getClass, notify, notifyAll, wait, wait, waitMethods inherited from interface org.tquadrat.foundation.lang.Lazy
getAsString, getAsString, ifPresent, map
-
Field Details
-
m_Lock
-
m_Supplier
The supplier for the value of thisLazyinstance. It will be set tonullafterm_Valueis initialised. -
m_Value
The value of thisLazyinstance; it isnullif it was not yet initialised.
-
-
Constructor Details
-
LazyImpl
Creates a newLazyinstance.- Parameters:
supplier- The supplier that initialises the value for this instance on the first call toget().
-
LazyImpl
Creates a new
Lazyinstance that is already initialised.This allows to use
Lazyinstances also for cloneable objects, given thatTis either cloneable itself or immutable.- Parameters:
value- The value; can benull.- See Also:
-
-
Method Details
-
equals
-
get
Returns the value for this instance ofLazy. -
hashCode
-
isPresent
Checks whether this
Lazyinstance has been initialised already. But even it was initialised,Lazy.get()may still returnnull. -
orElseThrow
Returns the value or throws the exception that is created by the givenSupplierwhen not yet initialised.- Specified by:
orElseThrowin interfaceLazy<T>- Type Parameters:
X- The type of the implementation ofThrowable.- Parameters:
exceptionSupplier- The supplier for the exception to throw when the instance was not yet initialised.- Returns:
- The value.
- Throws:
X- When not initialised, the exception created by the given supplier will be thrown.
-
toString
-
