private static final Predicate<Parent> HAS_SON = parent -> IS_CHILD(parent.getChild);
private static final Predicate<Child> IS_CHILD = Objects::nonNull;
private static final Predicate<Parent> IS_PARENT = Objects::nonNull;
private static final Predicate<Parent> IS_FATHER = IS_PARENT.and(HAS_SON);
I am trying to chain predicates, with one twist, and that is at one of predicate I want to use child object.
This is hypothetical situation I tried to make things easier for understanding.
parent
)parent
is not null
child
is not nullThere is a problem HAS_SON, I know syntax is not right, and may be nesting(parent.child) might not be allowed. Can some one please confirm? right now work around I am using is
private static final Predicate<Parent> HAS_SON = parent -> parent.getChild() != null;
Do not forget that a Predicate
is triggered by using the test
method of the FunctionalInterface
. The following will work
private static final Predicate<Child> IS_CHILD = Objects::nonNull;
private static final Predicate<Parent> HAS_SON = parent -> IS_CHILD.test(parent.getChild);
private static final Predicate<Parent> IS_PARENT = Objects::nonNull;
private static final Predicate<Parent> IS_FATHER = IS_PARENT.and(HAS_SON);