public final class Nullable<T> extends Any<T>
isPresent()
will return true
and
get()
will return the value.
Additional methods that depend on the presence or absence of a contained
value are provided, such as orElse()
(return a default value if value not present) and
ifPresent()
(execute a block
of code if the value is present).
This is a value-based
class; use of identity-sensitive operations (including reference equality
(==
), identity hash code, or synchronization) on instances of
Nullable
may have unpredictable results and should be avoided.
Modifier and Type | Method and Description |
---|---|
static <T> Nullable<T> |
empty()
Returns an empty
Nullable instance. |
boolean |
equals(java.lang.Object obj)
Indicates whether some other object is "equal to" this Optional.
|
<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
Nullable describing the value, otherwise return an
empty Nullable . |
<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
Nullable describing the value, otherwise return an
empty Nullable . |
<U,E extends java.lang.Exception> |
flatMap(Try.Function<? super T,? extends Any<U>,E> mapper)
If a value is present, apply the provided
Nullable -bearing
mapping function to it, return that result, otherwise return an empty
Nullable . |
<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
Nullable -bearing
mapping function to it, return that result, otherwise return an empty
Nullable . |
int |
hashCode()
Returns the hash code value of the present value, if any, or 0 (zero) if
no value is present.
|
<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,
|
<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> Nullable<T> |
of(T value)
Returns an
Nullable with the specified present nullable value. |
<E extends java.lang.Exception> |
or(Try.Supplier<? extends Any<T>,E> supplier)
Returns
this if it's present or call supplier.get() . |
java.lang.String |
toString()
Returns a non-empty string representation of this Optional suitable for
debugging.
|
get, ifNotNull, ifNotNullOrElse, ifPresent, ifPresentOrElse, isNotNull, isPresent, orElse, orElseGet, orElseThrow, orGetIfNull, orIfNull, orThrowIfNull, stream, streamIfNotNull, toJdkOptional, toList, toListIfNotNull, toOptional, toSet, toSetIfNotNull
public static <T> Nullable<T> empty()
Nullable
instance. No value is present for this
Optional.T
- Type of the non-existent valueNullable
public static <T> Nullable<T> of(T value)
Nullable
with the specified present nullable value.value
- the value to be present, which could be nullNullable
with the value presentpublic <E extends java.lang.Exception> Nullable<T> filter(Try.Predicate<? super T,E> predicate) throws E extends java.lang.Exception
Nullable
describing the value, otherwise return an
empty Nullable
.filter
in class Any<T>
predicate
- a predicate to apply to the value, if presentNullable
describing the value of this Nullable
if a value is present and the value matches the given predicate,
otherwise an empty Nullable
java.lang.NullPointerException
- if the predicate is nullE extends java.lang.Exception
public <U,E extends java.lang.Exception> Nullable<U> map(Try.Function<? super T,? extends U,E> mapper) throws E extends java.lang.Exception
map
in class Any<T>
U
- The type of the result of the mapping functionmapper
- a mapping function to apply to the value, if presentNullable
describing the result of applying a mapping
function to the value of this Nullable
, if a value is present,
otherwise an empty Nullable
java.lang.NullPointerException
- if the mapping function is nullE extends java.lang.Exception
public <U,E extends java.lang.Exception> Nullable<U> flatMap(Try.Function<? super T,? extends Any<U>,E> mapper) throws E extends java.lang.Exception
Nullable
-bearing
mapping function to it, return that result, otherwise return an empty
Nullable
. This method is similar to #map(Function)
,
but the provided mapper is one whose result is already an Nullable
,
and if invoked, flatMap
does not wrap it with an additional
Nullable
.flatMap
in class Any<T>
U
- The type parameter to the Nullable
returned bymapper
- a mapping function to apply to the value, if present
the mapping functionNullable
-bearing mapping
function to the value of this Nullable
, if a value is present,
otherwise an empty Nullable
java.lang.NullPointerException
- if the mapping function is null or returns
a null resultE extends java.lang.Exception
public <E extends java.lang.Exception> Nullable<T> filterIfNotNull(Try.Predicate<? super T,E> predicate) throws E extends java.lang.Exception
Nullable
describing the value, otherwise return an
empty Nullable
.filterIfNotNull
in class Any<T>
predicate
- a predicate to apply to the value, if presentNullable
describing the value of this Nullable
if a value is present and the value matches the given predicate,
otherwise an empty Nullable
java.lang.NullPointerException
- if the predicate is nullE extends java.lang.Exception
public <U,E extends java.lang.Exception> Nullable<U> mapIfNotNull(Try.Function<? super T,? extends U,E> mapper) throws E extends java.lang.Exception
mapIfNotNull
in class Any<T>
U
- The type of the result of the mapping functionmapper
- a mapping function to apply to the value, if not nullNullable
describing the result of applying a mapping
function to the value of this Nullable
, if a value is not null,
otherwise an empty Nullable
java.lang.NullPointerException
- if the mapping function is nullE extends java.lang.Exception
public <U,E extends java.lang.Exception> Nullable<U> flatMapIfNotNull(Try.Function<? super T,? extends Any<U>,E> mapper) throws E extends java.lang.Exception
Nullable
-bearing
mapping function to it, return that result, otherwise return an empty
Nullable
. This method is similar to #map(Function)
,
but the provided mapper is one whose result is already an Nullable
,
and if invoked, flatMap
does not wrap it with an additional
Nullable
.flatMapIfNotNull
in class Any<T>
U
- The type parameter to the Nullable
returned bymapper
- a mapping function to apply to the value, if not null
the mapping functionNullable
-bearing mapping
function to the value of this Nullable
, if a value is not null,
otherwise an empty Nullable
java.lang.NullPointerException
- if the mapping function is null or returns
a null resultE extends java.lang.Exception
public <E extends java.lang.Exception> Nullable<T> or(Try.Supplier<? extends Any<T>,E> supplier) throws E extends java.lang.Exception
this
if it's present or call supplier.get()
.public boolean equals(java.lang.Object obj)
Nullable
and;
equals()
.
equals
in class java.lang.Object
obj
- an object to be tested for equalityfalse
public int hashCode()
hashCode
in class java.lang.Object
public java.lang.String toString()
toString
in class java.lang.Object