@Beta
public abstract class Any<T>
extends java.lang.Object
Modifier and Type | Method and Description |
---|---|
static <T> Any<T> |
empty()
Returns an empty
Any instance. |
abstract <E extends java.lang.Exception> |
filter(Try.Predicate<? super T,E> predicate)
If a value is present, and the value matches the given predicate,
return an
Any describing the value, otherwise return an
empty Any . |
abstract <E extends java.lang.Exception> |
filterIfNotNull(Try.Predicate<? super T,E> predicate)
If a value is not null, and the value matches the given predicate,
return an
Any describing the value, otherwise return an
empty Any . |
abstract <U,E extends java.lang.Exception> |
flatMap(Try.Function<? super T,? extends Any<U>,E> mapper)
If a value is present, apply the provided
Any -bearing
mapping function to it, return that result, otherwise return an empty
Any . |
abstract <U,E extends java.lang.Exception> |
flatMapIfNotNull(Try.Function<? super T,? extends Any<U>,E> mapper)
If a value is not null, apply the provided
Any -bearing
mapping function to it, return that result, otherwise return an empty
Any . |
T |
get()
If a value is present in this
Any , returns the value,
otherwise throws NoSuchElementException . |
<E extends java.lang.Exception> |
ifNotNull(Try.Consumer<? super T,E> consumer)
If a value is not null, invoke the specified consumer with the value,
otherwise do nothing.
|
<E extends java.lang.Exception,E2 extends java.lang.Exception> |
ifNotNullOrElse(Try.Consumer<? super T,E> action,
Try.Runnable<E2> emptyAction)
If a value is not null, performs the given action with the value, otherwise performs the given empty-based action.
|
<E extends java.lang.Exception> |
ifPresent(Try.Consumer<? super T,E> consumer)
If a value is present, invoke the specified consumer with the value,
otherwise do nothing.
|
<E extends java.lang.Exception,E2 extends java.lang.Exception> |
ifPresentOrElse(Try.Consumer<? super T,E> action,
Try.Runnable<E2> emptyAction)
If a value is present, performs the given action with the value, otherwise performs the given empty-based action.
|
boolean |
isNotNull()
Return
true if there is a value not null, otherwise false . |
boolean |
isPresent()
Return
true if there is a value present, otherwise false . |
abstract <U,E extends java.lang.Exception> |
map(Try.Function<? super T,? extends U,E> mapper)
If a value is present, apply the provided mapping function to it,
|
abstract <U,E extends java.lang.Exception> |
mapIfNotNull(Try.Function<? super T,? extends U,E> mapper)
If a value is not null, apply the provided mapping function to it,
|
static <T> Any<T> |
of(T value)
Returns an
Any with the specified present nullable value. |
abstract <E extends java.lang.Exception> |
or(Try.Supplier<? extends Any<T>,E> supplier)
If a value is present, returns an Optional describing the value, otherwise returns an Optional produced by the supplying function.
|
T |
orElse(T other)
Return the value if present, otherwise return
other . |
<E extends java.lang.Exception> |
orElseGet(Try.Supplier<? extends T,E> other)
Return the value if present, otherwise invoke
other and return the result of that invocation. |
<X extends java.lang.Throwable> |
orElseThrow(Supplier<? extends X> exceptionSupplier)
Return the contained value, if present, otherwise throw an exception to be created by the provided supplier.
|
<E extends java.lang.Exception> |
orGetIfNull(Try.Supplier<? extends T,E> other)
Return the value is not null, otherwise invoke
other and return the result of that invocation. |
T |
orIfNull(T other)
Return the value is not null, otherwise return
other . |
<X extends java.lang.Throwable> |
orThrowIfNull(Supplier<? extends X> exceptionSupplier)
Return the value is not null, otherwise throw an exception to be created by the provided supplier.
|
Stream<T> |
stream() |
Stream<T> |
streamIfNotNull() |
java.util.Optional<T> |
toJdkOptional() |
java.util.List<T> |
toList()
Returns a
List with the value if it presents, otherwise an empty List . |
java.util.List<T> |
toListIfNotNull()
Returns a
List with the value if the value is not null, otherwise an empty List . |
Optional<T> |
toOptional() |
java.util.Set<T> |
toSet()
Returns a
Set with the value if it presents, otherwise an empty Set . |
java.util.Set<T> |
toSetIfNotNull()
Returns a
Set with the value if the value is not null, otherwise an empty Set . |
public static <T> Any<T> empty()
Any
instance. No value is present for this
Optional.T
- Type of the non-existent valueAny
public static <T> Any<T> of(T value)
Any
with the specified present nullable value.value
- the value to be present, which could be nullAny
with the value presentpublic T get()
Any
, returns the value,
otherwise throws NoSuchElementException
.Any
java.util.NoSuchElementException
- if there is no value presentisPresent()
public boolean isPresent()
true
if there is a value present, otherwise false
.true
if there is a value present, otherwise false
public boolean isNotNull()
true
if there is a value not null, otherwise false
.true
if there is a value not null, otherwise false
public <E extends java.lang.Exception> void ifPresent(Try.Consumer<? super T,E> consumer) throws E extends java.lang.Exception
consumer
- block to be executed if a value is presentjava.lang.NullPointerException
- if value is present and consumer
is
nullE extends java.lang.Exception
public <E extends java.lang.Exception,E2 extends java.lang.Exception> void ifPresentOrElse(Try.Consumer<? super T,E> action, Try.Runnable<E2> emptyAction) throws E extends java.lang.Exception, E2 extends java.lang.Exception
action
- emptyAction
- E extends java.lang.Exception
public <E extends java.lang.Exception> void ifNotNull(Try.Consumer<? super T,E> consumer) throws E extends java.lang.Exception
consumer
- block to be executed if a value is not null.java.lang.NullPointerException
- if value is present and consumer
is
nullE extends java.lang.Exception
public <E extends java.lang.Exception,E2 extends java.lang.Exception> void ifNotNullOrElse(Try.Consumer<? super T,E> action, Try.Runnable<E2> emptyAction) throws E extends java.lang.Exception, E2 extends java.lang.Exception
action
- emptyAction
- E extends java.lang.Exception
public abstract <E extends java.lang.Exception> Any<T> filter(Try.Predicate<? super T,E> predicate) throws E extends java.lang.Exception
Any
describing the value, otherwise return an
empty Any
.predicate
- a predicate to apply to the value, if presentAny
describing the value of this Any
if a value is present and the value matches the given predicate,
otherwise an empty Any
java.lang.NullPointerException
- if the predicate is nullE extends java.lang.Exception
public abstract <U,E extends java.lang.Exception> Any<U> map(Try.Function<? super T,? extends U,E> mapper) throws E extends java.lang.Exception
U
- The type of the result of the mapping functionmapper
- a mapping function to apply to the value, if presentAny
describing the result of applying a mapping
function to the value of this Any
, if a value is present,
otherwise an empty Any
java.lang.NullPointerException
- if the mapping function is nullE extends java.lang.Exception
public abstract <U,E extends java.lang.Exception> Any<U> flatMap(Try.Function<? super T,? extends Any<U>,E> mapper) throws E extends java.lang.Exception
Any
-bearing
mapping function to it, return that result, otherwise return an empty
Any
. This method is similar to #map(Function)
,
but the provided mapper is one whose result is already an Any
,
and if invoked, flatMap
does not wrap it with an additional
Any
.U
- The type parameter to the Any
returned bymapper
- a mapping function to apply to the value, if present
the mapping functionAny
-bearing mapping
function to the value of this Any
, if a value is present,
otherwise an empty Any
java.lang.NullPointerException
- if the mapping function is null or returns
a null resultE extends java.lang.Exception
public abstract <E extends java.lang.Exception> Any<T> filterIfNotNull(Try.Predicate<? super T,E> predicate) throws E extends java.lang.Exception
Any
describing the value, otherwise return an
empty Any
.predicate
- a predicate to apply to the value, if presentAny
describing the value of this Any
if a value is present and the value matches the given predicate,
otherwise an empty Any
java.lang.NullPointerException
- if the predicate is nullE extends java.lang.Exception
public abstract <U,E extends java.lang.Exception> Any<U> mapIfNotNull(Try.Function<? super T,? extends U,E> mapper) throws E extends java.lang.Exception
U
- The type of the result of the mapping functionmapper
- a mapping function to apply to the value, if not nullAny
describing the result of applying a mapping
function to the value of this Any
, if a value is not null,
otherwise an empty Any
java.lang.NullPointerException
- if the mapping function is nullE extends java.lang.Exception
public abstract <U,E extends java.lang.Exception> Any<U> flatMapIfNotNull(Try.Function<? super T,? extends Any<U>,E> mapper) throws E extends java.lang.Exception
Any
-bearing
mapping function to it, return that result, otherwise return an empty
Any
. This method is similar to #map(Function)
,
but the provided mapper is one whose result is already an Any
,
and if invoked, flatMap
does not wrap it with an additional
Any
.U
- The type parameter to the Any
returned bymapper
- a mapping function to apply to the value, if not null
the mapping functionAny
-bearing mapping
function to the value of this Any
, if a value is not null,
otherwise an empty Any
java.lang.NullPointerException
- if the mapping function is null or returns
a null resultE extends java.lang.Exception
public abstract <E extends java.lang.Exception> Any<T> or(Try.Supplier<? extends Any<T>,E> supplier) throws E extends java.lang.Exception
supplier
- E
E extends java.lang.Exception
public T orElse(T other)
other
.other
- the value to be returned if there is no value present, may be nullother
public <E extends java.lang.Exception> T orElseGet(Try.Supplier<? extends T,E> other) throws E extends java.lang.Exception
other
and return the result of that invocation.other
- a Supplier
whose result is returned if no value is presentother.get()
java.lang.NullPointerException
- if value is not present and other
is
nullE extends java.lang.Exception
public <X extends java.lang.Throwable> T orElseThrow(Supplier<? extends X> exceptionSupplier) throws X extends java.lang.Throwable
X
- Type of the exception to be thrownexceptionSupplier
- The supplier which will return the exception to
be thrownX
- if there is no value presentjava.lang.NullPointerException
- if no value is present and
exceptionSupplier
is nullX extends java.lang.Throwable
public T orIfNull(T other)
other
.other
- the value to be returned if not present or null, may be nullother
public <E extends java.lang.Exception> T orGetIfNull(Try.Supplier<? extends T,E> other) throws E extends java.lang.Exception
other
and return the result of that invocation.other
- a Supplier
whose result is returned if not present or nullother.get()
java.lang.NullPointerException
- if value is not present and other
is nullE extends java.lang.Exception
public <X extends java.lang.Throwable> T orThrowIfNull(Supplier<? extends X> exceptionSupplier) throws X extends java.lang.Throwable
X
- Type of the exception to be thrownexceptionSupplier
- The supplier which will return the exception to be thrownX
- if not present or nulljava.lang.NullPointerException
- if not present or null and
exceptionSupplier
is nullX extends java.lang.Throwable
public java.util.List<T> toList()
List
with the value if it presents, otherwise an empty List
.public java.util.List<T> toListIfNotNull()
List
with the value if the value is not null, otherwise an empty List
.public java.util.Set<T> toSet()
Set
with the value if it presents, otherwise an empty Set
.public java.util.Set<T> toSetIfNotNull()
Set
with the value if the value is not null, otherwise an empty Set
.public Optional<T> toOptional()
Optional.empty()
if the value is not present or null
.public java.util.Optional<T> toJdkOptional()
java.util.Optional.empty()
if the value is not present or null
.