T
- the inputR
- the result@FunctionalInterface
public interface Nullifier<T,R>
A functional interface with null-safe checks.
For use with getter chains where one or more elements in the chain can be null.
Example usage:
D d = Nullifier.eval(a, A::getB, B::getC, C::getD);
The above code is equivalent to:
D d = null; if (a != null) { B b = a.getB(); if (b != null) { C c = b.getC(); if (c != null) { d = c.getD(); } } }
To test if d
is null, use:
boolean dIsNull = Nullifier.isNull(a, A::getB, B::getC, C::getD);
Implement $apply(Object)
; invoke apply(Object)
.
Modifier and Type | Method and Description |
---|---|
R |
$apply(T t)
Implement this method with a lambda expression/method reference.
|
default <V> Nullifier<T,V> |
andThenSpan(Nullifier<? super R,? extends V> after)
Chains two instances together.
|
default R |
apply(T t)
If the argument is null, returns null; else invokes
$apply(Object) . |
default R |
applyOr(T t,
R defaultValue)
As
apply(Object) except checked another value can be returned if the result
would otherwise be null. |
default R |
applyOrGet(T t,
java.util.function.Supplier<R> factory)
As
apply(Object) except checked another value can be returned if the result
would otherwise be null. |
static <A,B,C,D,E,Z> |
eval(A a,
Nullifier<? super A,? extends B> f0,
Nullifier<? super B,? extends C> f1,
Nullifier<? super C,? extends D> f2,
Nullifier<? super D,? extends E> f3,
Nullifier<? super E,? extends Z> f4) |
static <A,B,C,D,Z> |
eval(A a,
Nullifier<? super A,? extends B> f0,
Nullifier<? super B,? extends C> f1,
Nullifier<? super C,? extends D> f2,
Nullifier<? super D,? extends Z> f3) |
static <A,B,C,Z> Z |
eval(A a,
Nullifier<? super A,? extends B> f0,
Nullifier<? super B,? extends C> f1,
Nullifier<? super C,? extends Z> f2) |
static <A,B,Z> Z |
eval(A a,
Nullifier<? super A,? extends B> f0,
Nullifier<? super B,? extends Z> f1)
Convenience method for evaluating a chain of
Nullifier calls. |
static <A,Z> Z |
eval(A a,
Nullifier<? super A,? extends Z> f0) |
static <A,B,C,D,E,Z> |
isNull(A a,
Nullifier<? super A,? extends B> f0,
Nullifier<? super B,? extends C> f1,
Nullifier<? super C,? extends D> f2,
Nullifier<? super D,? extends E> f3,
Nullifier<? super E,? extends Z> f4) |
static <A,B,C,D,Z> |
isNull(A a,
Nullifier<? super A,? extends B> f0,
Nullifier<? super B,? extends C> f1,
Nullifier<? super C,? extends D> f2,
Nullifier<? super D,? extends Z> f3) |
static <A,B,C,Z> boolean |
isNull(A a,
Nullifier<? super A,? extends B> f0,
Nullifier<? super B,? extends C> f1,
Nullifier<? super C,? extends Z> f2) |
static <A,B,Z> boolean |
isNull(A a,
Nullifier<? super A,? extends B> f0,
Nullifier<? super B,? extends Z> f1)
Convenience method for evaluating a chain of
Nullifier calls to see if any link is null. |
static <A,Z> boolean |
isNull(A a,
Nullifier<? super A,? extends Z> f0) |
static <A,B,C,D,E,Z> |
span(Nullifier<? super A,? extends B> f0,
Nullifier<? super B,? extends C> f1,
Nullifier<? super C,? extends D> f2,
Nullifier<? super D,? extends E> f3,
Nullifier<? super E,? extends Z> f4) |
static <A,B,C,D,Z> |
span(Nullifier<? super A,? extends B> f0,
Nullifier<? super B,? extends C> f1,
Nullifier<? super C,? extends D> f2,
Nullifier<? super D,? extends Z> f3) |
static <A,B,C,Z> Nullifier<A,Z> |
span(Nullifier<? super A,? extends B> f0,
Nullifier<? super B,? extends C> f1,
Nullifier<? super C,? extends Z> f2) |
static <A,B,Z> Nullifier<A,Z> |
span(Nullifier<? super A,? extends B> f0,
Nullifier<? super B,? extends Z> f1)
Creates a null-safe chain of calls spanning possibly null call sites.
|
R $apply(T t) throws java.lang.Exception
Consumers should invoke apply(Object)
and NOT call this method directly.
t
- the argument; not null if invoked by default apply(Object)
java.lang.Exception
- on errorstatic <A,B,Z> Nullifier<A,Z> span(Nullifier<? super A,? extends B> f0, Nullifier<? super B,? extends Z> f1)
The functions may not be null, but the inputs and outputs may be.
A number of overloaded methods are provided with varying argument counts.
A
- the initial typeB
- an intermediary typeZ
- the resultant typef0
- the initial function; MUST NOT be nullf1
- a subsequent function; MUST NOT be nullstatic <A,B,C,Z> Nullifier<A,Z> span(Nullifier<? super A,? extends B> f0, Nullifier<? super B,? extends C> f1, Nullifier<? super C,? extends Z> f2)
static <A,B,C,D,Z> Nullifier<A,Z> span(Nullifier<? super A,? extends B> f0, Nullifier<? super B,? extends C> f1, Nullifier<? super C,? extends D> f2, Nullifier<? super D,? extends Z> f3)
static <A,B,C,D,E,Z> Nullifier<A,Z> span(Nullifier<? super A,? extends B> f0, Nullifier<? super B,? extends C> f1, Nullifier<? super C,? extends D> f2, Nullifier<? super D,? extends E> f3, Nullifier<? super E,? extends Z> f4)
static <A,Z> Z eval(A a, Nullifier<? super A,? extends Z> f0)
static <A,B,Z> Z eval(A a, Nullifier<? super A,? extends B> f0, Nullifier<? super B,? extends Z> f1)
Nullifier
calls.
A number of overloaded methods are provided with varying argument counts.
A
- the root typeB
- an intermediary typeZ
- the result typea
- the root object in the object graph (may be null)f0
- is passed "a"; MUST NOT be nullf1
- is passed the result of "f0"; MUST NOT be nulleval(Object, Nullifier)
,
eval(Object, Nullifier, Nullifier)
,
eval(Object, Nullifier, Nullifier, Nullifier)
,
eval(Object, Nullifier, Nullifier, Nullifier, Nullifier, Nullifier)
static <A,B,C,Z> Z eval(A a, Nullifier<? super A,? extends B> f0, Nullifier<? super B,? extends C> f1, Nullifier<? super C,? extends Z> f2)
static <A,B,C,D,Z> Z eval(A a, Nullifier<? super A,? extends B> f0, Nullifier<? super B,? extends C> f1, Nullifier<? super C,? extends D> f2, Nullifier<? super D,? extends Z> f3)
static <A,B,C,D,E,Z> Z eval(A a, Nullifier<? super A,? extends B> f0, Nullifier<? super B,? extends C> f1, Nullifier<? super C,? extends D> f2, Nullifier<? super D,? extends E> f3, Nullifier<? super E,? extends Z> f4)
static <A,Z> boolean isNull(A a, Nullifier<? super A,? extends Z> f0)
static <A,B,Z> boolean isNull(A a, Nullifier<? super A,? extends B> f0, Nullifier<? super B,? extends Z> f1)
Convenience method for evaluating a chain of Nullifier
calls to see if any link is null.
A number of overloaded methods are provided with varying argument counts.
Equivalent to:
boolean isNull = (Nullifier.eval(a, f0, f1) == null);
A
- the root typeB
- an intermediary typeZ
- the result typea
- the root object in the object graph (may be null)f0
- is passed "a"; MUST NOT be nullf1
- is passed the result of "f0"; MUST NOT be nullisNull(Object, Nullifier)
,
isNull(Object, Nullifier, Nullifier)
,
isNull(Object, Nullifier, Nullifier, Nullifier)
,
isNull(Object, Nullifier, Nullifier, Nullifier, Nullifier, Nullifier)
static <A,B,C,Z> boolean isNull(A a, Nullifier<? super A,? extends B> f0, Nullifier<? super B,? extends C> f1, Nullifier<? super C,? extends Z> f2)
static <A,B,C,D,Z> boolean isNull(A a, Nullifier<? super A,? extends B> f0, Nullifier<? super B,? extends C> f1, Nullifier<? super C,? extends D> f2, Nullifier<? super D,? extends Z> f3)
static <A,B,C,D,E,Z> boolean isNull(A a, Nullifier<? super A,? extends B> f0, Nullifier<? super B,? extends C> f1, Nullifier<? super C,? extends D> f2, Nullifier<? super D,? extends E> f3, Nullifier<? super E,? extends Z> f4)
default R apply(T t)
$apply(Object)
.
This method rethrows any exception thrown by $apply(Object)
as an unchecked exception.
t
- the input which may be nullExceptions.throwChecked(Throwable)
default <V> Nullifier<T,V> andThenSpan(Nullifier<? super R,? extends V> after)
V
- the new result typeafter
- the nullifier to invoke after this; MUST NOT be nulldefault R applyOr(T t, R defaultValue)
apply(Object)
except checked another value can be returned if the result
would otherwise be null.t
- the input which may be nulldefaultValue
- a default valuedefault R applyOrGet(T t, java.util.function.Supplier<R> factory)
apply(Object)
except checked another value can be returned if the result
would otherwise be null.t
- the input which may be nullfactory
- result producer called for null cases