diff --git a/lucene/CHANGES.txt b/lucene/CHANGES.txt index 2c2cdb145f01..be199050a660 100644 --- a/lucene/CHANGES.txt +++ b/lucene/CHANGES.txt @@ -18,6 +18,8 @@ Improvements * GITHUB#13092: `static final Map` constants have been made immutable (Dmitry Cherniachenko) +* GITHUB#13041: TokenizedPhraseQueryNode code cleanup (Dmitry Cherniachenko) + Optimizations --------------------- (No changes) diff --git a/lucene/queryparser/src/java/org/apache/lucene/queryparser/flexible/core/nodes/TokenizedPhraseQueryNode.java b/lucene/queryparser/src/java/org/apache/lucene/queryparser/flexible/core/nodes/TokenizedPhraseQueryNode.java index f5a7b0acab0c..29710dce2afa 100644 --- a/lucene/queryparser/src/java/org/apache/lucene/queryparser/flexible/core/nodes/TokenizedPhraseQueryNode.java +++ b/lucene/queryparser/src/java/org/apache/lucene/queryparser/flexible/core/nodes/TokenizedPhraseQueryNode.java @@ -32,10 +32,11 @@ public TokenizedPhraseQueryNode() { @Override public String toString() { - if (getChildren() == null || getChildren().size() == 0) return ""; + List children = getChildren(); + if (children == null || children.isEmpty()) return ""; StringBuilder sb = new StringBuilder(); - sb.append(""); - for (QueryNode child : getChildren()) { + sb.append(""); + for (QueryNode child : children) { sb.append("\n"); sb.append(child.toString()); } @@ -46,16 +47,15 @@ public String toString() { // This text representation is not re-parseable @Override public CharSequence toQueryString(EscapeQuerySyntax escapeSyntaxParser) { - if (getChildren() == null || getChildren().size() == 0) return ""; - + List children = getChildren(); + if (children == null || children.isEmpty()) return ""; StringBuilder sb = new StringBuilder(); String filler = ""; - for (QueryNode child : getChildren()) { + for (QueryNode child : children) { sb.append(filler).append(child.toQueryString(escapeSyntaxParser)); filler = ","; } - - return "[TP[" + sb.toString() + "]]"; + return "[TP[" + sb + "]]"; } @Override @@ -70,27 +70,25 @@ public QueryNode cloneTree() throws CloneNotSupportedException { @Override public CharSequence getField() { List children = getChildren(); - - if (children == null || children.size() == 0) { - return null; - - } else { - return ((FieldableNode) children.get(0)).getField(); + if (children != null) { + for (QueryNode child : children) { + if (child instanceof FieldableNode) { + return ((FieldableNode) child).getField(); + } + } } + return null; } @Override public void setField(CharSequence fieldName) { List children = getChildren(); - if (children != null) { - - for (QueryNode child : getChildren()) { - + for (QueryNode child : children) { if (child instanceof FieldableNode) { ((FieldableNode) child).setField(fieldName); } } } } -} // end class MultitermQueryNode +}