Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
corrected handling of null value in the TreeNodeComparator. Previous …
…if any node or atom were null the comparator would return '0' which is incorrect. If one is null and the other is not the objects are not equal.

Change-Id: I928a6832ec15cdf5fad67a9dec41bcec51b05db1
Signed-off-by: Egon Willighagen <egonw@users.sourceforge.net>

Conflicts:
	src/main/org/openscience/cdk/tools/HOSECodeGenerator.java
  • Loading branch information
johnmay authored and egonw committed Dec 12, 2012
1 parent 6a323f0 commit 65ed12c
Showing 1 changed file with 24 additions and 16 deletions.
40 changes: 24 additions & 16 deletions src/main/org/openscience/cdk/tools/HOSECodeGenerator.java
Expand Up @@ -733,25 +733,33 @@ class TreeNodeComparator implements Comparator<TreeNode> {
/**
*The compare method, compares by canonical label of atoms
*
* @param obj1 The first TreeNode
* @param obj2 The second TreeNode
* @param a The first TreeNode
* @param b The second TreeNode
* @return -1,0,1
*/
public int compare(TreeNode obj1, TreeNode obj2) {
if(obj1==null || obj2==null || ((TreeNode) obj1).getAtom()==null || ((TreeNode) obj2).getAtom()==null)
return 0;
Long label1 = (Long)((TreeNode) obj1).getAtom().getProperty(InvPair.CANONICAL_LABEL);
Long label2 = (Long)((TreeNode) obj2).getAtom().getProperty(InvPair.CANONICAL_LABEL);
if(label1==null || label2==null)
return 0;
if (label1.intValue() < label2.intValue()) {
return (-1);
}
if (label1.intValue() > label2.intValue()) {
return (1);
}
return (0);
public int compare(TreeNode a, TreeNode b) {
return label(a).compareTo(label(b));
}

/**
* Access the canonical label for the given tree node's atom. If any component is null
* then {@link Long#MIN_VALUE} is return thus sorting that object in lower order.
* @param node a tree node to get the label from
* @return canonical label value
*/
private Long label(TreeNode node){
if(node == null)
return Long.MIN_VALUE;
IAtom atom = node.getAtom();
if(atom == null)
return Long.MIN_VALUE;
// cast can be removed in master
Long label = (Long) atom.getProperty(InvPair.CANONICAL_LABEL);
if(label == null)
return Long.MIN_VALUE;
return label;
}

}

/**
Expand Down

0 comments on commit 65ed12c

Please sign in to comment.