T
- Value typeE
- Exception type@Beta @NonNullByDefault public class CheckedValue<T,E extends Exception> extends Variant<T,E>
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)
.Modifier | Constructor and Description |
---|---|
protected |
CheckedValue(E violation,
@Nullable Void dummy) |
protected |
CheckedValue(T value) |
Modifier and Type | Method and Description |
---|---|
boolean |
completeFuture(CompletableFuture<T> future)
Complete target
CompletableFuture either successfully or exceptionally based on the state of this object. |
boolean |
completeFuture(SettableFuture<T> future)
Complete target
SettableFuture 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 if
isPresent() would return true, throws IllegalStateException
otherwise. |
E |
getException()
Return the contained error string if
isPresent() would return false, throws
IllegalStateException 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> |
mapException(Function<? super E,X> mapper) |
static <T,E extends Exception> |
ofException(E cause)
Create a new instance containing an
Exception . |
static <T,E extends Exception> |
ofValue(T value)
Create a new instance containing specified value.
|
static <T,U,E extends Exception> |
ofVariant(Variant<T,U> variant,
Function<U,E> mapper)
Convert a Variant into a
CheckedValue , 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> |
orElseThrow(Function<E,X> exceptionMapper)
Return contained value if present or throw the exception alternative mapped through provided mapper.
|
<X extends Throwable> |
orElseThrow(Supplier<X> supplier)
Return contained value if present or throw the exception supplied by supplier.
|
CompletableFuture<T> |
toCompletableFuture()
Transform this object into an immediately-completed
CompletableFuture . |
FluentFuture<T> |
toFluentFuture()
Transform this object into an immediately-completed
FluentFuture . |
protected CheckedValue(T value)
public static <T,E extends Exception> CheckedValue<T,E> ofException(E cause)
Exception
.T
- Value typeE
- Exception typecause
- ThrowableNullPointerException
- if cause
is nullpublic static <T,E extends Exception> CheckedValue<T,E> ofValue(T value)
T
- Value typeE
- Exception typevalue
- ValueNullPointerException
- if value
is nullpublic static <T,U,E extends Exception> CheckedValue<T,E> ofVariant(Variant<T,U> variant, Function<U,E> mapper)
CheckedValue
, converting the second value into an exception.T
- First alternative typeU
- Second alternative typeE
- Exception typevariant
- Input variantmapper
- Mapping function from second alternative to an exceptionCheckedValue
public final T get()
isPresent()
would return true, throws IllegalStateException
otherwise.IllegalStateException
- if an error string is present.public final E getException()
isPresent()
would return false, throws
IllegalStateException
otherwise.IllegalStateException
- if a value is present.public final boolean isPresent()
public final void ifPresent(Consumer<? super T> consumer)
consumer
- block to be executed if a value is presentNullPointerException
- if value is present and consumer
is nullpublic <U> CheckedValue<U,E> map(Function<? super T,U> mapper)
public <X extends Exception> CheckedValue<T,X> mapException(Function<? super E,X> mapper)
public <U> CheckedValue<U,E> flatMap(Function<? super T,CheckedValue<U,E>> mapper)
public final T orElse(T other)
other
- Replacement valuepublic final T orElseGet(Supplier<T> supplier)
supplier
- Replacement value supplierNullPointerException
- if supplier
is nullpublic final T orElseThrow() throws E extends Exception
public final <X extends Throwable> T orElseThrow(Function<E,X> exceptionMapper) throws X extends Throwable
X
- Thrown exception typeexceptionMapper
- Exception mapperNullPointerException
- if exceptionMapper
is nullX
- When there is no contained valueX extends Throwable
public final <X extends Throwable> T orElseThrow(Supplier<X> supplier) throws X extends Throwable
X
- Thrown exception typesupplier
- Exception supplierNullPointerException
- if exceptionMapper
is nullX
- When there is no contained valueX extends Throwable
public final boolean completeFuture(CompletableFuture<T> future)
CompletableFuture
either successfully or exceptionally based on the state of this object.future
- Future to completeNullPointerException
- if {code future} is nullpublic final boolean completeFuture(SettableFuture<T> future)
SettableFuture
either successfully or exceptionally based on the state of this object.future
- Future to completeNullPointerException
- if {code future} is nullpublic final CompletableFuture<T> toCompletableFuture()
CompletableFuture
. The future will be successful
if this object has a contained value or unsuccessful if this objects contains an exception.CompletableFuture
.public final FluentFuture<T> toFluentFuture()
FluentFuture
. The future will be successful
if this object has a contained value or unsuccessful if this objects contains an exception.FluentFuture
.Copyright © 2020 OpenDaylight. All rights reserved.