Class CheckedValue<T,E extends Exception>
- java.lang.Object
-
- org.opendaylight.yangtools.concepts.Variant<T,E>
-
- org.opendaylight.yangtools.concepts.CheckedValue<T,E>
-
- Type Parameters:
T
- Value typeE
- Exception type
- All Implemented Interfaces:
Immutable
,MutationBehaviour<Immutable>
@Beta @NonNullByDefault public class CheckedValue<T,E extends Exception> extends Variant<T,E>
Utility holder similar toOptional
, except the empty case contains an Exception, which should be reported, for example via throwing it. It provides analogous methods such asisPresent()
,ifPresent(Consumer)
,get()
,orElse(Object)
,orElseGet(Supplier)
,orElseThrow(Function)
.- Author:
- Robert Varga
-
-
Constructor Summary
Constructors Modifier Constructor Description protected
CheckedValue(E violation, @Nullable Void dummy)
protected
CheckedValue(T value)
-
Method Summary
All Methods Static Methods Instance Methods Concrete Methods Modifier and Type Method Description boolean
completeFuture(SettableFuture<T> future)
Complete targetSettableFuture
either successfully or exceptionally based on the state of this object.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)
T
get()
Return the contained value ifisPresent()
would return true, throwsIllegalStateException
otherwise.E
getException()
Return the contained error string ifisPresent()
would return false, throwsIllegalStateException
otherwise.void
ifPresent(Consumer<? super T> consumer)
If a value is present, invoke the specified consumer with the value, otherwise do nothing.boolean
isPresent()
Return true if a value is present.<U> CheckedValue<U,E>
map(Function<? super T,U> mapper)
<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>ofVariant(Variant<T,U> variant, Function<U,E> mapper)
Convert a Variant into aCheckedValue
, converting the second value into an exception.T
orElse(T other)
Return contained value if present, otherwise return supplied value.T
orElseGet(Supplier<T> supplier)
Return contained value if present, otherwise return the value produced by a supplier.T
orElseThrow()
Return contained value if present or throw the exception alternative.<X extends Throwable>
TorElseThrow(Function<E,X> exceptionMapper)
Return contained value if present or throw the exception alternative mapped through provided mapper.<X extends Throwable>
TorElseThrow(Supplier<X> supplier)
Return contained value if present or throw the exception supplied by supplier.CompletableFuture<T>
toCompletableFuture()
Transform this object into an immediately-completedCompletableFuture
.FluentFuture<T>
toFluentFuture()
Transform this object into an immediately-completedFluentFuture
.
-
-
-
Method Detail
-
ofException
public static <T,E extends Exception> CheckedValue<T,E> ofException(E cause)
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
public static <T,E extends Exception> CheckedValue<T,E> ofValue(T value)
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,E extends Exception> CheckedValue<T,E> ofVariant(Variant<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
public final T get()
Return the contained value ifisPresent()
would return true, throwsIllegalStateException
otherwise.- Returns:
- Contained value
- Throws:
IllegalStateException
- if an error string is present.
-
getException
public final E 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
public final void ifPresent(Consumer<? super T> consumer)
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
public <U> CheckedValue<U,E> map(Function<? super T,U> mapper)
-
mapException
public <X extends Exception> CheckedValue<T,X> mapException(Function<? super E,X> mapper)
-
flatMap
public <U> CheckedValue<U,E> flatMap(Function<? super T,CheckedValue<U,E>> mapper)
-
orElse
public final T orElse(T other)
Return contained value if present, otherwise return supplied value.- Parameters:
other
- Replacement value- Returns:
- Contained value or {code other}
-
orElseGet
public final T orElseGet(Supplier<T> supplier)
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
public final T orElseThrow() throws E extends Exception
Return contained value if present or throw the exception alternative.
-
orElseThrow
public final <X extends Throwable> T orElseThrow(Function<E,X> exceptionMapper) throws X extends Throwable
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 valueX extends Throwable
-
orElseThrow
public final <X extends Throwable> T orElseThrow(Supplier<X> supplier) throws X extends Throwable
Return contained value if present or throw the exception supplied by supplier.- Type Parameters:
X
- Thrown exception type- Parameters:
supplier
- Exception supplier- Returns:
- Contained value
- Throws:
NullPointerException
- ifexceptionMapper
is nullX
- When there is no contained valueX extends Throwable
-
completeFuture
public final boolean completeFuture(CompletableFuture<T> future)
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
public final boolean completeFuture(SettableFuture<T> future)
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
public final CompletableFuture<T> 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
public final FluentFuture<T> 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
.
-
-