Package org.springframework.data.util
Class Lazy<T>
java.lang.Object
org.springframework.data.util.Lazy<T>
- All Implemented Interfaces:
Supplier<T>
Simple value type to delay the creation of an object using a
Supplier
returning the produced object for
subsequent lookups.
Note, that no concurrency control is applied during the lookup of get()
, which means in concurrent access
scenarios, the provided Supplier
can be called multiple times.
- Since:
- 2.0
- Author:
- Oliver Gierke, Mark Paluch, Henning Rohlfs, Johannes Englmeier, Greg Turnquist
-
Method Summary
Modifier and TypeMethodDescriptionstatic <T> Lazy<T>
empty()
Creates a pre-resolved emptyLazy
.boolean
<S> Lazy<S>
Creates a newLazy
with the givenFunction
lazily applied to the current one.get()
Returns the value created by the configuredSupplier
.@Nullable T
Returns the value of the lazy evaluation.int
hashCode()
<S> Lazy<S>
Creates a newLazy
with the givenFunction
lazily applied to the current one.static <T> Lazy<T>
Creates a newLazy
to produce an object lazily.static <T> Lazy<T>
of
(T value) Creates a newLazy
to return the given value.Returns a newLazy
that will consume the given supplier in case the current one does not yield in a result.Returns a newLazy
that will return the given value in case the current one does not yield in a result.@Nullable T
Returns the value of the lazy computation or the given default value in case the computation yields null.@Nullable T
Returns the value of the lazy computation or the value produced by the givenSupplier
in case the original value is null.toString()
-
Method Details
-
of
Creates a newLazy
to produce an object lazily. -
of
Creates a newLazy
to return the given value.- Type Parameters:
T
- the type of the value to return eventually.- Parameters:
value
- the value to return.- Returns:
- a
Lazy
wrappingvalue
.
-
empty
Creates a pre-resolved emptyLazy
.- Returns:
- an empty
Lazy
. - Since:
- 2.1
-
get
Returns the value created by the configuredSupplier
. Will return the same instance for subsequent lookups.- Specified by:
get
in interfaceSupplier<T>
- Returns:
- the value created by the configured
Supplier
. Will return the same instance for subsequent lookups. - Throws:
IllegalStateException
- if the resolved value is null.
-
getNullable
Returns the value of the lazy evaluation.- Returns:
- the value of the lazy evaluation, can be null.
- Since:
- 2.2
-
getOptional
-
or
Returns a newLazy
that will return the given value in case the current one does not yield in a result.- Parameters:
other
- must not be null.- Returns:
- a new
Lazy
that will yield its value if present, otherwiseother
.
-
or
Returns a newLazy
that will consume the given supplier in case the current one does not yield in a result.- Parameters:
supplier
- the supplying function that produces a value to be returned, must not be null.- Returns:
- a new
Lazy
that will yield its value if present, otherwise the result produced by the supplying function.
-
orElse
Returns the value of the lazy computation or the given default value in case the computation yields null.- Parameters:
other
- the value to be returned, if no value is present. May be null.- Returns:
- the value, if present, otherwise
other
.
-
orElseGet
Returns the value of the lazy computation or the value produced by the givenSupplier
in case the original value is null.- Parameters:
supplier
- the supplying function that produces a value to be returned, must not be null.- Returns:
- the value, if present, otherwise the result produced by the supplying function.
-
map
Creates a newLazy
with the givenFunction
lazily applied to the current one.- Parameters:
function
- must not be null.- Returns:
- an
Lazy
describing the result of applying a mapping function to the value of thisLazy
or throwingIllegalStateException
if theLazy
is empty.
-
flatMap
Creates a newLazy
with the givenFunction
lazily applied to the current one.- Parameters:
function
- must not be null.- Returns:
- the result of applying an
Lazy
-bearing mapping function to the value of thisLazy
or aLazy
throwingIllegalStateException
if theLazy
is empty.
-
equals
-
hashCode
public int hashCode() -
toString
-
toString
Returns theString
representation of the already resolved value or the one provided through the givenSupplier
if the value has not been resolved yet.- Parameters:
fallback
- must not be null.- Returns:
- will never be null.
- Since:
- 3.0.1
-