Package de.team33.patterns.lazy.narvi
Class ReLazy<T>
java.lang.Object
de.team33.patterns.lazy.narvi.ReLazy<T>
- All Implemented Interfaces:
Supplier<T>
Implements a
Supplier that provides a virtually fixed value(1).
That value is only actually determined when it is accessed for the first time(2).
This implementation ensures that the originally defined initialization code is called at most once(1), even if there is concurrent access from multiple threads, unless the initialization attempt causes an (unchecked) exception.
Once the value is established, unnecessary effort to synchronize competing(3) read accesses is avoided.
(1) until reset().
(2) after initialization or after latest reset().
(3) Pure read accesses are of course not really competing.
-
Method Summary
Modifier and TypeMethodDescriptionfinal Tget()Executes the originally defined initialization code once on the first call and returns its result on that and every subsequent call without executing the initialization code again.static <T> ReLazy<T>Returns a new instance giving aSupplierthat defines the intended initialization of the represented value.static <T> ReLazy<T>Returns a new instance giving anXSupplierthat defines the intended initialization of the represented value.reset()Resets this to the initial state and returns this.
-
Method Details
-
init
Returns a new instance giving aSupplierthat defines the intended initialization of the represented value.- Type Parameters:
T- The result type of the initialisation code.- See Also:
-
initEx
Returns a new instance giving anXSupplierthat defines the intended initialization of the represented value. The initialization code may throw a checked exception. If so, it is caught, wrapped in anInitException, and rethrown.- Type Parameters:
T- The result type of the initialisation code.- See Also:
-
get
Executes the originally defined initialization code once on the first call and returns its result on that and every subsequent call without executing the initialization code again. This method is thread safe. -
reset
Resets this to the initial state and returns this.
-