Interface ValueContainer<T>
- Type Parameters:
T- type of stored value
- All Superinterfaces:
Supplier<T>
- All Known Implementing Classes:
ValueContainer.Containing,ValueContainer.Empty,ValueContainer.OfNull,ValueContainer.Supplying
A value which may be present (which includes
null) or not-present.
This differs from Optional as presence of a null value
does not make this value container empty.
-
Nested Class Summary
Nested ClassesModifier and TypeInterfaceDescriptionstatic final classA simple value-container which is meant to keep one unchanged value.static final classEmpty value-container.static classAn exception thrown wheneverget()is called on an empty value container.static final classNon-empty value-container containingnull.static final classValue-container which provides new values for each request. -
Method Summary
Modifier and TypeMethodDescriptionasResult()Converts this value container into anull-error result.Converts this value container into aResult.static <T> @NotNull ValueContainer<T>empty()Gets a value container being empty.get()Gets the value stored in this value container.booleanChecks whether the object is present ot not.static <T> @NotNull ValueContainer<T>nonnullOrEmpty(T value) Gets a value container containing the specified value or an empty one if the values isnull.static <T> @NotNull ValueContainer<T>of(T value) Gets a non-empty value container containing the specified value.static <T> @NotNull ValueContainer<T>ofNull()Gets a non-empty value container containingnull.Gets the value if it is present otherwise using the specified one.Gets the value if it is present otherwise using the one got from the specified supplier.orElseSneakyThrow(@NonNull Supplier<X> exceptionSupplier) Gets the value if it is present otherwise throwing an exception.orElseThrow(@NonNull Supplier<X> exceptionSupplier) Gets the value if it is present otherwise throwing an exception.
-
Method Details
-
empty
Gets a value container being empty.- Type Parameters:
T- type of stored value- Returns:
- empty value container
-
ofNull
Gets a non-empty value container containingnull.- Type Parameters:
T- type of stored value- Returns:
- value container containing
null
-
of
Gets a non-empty value container containing the specified value.- Type Parameters:
T- type of stored value- Parameters:
value- nullable value stored in the container- Returns:
- non-empty value container containing the specified value
-
nonnullOrEmpty
Gets a value container containing the specified value or an empty one if the values isnull.- Type Parameters:
T- type of stored value- Parameters:
value- value stored in the container- Returns:
- non-empty value container containing the specified value
if it is not
nulland an empty one otherwise
-
get
T get()Gets the value stored in this value container.- Specified by:
getin interfaceSupplier<T>- Returns:
- stored value if it is
present - Throws:
ValueContainer.EmptyValueException- if this value container is empty
-
isPresent
boolean isPresent()Checks whether the object is present ot not.- Returns:
trueif the value is present (might benull) andfalseotherwise
-
orElse
Gets the value if it is present otherwise using the specified one.- Parameters:
value- value to use if this value container is empty- Returns:
- this value container's value if it is not empty or the specified value otherwise
-
orElseGet
Gets the value if it is present otherwise using the one got from the specified supplier.- Parameters:
defaultValueSupplier- supplier to be used for getting the value if this value container is empty- Returns:
- this value container's value if it is not empty or the one got from the supplier otherwise
-
orElseThrow
@Nullable <X extends Throwable> T orElseThrow(@NonNull @NonNull Supplier<X> exceptionSupplier) throws X Gets the value if it is present otherwise throwing an exception.- Type Parameters:
X- type of an exception thrown if this value container is empty- Parameters:
exceptionSupplier- supplier to be used for getting the exception if this value container is empty- Returns:
- this value container's value if it is not empty
- Throws:
X- if this value container is empty
-
orElseSneakyThrow
@Nullable default <X extends Throwable> T orElseSneakyThrow(@NonNull @NonNull Supplier<X> exceptionSupplier) Gets the value if it is present otherwise throwing an exception. ThrowsXif this value container is empty.- Type Parameters:
X- type of an exception thrown if this value container is empty- Parameters:
exceptionSupplier- supplier to be used for getting the exception if this value container is empty- Returns:
- this value container's value if it is not empty
-
asResult
Converts this value container into anull-error result.- Type Parameters:
E- type of the result error- Returns:
successful resultcontaining the value contained by this value container if is ispresentor anull-error resultotherwise- See Also:
-
asResult
Converts this value container into aResult.- Type Parameters:
E- type of the result error- Parameters:
errorSupplier- supplier of the error in case of this value container being empty- Returns:
successful resultcontaining the value contained by this value container if is ispresentor anerror resultwith the error got by using the specified supplier- See Also:
-