Skip to content

Commit

Permalink
[refactor] Remove home-baked Function and Predicate interfaces in favour
Browse files Browse the repository at this point in the history
of those in Java 8
  • Loading branch information
adamretter committed Apr 6, 2015
1 parent 6b5d2ca commit 344b01b
Show file tree
Hide file tree
Showing 7 changed files with 80 additions and 202 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -19,10 +19,7 @@
*/
package org.exist.xquery.modules.ngram;

import java.util.ArrayList;
import java.util.HashSet;
import java.util.List;
import java.util.Set;
import java.util.*;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

Expand All @@ -47,7 +44,6 @@
import org.exist.xquery.modules.ngram.query.Wildcard;
import org.exist.xquery.modules.ngram.query.WildcardedExpression;
import org.exist.xquery.modules.ngram.query.WildcardedExpressionSequence;
import org.exist.xquery.modules.ngram.utils.F;
import org.exist.xquery.modules.ngram.utils.NodeProxies;
import org.exist.xquery.modules.ngram.utils.NodeSets;
import org.exist.xquery.util.Error;
Expand Down Expand Up @@ -299,19 +295,13 @@ private NodeSet processMatches(
else if (getLocalName().startsWith("ends-with"))
result = NodeSets.getNodesMatchingAtEnd(result, getExpressionId());

result = NodeSets.fmapNodes(result, new F<NodeProxy, NodeProxy>() {

@Override
public NodeProxy f(NodeProxy a) {
return NodeProxies.fmapOwnMatches(a, new F<Match, Match>() {

@Override
public Match f(Match a) {
return a.filterOutOverlappingOffsets();
}
}, getExpressionId());
}
});
result = NodeSets.transformNodes(result, proxy ->
NodeProxies.transformOwnMatches(
proxy,
Match::filterOutOverlappingOffsets,
getExpressionId()
)
);

return result;
}
Expand Down Expand Up @@ -524,18 +514,10 @@ public NodeSet fixedStringSearch(

final NodeSet nodesContainingFirstINgrams = result;

result = NodeSets.fmapNodes(nodes, new F<NodeProxy, NodeProxy>() {

@Override
public NodeProxy f(NodeProxy a) {
NodeProxy before = nodesContainingFirstINgrams.get(a);
if (before != null) {
return getContinuousMatches(before, a);
} else {
return null;
}
}
});
result = NodeSets.transformNodes(nodes, proxy ->
Optional.ofNullable(nodesContainingFirstINgrams.get(proxy))
.map(before -> getContinuousMatches(before, proxy))
.orElse(null));
}
return result;
}
Expand Down Expand Up @@ -563,13 +545,7 @@ private NodeProxy getContinuousMatches(final NodeProxy head, final NodeProxy tai
headMatch = headMatch.getNextMatch();
}
if (continuousMatch != null) {
NodeProxies.filterMatches(tail, new F<Match, Boolean>() {

@Override
public Boolean f(Match a) {
return (a.getContextId() != getExpressionId());
}
});
NodeProxies.filterMatches(tail, match -> match.getContextId() != getExpressionId());

tail.addMatch(continuousMatch);
return tail;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,13 +26,10 @@
import org.apache.logging.log4j.Logger;
import org.exist.dom.persistent.DocumentSet;
import org.exist.dom.persistent.EmptyNodeSet;
import org.exist.dom.persistent.Match;
import org.exist.dom.persistent.NodeProxy;
import org.exist.dom.persistent.NodeSet;
import org.exist.dom.QName;
import org.exist.indexing.ngram.NGramIndexWorker;
import org.exist.xquery.XPathException;
import org.exist.xquery.modules.ngram.utils.F;
import org.exist.xquery.modules.ngram.utils.NodeProxies;
import org.exist.xquery.modules.ngram.utils.NodeSets;

Expand All @@ -46,7 +43,7 @@ public class WildcardedExpressionSequence implements EvaluatableExpression {

public WildcardedExpressionSequence(final List<WildcardedExpression> expressions) {

this.expressions = new ArrayList<WildcardedExpression>(expressions.size());
this.expressions = new ArrayList<>(expressions.size());

WildcardedExpression currentExpression = expressions.remove(0);

Expand Down Expand Up @@ -119,44 +116,26 @@ public NodeSet eval(
return result;
}

private NodeSet expandMatchesForward(final Wildcard trailingWildcard, final NodeSet nodes, final int expressionId)
throws XPathException {
return NodeSets.fmapNodes(nodes, new F<NodeProxy, NodeProxy>() {

@Override
public NodeProxy f(NodeProxy node) {
final int nodeLength = node.getNodeValue().length();
return NodeProxies.fmapOwnMatches(node, new F<Match, Match>() {

@Override
public Match f(Match a) {
return a.expandForward(trailingWildcard.minimumLength, trailingWildcard.maximumLength,
nodeLength);
}
}, expressionId);
}
});
private NodeSet expandMatchesForward(final Wildcard trailingWildcard, final NodeSet nodes, final int expressionId) throws XPathException {
return NodeSets.transformNodes(nodes, proxy ->
NodeProxies.transformOwnMatches(
proxy,
match -> match.expandForward(trailingWildcard.minimumLength, trailingWildcard.maximumLength, proxy.getNodeValue().length()),
expressionId
)
);
}

private NodeSet expandMatchesBackward(final Wildcard leadingWildcard, final NodeSet nodes, final int expressionId)
throws XPathException {
return NodeSets.fmapNodes(nodes, new F<NodeProxy, NodeProxy>() {

@Override
public NodeProxy f(NodeProxy node) {
return NodeProxies.fmapOwnMatches(node, new F<Match, Match>() {

@Override
public Match f(Match a) {
return a.expandBackward(leadingWildcard.minimumLength, leadingWildcard.maximumLength);
}
}, expressionId);
}
});
private NodeSet expandMatchesBackward(final Wildcard leadingWildcard, final NodeSet nodes, final int expressionId) throws XPathException {
return NodeSets.transformNodes(nodes, proxy ->
NodeProxies.transformOwnMatches(
proxy,
match -> match.expandBackward(leadingWildcard.minimumLength, leadingWildcard.maximumLength),
expressionId
)
);
}



/**
*
*/
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
package org.exist.xquery.modules.ngram.query;

import java.util.List;
import java.util.Optional;

import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
Expand All @@ -30,7 +31,6 @@
import org.exist.dom.QName;
import org.exist.indexing.ngram.NGramIndexWorker;
import org.exist.xquery.XPathException;
import org.exist.xquery.modules.ngram.utils.F;
import org.exist.xquery.modules.ngram.utils.NodeProxies;
import org.exist.xquery.modules.ngram.utils.NodeSets;

Expand Down Expand Up @@ -68,18 +68,10 @@ public NodeSet eval(
return tailNodes;
}

NodeSet result = NodeSets.fmapNodes(headNodes, new F<NodeProxy, NodeProxy>() {

@Override
public NodeProxy f(NodeProxy headNode) {
NodeProxy tailNode = tailNodes.get(headNode);
if (tailNode != null) {
return getMatchingNode(headNode, tailNode, expressionId);
} else {
return null;
}
}
});
final NodeSet result = NodeSets.transformNodes(headNodes, headNode ->
Optional.ofNullable(tailNodes.get(headNode))
.map(tailNode -> getMatchingNode(headNode, tailNode, expressionId))
.orElse(null));

return result;
}
Expand All @@ -106,13 +98,7 @@ private NodeProxy getMatchingNode(NodeProxy headNode, NodeProxy tailNode, final

if (found) {
// Remove own (partial) matches and add new complete match
NodeProxies.filterMatches(tailNode, new F<Match, Boolean>() {

@Override
public Boolean f(Match a) {
return a.getContextId() != expressionId;
}
});
NodeProxies.filterMatches(tailNode, a -> a.getContextId() != expressionId);

tailNode.addMatch(match);
result = tailNode;
Expand Down

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,9 @@
import org.exist.dom.persistent.Match;
import org.exist.dom.persistent.NodeProxy;

import java.util.function.Function;
import java.util.function.Predicate;

public final class NodeProxies {

private NodeProxies() {
Expand All @@ -36,12 +39,13 @@ private NodeProxies() {
* the predicate based on which the full-text matches are filtered: If the predicate returns true the
* match stays, if not the match is removed.
*/
public static void filterMatches(final NodeProxy node, final F<Match, Boolean> predicate) {
public static void filterMatches(final NodeProxy node, final Predicate<Match> predicate) {
Match m = node.getMatches();
node.setMatches(null);
while (m != null) {
if (predicate.f(m).booleanValue())
if (predicate.test(m)) {
node.addMatch(m);
}
m = m.getNextMatch();
}
}
Expand All @@ -53,14 +57,14 @@ public static void filterMatches(final NodeProxy node, final F<Match, Boolean> p
*
* @param node
* the NodeProxy to modify
* @param f
* @param transform
* the function to apply to all matches with the supplied expression id
* @param ownExpressionId
* the expression id of the matches to be transformed
* @return the modified node if at least one match with the supplied expression id was not transformed to null or
* null otherwise
*/
public static NodeProxy fmapOwnMatches(final NodeProxy node, final F<Match, Match> f, int ownExpressionId) {
public static NodeProxy transformOwnMatches(final NodeProxy node, final Function<Match, Match> transform, int ownExpressionId) {
Match m = node.getMatches();
node.setMatches(null);
boolean ownMatch = false;
Expand All @@ -69,7 +73,7 @@ public static NodeProxy fmapOwnMatches(final NodeProxy node, final F<Match, Matc
if (m.getContextId() != ownExpressionId) {
node.addMatch(m);
} else {
Match nm = f.f(m);
final Match nm = transform.apply(m);
if (nm != null) {
node.addMatch(nm);
ownMatch = true;
Expand All @@ -78,10 +82,6 @@ public static NodeProxy fmapOwnMatches(final NodeProxy node, final F<Match, Matc
m = m.getNextMatch();
}

if (ownMatch)
return node;
else
return null;
return ownMatch ? node : null;
}

}
Loading

0 comments on commit 344b01b

Please sign in to comment.