I have two lambda functions (predicates):
final Predicate<Node> isElement = node -> node.getNodeType() == Node.ELEMENT_NODE;
final BiPredicate<Node, String> hasName = (node, name) -> node.getNodeName().equals(name);
Which I want to combine in some concise way, something like this:
// Pseudocode
isElement.and(hasName("tag")) // type of Predicate
and then pass to another lambda function:
final BiFunction<Node, Predicate<Node>, List<Node>> getChilds = (node, cond) -> {
List<Node> resultList = new ArrayList<>();
NodeList nodeList = node.getChildNodes();
for (int i = 0; i < nodeList.getLength(); ++i) {
Node tmp = nodeList.item(i);
if (cond.test(tmp)) {
resultList.add(tmp);
}
}
return resultList;
};
As a result I'm expecting it would look like the following:
List<Node> listNode = getChilds.apply(document, isElement.and(hasName("tag")));
But and
method of Predicate
doesn't accept BiPredicate
parameter.
How I could do this?
You need to write a static helper function that curries your BiPredicate down to a Predicate which you can then
.and()
with the other predicate.isElement.and(curryRight(hasName, "tag"))
alternatively, you can just do this:
isElement.and(node -> hasName.test(node, "tag"))
it's not that much longer