- java.lang.Object
-
- io.github.suppierk.utils.Memoized<T>
-
- Type Parameters:
T- is the type of the value
- All Implemented Interfaces:
jakarta.inject.Provider<T>,java.util.function.Supplier<T>
public final class Memoized<T> extends java.lang.Object implements jakarta.inject.Provider<T>, java.util.function.Supplier<T>Provides simple implementation of the lazily computed value which gets evaluated once.Can be used as both
ProviderandSupplierdue to their contract equality making this class useful for multiple scenarios.NOTE: due to the lazy nature of this value class it does not provide
Object.equals(Object)andObject.hashCode()implementations and MUST NOT be used inSetor as a key inMap.- See Also:
- Memoization on Wikipedia
-
-
Method Summary
All Methods Static Methods Instance Methods Concrete Methods Modifier and Type Method Description Tget()Double-checked locking withLockinstead ofsynchronizedto facilitate its usage on newer JVM versions affected by thread pinning problem.voidifPresent(java.util.function.Consumer<T> action)If a value is evaluated, performs the given action with the value, otherwise does nothing.voidifPresentOrElse(java.util.function.Consumer<? super T> action, java.lang.Runnable emptyAction)If a value is evaluated, performs the given action with the value, otherwise performs the given empty-based action.booleanisEmpty()If a value is not evaluated, returnstrue, otherwisefalse.booleanisPresent()If a value is evaluated, returnstrue, otherwisefalse.static <T> Memoized<T>memoizedProvider(jakarta.inject.Provider<T> provider)Static factory method forMemoizedclass.static <T> Memoized<T>memoizedSupplier(java.util.function.Supplier<T> supplier)Static factory method forMemoizedclass.java.util.stream.Stream<T>stream()If a value is evaluated, returns a sequentialStreamcontaining only that value, otherwise returns an emptyStream.java.lang.StringtoString()
-
-
-
Method Detail
-
memoizedProvider
public static <T> Memoized<T> memoizedProvider(jakarta.inject.Provider<T> provider)
Static factory method forMemoizedclass.- Type Parameters:
T- is the type of the value- Parameters:
provider- to invoke to compute the value- Returns:
- new
Memoizedinstance
-
memoizedSupplier
public static <T> Memoized<T> memoizedSupplier(java.util.function.Supplier<T> supplier)
Static factory method forMemoizedclass.- Type Parameters:
T- is the type of the value- Parameters:
supplier- to invoke to compute the value- Returns:
- new
Memoizedinstance - Throws:
java.lang.IllegalArgumentException- ifSupplierisnull
-
get
public T get()
Double-checked locking withLockinstead ofsynchronizedto facilitate its usage on newer JVM versions affected by thread pinning problem.Despite
AtomicReference, we use explicit locking to solve the problem of avoiding excessiveProvidercalls which would have occurred if we would useAtomicReference.compareAndSet(Object, Object).- Specified by:
getin interfacejakarta.inject.Provider<T>- Specified by:
getin interfacejava.util.function.Supplier<T>- Returns:
- computed value
- Throws:
java.lang.IllegalArgumentException- ifProviderreturnsnull- See Also:
- JEP to fix thread pinning problem
-
isEmpty
public boolean isEmpty()
If a value is not evaluated, returnstrue, otherwisefalse.- Returns:
trueif a value is not evaluated, otherwisefalse
-
isPresent
public boolean isPresent()
If a value is evaluated, returnstrue, otherwisefalse.- Returns:
trueif a value is evaluated, otherwisefalse
-
ifPresent
public void ifPresent(java.util.function.Consumer<T> action)
If a value is evaluated, performs the given action with the value, otherwise does nothing.- Parameters:
action- the action to be performed if a value is evaluated- Throws:
java.lang.NullPointerException- if value is evaluated and the given action isnull
-
ifPresentOrElse
public void ifPresentOrElse(java.util.function.Consumer<? super T> action, java.lang.Runnable emptyAction)
If a value is evaluated, performs the given action with the value, otherwise performs the given empty-based action.- Parameters:
action- the action to be performed if a value is evaluatedemptyAction- the empty-based action to be performed if no value is evaluated- Throws:
java.lang.NullPointerException- if a value is evaluated and the given action isnull, or no value is evaluated and the given empty-based action isnull.
-
stream
public java.util.stream.Stream<T> stream()
If a value is evaluated, returns a sequentialStreamcontaining only that value, otherwise returns an emptyStream.- Returns:
- the memoized value as a
Stream
-
toString
public java.lang.String toString()
- Overrides:
toStringin classjava.lang.Object
-
-