FazBrowse GitHub Viewer | Trending |
URL:
| Home
Tools: [Download Repo ZIP]   [Original HTTPS Page]

Javadoc qvar constraint string in XQG and refactor · javaUsers/MathMLQueryGenerator@502acaa · GitHub

Commit 502acaa

Browse files
committed
Javadoc qvar constraint string in XQG and refactor
* Replace concatenation with StringBuilders * Document the qvar constraint method
1 parent 64894ce commit 502acaa

1 file changed

Lines changed: 21 additions & 16 deletions

File tree

‎src/main/java/com/formulasearchengine/mathmlquerygenerator/XQueryGenerator.java‎

Lines changed: 21 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@
2828
@SuppressWarnings("WeakerAccess")
2929
public class XQueryGenerator {
3030
private static final Pattern ANNOTATION_XML_PATTERN = Pattern.compile( "annotation(-xml)?" );
31+
//Qvar map of qvar name to XPaths referenced by each qvar
3132
private Map<String, ArrayList<String>> qvar = new HashMap<>();
3233
private String relativeXPath = "";
3334
private String lengthConstraint = "";
@@ -139,33 +140,37 @@ public String toString() {
139140
if ( mainElement == null ) {
140141
return null;
141142
}
142-
String fixedConstraints = generateConstraints( mainElement, true );
143-
String qvarConstraintString = "";
143+
final String fixedConstraints = generateConstraints( mainElement, true );
144+
final StringBuilder qvarConstraintString = new StringBuilder();
145+
//Generate qvar constraint string
146+
//This specifies that the same qvars must refer to the same nodes, using the XQuery "=" equality
147+
//This is equality based on: same text, same node names, and same children by the "=" equality
144148
for ( Map.Entry<String, ArrayList<String>> entry : qvar.entrySet() ) {
145-
String addString = "";
146-
boolean newContent = false;
149+
final StringBuilder addString = new StringBuilder();
147150
if ( entry.getValue().size() > 1 ) {
148-
String first = entry.getValue().get( 0 );
149-
if ( qvarConstraintString.length() > 0 ) {
150-
addString += "\n and ";
151+
final String firstEntry = entry.getValue().get( 0 );
152+
if ( qvarConstraintString.length() != 0 ) {
153+
addString.append("\n and ");
151154
}
152-
String lastSecond = "";
153-
for ( String second : entry.getValue() ) {
154-
if ( !second.equals( first ) ) {
155-
if ( lastSecond.length() > 0 ) {
156-
addString += " and ";
155+
String lastEntry = "";
156+
boolean newContent = false;
157+
//begins at second entry
158+
for ( final String currentEntry : entry.getValue() ) {
159+
if ( !currentEntry.equals( firstEntry ) ) {
160+
if ( !lastEntry.isEmpty() ) {
161+
addString.append(" and ");
157162
}
158-
addString += "$x" + first + " = $x" + second;
159-
lastSecond = second;
163+
addString.append("$x").append(firstEntry).append(" = $x").append(currentEntry);
164+
lastEntry = currentEntry;
160165
newContent = true;
161166
}
162167
}
163168
if ( newContent ) {
164-
qvarConstraintString += addString;
169+
qvarConstraintString.append(addString);
165170
}
166171
}
167172
}
168-
return getString( mainElement, fixedConstraints, qvarConstraintString );
173+
return getString( mainElement, fixedConstraints, qvarConstraintString.toString() );
169174
}
170175

171176
/**

0 commit comments

Comments
 (0)

Back | FazBrowse Home | New Git URL