T - Type of contained elementpublic interface MonoElement<T> extends Supplier<T>
Unary offers as a replacement for Optional.Optional to be as compatible and intuitive as possible but it departs
from its concepts adding own variants to simplify o complete use cases. Also makes explicit differences
when there are similar methods between Stream and Optional but they have
different semantics.MonoElement can contain an element, it can be used as Supplier.| Modifier and Type | Method and Description |
|---|---|
Optional<T> |
asOptional()
Returns a native Optional instance to be used with Nary unaware code
|
T |
get()
If only one value is present in this
Nary, returns the value,
otherwise throws NoSuchElementException, or MoreThanOneElementException. |
Unary<T> |
ifAbsent(Runnable runnable)
If the only value is absent invoke the given lambda, or else do nothing
This Nary as Stream is consumed.
|
Unary<T> |
ifPresent(Consumer<? super T> consumer)
If the only value is present, invoke the specified consumer with the value,
otherwise do nothing.
|
default boolean |
isAbsent()
Negation of isPresent().
|
boolean |
isPresent()
Return
true if there is only a value present, otherwise false. |
T |
orElse(T other)
Return the only value if present, otherwise return
other. |
T |
orElseGet(Supplier<? extends T> other)
Return the only value if present, otherwise invoke
other and return
the result of that invocation. |
<X extends Throwable> |
orElseThrow(Supplier<? extends X> exceptionSupplier)
Return the only contained value, if present, otherwise throw an exception created by the provided supplier.
|
Unary<T> |
orElseUse(Supplier<? extends T> mapper)
Returns a Nary element that is populated with the given supplier only if this Nary is empty.
This method allows defining a mapping function for nary that can be empty and keep chaining other mapping definitions after that. This stream is consumed and it's assumed to have at most 1 element. |
T get() throws NoSuchElementException, MoreThanOneElementException
Nary, returns the value,
otherwise throws NoSuchElementException, or MoreThanOneElementException.
This Nary as Stream is consumed to return the value so any attempt to use it as stream will fail after this method
callget in interface Supplier<T>NaryNoSuchElementException - if there is no value presentMoreThanOneElementException - if there are more than one valuesOptional.get()boolean isPresent()
throws MoreThanOneElementException
true if there is only a value present, otherwise false.
This Nary as Stream is consumed.true if there is a value present, otherwise falseMoreThanOneElementException - if there are more than oneOptional.isPresent()default boolean isAbsent()
throws MoreThanOneElementException
MoreThanOneElementException - If there's more than one value to get as OptionalUnary<T> ifPresent(Consumer<? super T> consumer) throws MoreThanOneElementException
consumer - block to be executed if a value is presentNullPointerException - if value is present and consumer is
nullMoreThanOneElementException - if there are more than oneOptional.ifPresent(Consumer)Unary<T> ifAbsent(Runnable runnable) throws MoreThanOneElementException
runnable - The code to execute if this optional is emptyMoreThanOneElementException - if there are more than oneT orElse(T other) throws MoreThanOneElementException
other.
This Nary as Stream is consumed.other - the value to be returned if there is no value present, may
be nullotherMoreThanOneElementException - if there are more than oneT orElseGet(Supplier<? extends T> other) throws MoreThanOneElementException
other and return
the result of that invocation.
This Nary as Stream is consumed.other - a Supplier whose result is returned if no value
is presentother.asUni().get()MoreThanOneElementException - if there are more than oneUnary<T> orElseUse(Supplier<? extends T> mapper) throws MoreThanOneElementException
mapper - The function that supplies a value if missingMoreThanOneElementException - If this instance has more than 1 element<X extends Throwable> T orElseThrow(Supplier<? extends X> exceptionSupplier) throws X extends Throwable, MoreThanOneElementException
X - Type of the exception to be thrownexceptionSupplier - The supplier which will return the exception to
be thrownX - if there is no value presentNullPointerException - if no value is present and
exceptionSupplier is nullMoreThanOneElementException - if there are more than oneX extends ThrowableOptional<T> asOptional() throws MoreThanOneElementException
MoreThanOneElementException - if there are more than one elements in this instanceCopyright © 2020. All rights reserved.