Skip to content

Commit

Permalink
Updated comparator on AtomPlacer. The comparator now uses the built i…
Browse files Browse the repository at this point in the history
…n Integer comparator. To avoid null pointer exceptions the access to the weight value is wrapped in a private method which return the minimum integer value when the weight is not set (i.e. null).
  • Loading branch information
johnmay authored and egonw committed Dec 12, 2012
1 parent ed399df commit 0d658df
Showing 1 changed file with 17 additions and 12 deletions.
29 changes: 17 additions & 12 deletions src/main/org/openscience/cdk/layout/AtomPlacer.java
Expand Up @@ -77,20 +77,25 @@ public class AtomPlacer
final Comparator<IAtom> ATOM_ORDER =
new Comparator<IAtom>()
{
public int compare(IAtom o1, IAtom o2)
public int compare(IAtom a, IAtom b)
{
int i1 = ((Integer) o1.getProperty("Weight"));
int i2 = ((Integer) o1.getProperty("Weight"));
if (i1 < i2)
{
return -1;
}
if (i1 == i2)
{
return 0;
}
return 1;
return weight(a).compareTo(weight(b));
}

/**
* Access the weight property of the provided atom. If the weight is
* not set (i.e. <i>null</i>) then {@link Integer#MIN_VALUE} is
* returned. This allows atoms with no weight to sort lower then
* those with weight.
*
* @param atom an atom to obtain the weight property from
* @return the weight value or minimum integer value if null
*/
private Integer weight(IAtom atom){
Integer weight = (Integer) atom.getProperty("Weight");
return weight != null ? weight : Integer.MIN_VALUE;
}

};


Expand Down

0 comments on commit 0d658df

Please sign in to comment.