Class CheckedValue<T,E extends Exception>
java.lang.Object
org.opendaylight.yangtools.concepts.Either<T,E>
org.opendaylight.yangtools.concepts.CheckedValue<T,E>
- Type Parameters:
T
- Value typeE
- Exception type
- All Implemented Interfaces:
Immutable
Utility holder similar to
Optional
, except the empty case contains an Exception, which should be
reported, for example via throwing it. It provides analogous methods such as isPresent()
,
ifPresent(Consumer)
, get()
, orElse(Object)
, orElseGet(Supplier)
,
orElseThrow(Function)
.- Author:
- Robert Varga
-
Constructor Summary
ModifierConstructorDescriptionprotected
CheckedValue
(E violation, @Nullable Void dummy) protected
CheckedValue
(T value) -
Method Summary
Modifier and TypeMethodDescriptionfinal boolean
completeFuture
(SettableFuture<T> future) Complete targetSettableFuture
either successfully or exceptionally based on the state of this object.final boolean
completeFuture
(CompletableFuture<T> future) Complete targetCompletableFuture
either successfully or exceptionally based on the state of this object.<U> CheckedValue
<U, E> flatMap
(Function<? super T, CheckedValue<U, E>> mapper) final T
get()
Return the contained value ifisPresent()
would return true, throwsIllegalStateException
otherwise.final E
Return the contained error string ifisPresent()
would return false, throwsIllegalStateException
otherwise.final void
If a value is present, invoke the specified consumer with the value, otherwise do nothing.final boolean
Return true if a value is present.<U> CheckedValue
<U, E> <X extends Exception>
CheckedValue<T, X> mapException
(Function<? super E, X> mapper) static <T,
E extends Exception>
CheckedValue<T, E> ofException
(E cause) Create a new instance containing anException
.static <T,
E extends Exception>
CheckedValue<T, E> ofValue
(T value) Create a new instance containing specified value.static <T,
U, E extends Exception>
CheckedValue<T, E> Convert a Variant into aCheckedValue
, converting the second value into an exception.final T
Return contained value if present, otherwise return supplied value.final T
Return contained value if present, otherwise return the value produced by a supplier.final T
Return contained value if present or throw the exception alternative.orElseThrow
(Function<E, X> exceptionMapper) Return contained value if present or throw the exception alternative mapped through provided mapper.final CompletableFuture
<T> Transform this object into an immediately-completedCompletableFuture
.final FluentFuture
<T> Transform this object into an immediately-completedFluentFuture
.
-
Constructor Details
-
CheckedValue
-
CheckedValue
-
-
Method Details
-
ofException
Create a new instance containing anException
.- Type Parameters:
T
- Value typeE
- Exception type- Parameters:
cause
- Throwable- Returns:
- A new instance
- Throws:
NullPointerException
- ifcause
is null
-
ofValue
Create a new instance containing specified value.- Type Parameters:
T
- Value typeE
- Exception type- Parameters:
value
- Value- Returns:
- A new instance
- Throws:
NullPointerException
- ifvalue
is null
-
ofVariant
public static <T,U, CheckedValue<T,E extends Exception> E> ofVariant(Either<T, U> variant, Function<U, E> mapper) Convert a Variant into aCheckedValue
, converting the second value into an exception.- Type Parameters:
T
- First alternative typeU
- Second alternative typeE
- Exception type- Parameters:
variant
- Input variantmapper
- Mapping function from second alternative to an exception- Returns:
- Resulting
CheckedValue
-
get
Return the contained value ifisPresent()
would return true, throwsIllegalStateException
otherwise.- Returns:
- Contained value
- Throws:
IllegalStateException
- if an error string is present.
-
getException
Return the contained error string ifisPresent()
would return false, throwsIllegalStateException
otherwise.- Returns:
- Throwable which was used to instantiate this object, or absent if it was instantiated using an error string.
- Throws:
IllegalStateException
- if a value is present.
-
isPresent
public final boolean isPresent()Return true if a value is present.- Returns:
- True if a value is present.
-
ifPresent
If a value is present, invoke the specified consumer with the value, otherwise do nothing.- Parameters:
consumer
- block to be executed if a value is present- Throws:
NullPointerException
- if value is present andconsumer
is null
-
map
-
mapException
-
flatMap
-
orElse
Return contained value if present, otherwise return supplied value.- Parameters:
other
- Replacement value- Returns:
- Contained value or {code other}
-
orElseGet
Return contained value if present, otherwise return the value produced by a supplier.- Parameters:
supplier
- Replacement value supplier- Returns:
- Contained value or supplier's value
- Throws:
NullPointerException
- ifsupplier
is null
-
orElseThrow
Return contained value if present or throw the exception alternative.- Returns:
- Contained value
- Throws:
E
- When there is no contained value
-
orElseThrow
Return contained value if present or throw the exception alternative mapped through provided mapper.- Type Parameters:
X
- Thrown exception type- Parameters:
exceptionMapper
- Exception mapper- Returns:
- Contained value
- Throws:
NullPointerException
- ifexceptionMapper
is nullX
- When there is no contained value
-
completeFuture
Complete targetCompletableFuture
either successfully or exceptionally based on the state of this object.- Parameters:
future
- Future to complete- Returns:
- True if this call has transitioned the future to a completed state, false otherwise.
- Throws:
NullPointerException
- if {code future} is null
-
completeFuture
Complete targetSettableFuture
either successfully or exceptionally based on the state of this object.- Parameters:
future
- Future to complete- Returns:
- True if this call has transitioned the future to a completed state, false otherwise.
- Throws:
NullPointerException
- if {code future} is null
-
toCompletableFuture
Transform this object into an immediately-completedCompletableFuture
. The future will be successful if this object has a contained value or unsuccessful if this objects contains an exception.- Returns:
- A
CompletableFuture
.
-
toFluentFuture
Transform this object into an immediately-completedFluentFuture
. The future will be successful if this object has a contained value or unsuccessful if this objects contains an exception.- Returns:
- A
FluentFuture
.
-