P
- parameter type@FunctionalInterface
public interface Predicate1<P>
extends java.util.function.Predicate<P>
Predicates can be chained to simulate boolean logic by
and(Predicate)
, or(Predicate)
,
xor(Predicate)
, and negate()
.
The chaining does not always work as expected from
standard Java expressions. Therefore there are
versions of the above methods with an underscore suffix
which allow to widen the type: and_(Predicate)
,
or_(Predicate)
, xor_(Predicate)
.
There are also static methods which take care of the
type fiddling transparently: and(Predicate, Predicate)
,
Useful for start values in algorithms or as default values
are the two values returned by alwaysTrue()
(a predicate which always returns true
regardless
the argument applied to the Predicate.test(Object)
method)
and alwaysFalse()
(same for false
).
This also acts as a standard Predicate
.
Predicate2
,
Predicate3
,
Predicate4
,
Predicate5
,
Predicate6
,
Predicate7
,
Predicate8
,
Predicate9
Modifier and Type | Field and Description |
---|---|
static Predicate1<java.lang.Object> |
ALWAYS_FALSE
Deprecated.
Use
alwaysFalse() instead as the returned predicate is smarter. |
static Predicate1<java.lang.Object> |
ALWAYS_TRUE
Deprecated.
Use
alwaysTrue() instead as the returned predicate is smarter. |
Modifier and Type | Method and Description |
---|---|
default <S> Predicate1<S> |
adapted(java.util.function.Function<? super S,? extends P> mapper)
Use this predicate on a different type.
|
static <T> Predicate1<T> |
alwaysFalse()
Get a predicate which always evaluates to
false . |
static <T> Predicate1<T> |
alwaysTrue()
Get a predicate which always evaluates to
true . |
default <T extends P> |
and_(java.util.function.Predicate<T> other)
Combine this and another predicate with logical and.
|
default Predicate1<P> |
and(java.util.function.Predicate<? super P> other)
Combine this and another predicate with logical and.
|
static <T> Predicate1<T> |
and(java.util.function.Predicate<? super T> pred1,
java.util.function.Predicate<? super T> pred2)
Get a predicate which is the combination of two predicates
with a logical and.
|
static <T> Predicate1<T> |
from(java.util.function.Predicate<T> predicate)
Use a standard
Predicate as a Predicate1 ,
This is eg useful if your want to use the xor(Predicate) method. |
static <T> Predicate1<T> |
isEqual(T target)
Get a predicate which checks deep equality with the given target.
|
default Predicate1<P> |
negate()
Get the negation of this predicate.
|
default <T extends P> |
or_(java.util.function.Predicate<T> other)
Combine this and another predicate with logical or.
|
default Predicate1<P> |
or(java.util.function.Predicate<? super P> other)
Combine this and another predicate with logical or.
|
static <T> Predicate1<T> |
or(java.util.function.Predicate<? super T> pred1,
java.util.function.Predicate<? super T> pred2)
Get a predicate which is the combination of two predicates
with a logical or.
|
default boolean |
testNonNull(P argument,
boolean fallback)
Test the argument only if it is not
null , otherwise return the fallback. |
default <T extends P> |
xor_(java.util.function.Predicate<T> other)
Combine this and another predicate with logical exclusive or.
|
default Predicate1<P> |
xor(java.util.function.Predicate<? super P> other)
Combine this and another predicate with logical exclusive or.
|
static <T> Predicate1<T> |
xor(java.util.function.Predicate<? super T> pred1,
java.util.function.Predicate<? super T> pred2)
Get a predicate which is the combination of two predicates
with a logical exclusive or.
|
@Deprecated static final Predicate1<java.lang.Object> ALWAYS_TRUE
alwaysTrue()
instead as the returned predicate is smarter.@Deprecated static final Predicate1<java.lang.Object> ALWAYS_FALSE
alwaysFalse()
instead as the returned predicate is smarter.default boolean testNonNull(P argument, boolean fallback)
null
, otherwise return the fallback.
Using this invocation it is guaranteed that Predicate.test(Object)
will always be called with
a non-null argument.
argument
- argument of this functionfallback
- fallback returned if argument
is null
argument
,
or fallback
if argument
is null@NotNull default Predicate1<P> and(@NotNull java.util.function.Predicate<? super P> other)
false
the other one is
not evaluated.and
in interface java.util.function.Predicate<P>
other
- other predicate to be chained with logical andand(Predicate, Predicate)
,
and_(Predicate)
@NotNull default <T extends P> Predicate1<T> and_(@NotNull java.util.function.Predicate<T> other)
false
the other one is
not evaluated.
Compared to and(Predicate)
this method allows
to widen the type of this predicate to the one of the other predicate
like in standard Jave logical expressions, eg
Predicate1<Object> nonNull = o -> o != null;
Predicate1<String> notEmpty = s -> !s.isEmpty();
// Predicate1<???> bad = nonNull.and(notEmpty); // not possible as String is not a super type of Object
Predicate1<String> valid = nonMull.and_(notEmpty); // not nice, but it works
T
- type on which the other predicate operates, also the type of
the returned predicateother
- other predicateand(Predicate, Predicate)
,
and(Predicate)
@NotNull default Predicate1<P> or(@NotNull java.util.function.Predicate<? super P> other)
true
the other one is
not evaluated.or
in interface java.util.function.Predicate<P>
other
- other predicate to be chained with logical oror(Predicate, Predicate)
,
or_(Predicate)
@NotNull default <T extends P> Predicate1<T> or_(@NotNull java.util.function.Predicate<T> other)
true
the other one is
not evaluated.
Compared to or(Predicate)
this method allows
to widen the type of this predicate to the one of the other predicate
like in standard Jave logical expressions, eg
Predicate1<Object> isNull = o -> o == null;
Predicate1<String> isEmpty = s -> !s.isEmpty();
// Predicate1<???> bad = pred1.or(pred2); // not possible as String is not a super type of Object
Predicate1<String> useless = pred1.or_(pred2); // not nice, but it works
T
- type on which the other predicate operates, also the type of
the returned predicateother
- other predicateor(Predicate, Predicate)
,
or(Predicate)
@NotNull default Predicate1<P> xor(@NotNull java.util.function.Predicate<? super P> other)
other
- other predicate to be chained with logical exclusive orxor(Predicate, Predicate)
,
xor_(Predicate)
@NotNull default <T extends P> Predicate1<T> xor_(@NotNull java.util.function.Predicate<T> other)
true
the other one is
not evaluated.
Compared to or(Predicate)
this method allows
to widen the type of this predicate to the one of the other predicate
like in standard Jave logical expressions, eg
Predicate1<Object> isNull = o -> o == null;
Predicate1<String> isEmpty = s -> !s.isEmpty();
// Predicate1<???> bad = isNull.xor(isEmpty); // not possible as String is not a super type of Object
Predicate1<String> useless = isNull.xor_(isEmpty); // bad example, but it works
T
- type on which the other predicate operates, also the type of
the returned predicateother
- other predicatexor(Predicate, Predicate)
,
xor(Predicate)
@NotNull default Predicate1<P> negate()
negate
in interface java.util.function.Predicate<P>
@NotNull default <S> Predicate1<S> adapted(@NotNull java.util.function.Function<? super S,? extends P> mapper)
S
- different type on which the returned predicate operatesmapper
- mapper function which converts the different type to one this predicate accepts.@NotNull static <T> Predicate1<T> from(@NotNull java.util.function.Predicate<T> predicate)
Predicate
as a Predicate1
,
This is eg useful if your want to use the xor(Predicate)
method.T
- argument typepredicate
- standard predicatePredicate1
view of given predicate@NotNull static <T> Predicate1<T> alwaysTrue()
true
.T
- type of predicatetrue
@NotNull static <T> Predicate1<T> alwaysFalse()
false
.T
- type of predicatefalse
@NotNull static <T> Predicate1<T> isEqual(@Nullable T target)
Different to Predicate.isEqual(Object)
this internally uses
Objects.deepEquals(Object, Object)
to check for equality.
isEqual
in interface java.util.function.Predicate<P>
T
- target typetarget
- target object, possibly null
target
@NotNull static <T> Predicate1<T> and(@NotNull java.util.function.Predicate<? super T> pred1, @NotNull java.util.function.Predicate<? super T> pred2)
This method takes care of always creating a predicate of the correct type
without having to fiddle with and(Predicate)
or and_(Predicate)
depending on the types.
The result will use shortcut evaluation, so if this
predicate already returns false
the other one is
not evaluated.
T
- resulting type, which is the most extending type of both pred1
's
type and pred2
's type, so if neither type is extending the
other this method will not compilepred1
- first predicatepred2
- second predicateand(Predicate)
,
and_(Predicate)
@NotNull static <T> Predicate1<T> or(@NotNull java.util.function.Predicate<? super T> pred1, @NotNull java.util.function.Predicate<? super T> pred2)
This method takes care of always creating a predicate of the correct type
without having to fiddle with or(Predicate)
or or_(Predicate)
depending on the types.
The result will use shortcut evaluation, so if this
predicate already returns false
the other one is
not evaluated.
T
- resulting type, which is the most extending type of both pred1
's
type and pred2
's type, so if neither type is extending the
other this method will not compilepred1
- first predicatepred2
- second predicateor(Predicate)
,
or_(Predicate)
@NotNull static <T> Predicate1<T> xor(@NotNull java.util.function.Predicate<? super T> pred1, @NotNull java.util.function.Predicate<? super T> pred2)
This method takes care of always creating a predicate of the correct type
without having to fiddle with or(Predicate)
or or_(Predicate)
depending on the types.
The result will use shortcut evaluation, so if this
predicate already returns true
the other one is
not evaluated.
T
- resulting type, which is the most extending type of both pred1
's
type and pred2
's type, so if neither type is extending the
other this method will not compilepred1
- first predicatepred2
- second predicatexor(Predicate)
,
xor_(Predicate)