Skip to content
Permalink
Browse files
[JXPATH-133] use == or .equals on parent node comparison in compareTo…
…(); associated refactorings

git-svn-id: https://svn.apache.org/repos/asf/commons/proper/jxpath/trunk@918156 13f79535-47bb-0310-9956-ffa450edef68
  • Loading branch information
mbenson committed Mar 2, 2010
1 parent 465e2fe commit 5badb5743937ebdc218a5ca51385e2153579cf1b
Showing 1 changed file with 8 additions and 14 deletions.
@@ -422,10 +422,10 @@ public boolean testNode(NodeTest test) {

String testPrefix = testName.getPrefix();
String nodePrefix = nodeName.getPrefix();
if (!equalStrings(testPrefix, nodePrefix)) {
if (!safeEquals(testPrefix, nodePrefix)) {
String testNS = getNamespaceURI(testPrefix);
String nodeNS = getNamespaceURI(nodePrefix);
if (!equalStrings(testNS, nodeNS)) {
if (!safeEquals(testNS, nodeNS)) {
return false;
}
}
@@ -438,16 +438,6 @@ public boolean testNode(NodeTest test) {
&& ((NodeTypeTest) test).getNodeType() == Compiler.NODE_TYPE_NODE && isNode();
}

/**
* Compare two strings, either of which may be null, for equality.
* @param s1 the first String to compare
* @param s2 the second String to compare
* @return true if both Strings are null, same or equal
*/
private static boolean equalStrings(String s1, String s2) {
return s1 == s2 || s1 != null && s1.equals(s2);
}

/**
* Called directly by JXPathContext. Must create path and
* set value.
@@ -740,7 +730,7 @@ public int compareTo(Object object) {
}
// Let it throw a ClassCastException
NodePointer pointer = (NodePointer) object;
if (parent == pointer.parent) {
if (safeEquals(parent, pointer.parent)) {
return parent == null ? 0 : parent.compareChildNodePointers(this, pointer);
}

@@ -791,7 +781,7 @@ private int compareNodePointers(
return r == 0 ? 1 : r;
}
//henceforth depth1 == depth2:
if (p1 == p2 || p1 != null && p1.equals(p2)) {
if (safeEquals(p1, p2)) {
return 0;
}
if (depth1 == 1) {
@@ -920,4 +910,8 @@ private static void printDeep(NodePointer pointer, String indent) {
printDeep(pointer.getImmediateParentPointer(), indent + " ");
}
}

private static boolean safeEquals(Object o1, Object o2) {
return o1 == o2 || o1 != null && o1.equals(o2);
}
}

0 comments on commit 5badb57

Please sign in to comment.