Skip to content

Commit

Permalink
Generified comparators, removing redundant casts
Browse files Browse the repository at this point in the history
Change-Id: I266c7c45501f91c04577d572870e858d2140b171
Signed-off-by: Egon Willighagen <egonw@users.sourceforge.net>
  • Loading branch information
johnmay authored and egonw committed Dec 12, 2012
1 parent 2f92c91 commit 5a5db14
Show file tree
Hide file tree
Showing 6 changed files with 31 additions and 42 deletions.
12 changes: 5 additions & 7 deletions src/main/org/openscience/cdk/layout/AtomPlacer.java
Expand Up @@ -74,15 +74,13 @@ public class AtomPlacer
*/
IAtomContainer molecule = null;

final Comparator ATOM_ORDER =
new Comparator()
final Comparator<IAtom> ATOM_ORDER =
new Comparator<IAtom>()
{
public int compare(Object o1, Object o2)
public int compare(IAtom o1, IAtom o2)
{
IAtom a1 = (IAtom) o1;
IAtom a2 = (IAtom) o2;
int i1 = ((Integer) a1.getProperty("Weight")).intValue();
int i2 = ((Integer) a2.getProperty("Weight")).intValue();
int i1 = ((Integer) o1.getProperty("Weight"));
int i2 = ((Integer) o1.getProperty("Weight"));
if (i1 < i2)
{
return -1;
Expand Down
Expand Up @@ -57,13 +57,13 @@
public class SimpleCycleBasis {

private List edgeList;
private List cycles;
private List<SimpleCycle> cycles;
private UndirectedGraph graph;

private boolean isMinimized = false;
private HashMap edgeIndexMap;

public SimpleCycleBasis (List cycles, List edgeList, UndirectedGraph graph) {
public SimpleCycleBasis (List<SimpleCycle> cycles, List edgeList, UndirectedGraph graph) {
this.edgeList = edgeList;
this.cycles = cycles;
this.graph = graph;
Expand All @@ -73,7 +73,7 @@ public SimpleCycleBasis (List cycles, List edgeList, UndirectedGraph graph) {


public SimpleCycleBasis (UndirectedGraph graph) {
this.cycles = new ArrayList();
this.cycles = new ArrayList<SimpleCycle>();
this.edgeList = new ArrayList();
this.graph = graph;

Expand Down Expand Up @@ -256,7 +256,7 @@ private void minimize(int startIndex) {
// Construct auxiliary graph gu
AuxiliaryGraph gu = new AuxiliaryGraph(graph, u);

SimpleCycle shortestCycle = (SimpleCycle) cycles.get(i);
SimpleCycle shortestCycle = cycles.get(i);

Iterator vertexIterator = graph.vertexSet().iterator();
while (vertexIterator.hasNext()) {
Expand Down Expand Up @@ -504,7 +504,7 @@ public Collection essentialCycles() {
}

if (isEssential) {
result.add((SimpleCycle)cycles.get(i));
result.add(cycles.get(i));
}

}
Expand Down Expand Up @@ -596,11 +596,11 @@ public Map relevantCycles() {

public List equivalenceClasses() {
int[] weight = weightVector();
Object[] cyclesArray = (Object[]) cycles.toArray();
Arrays.sort(cyclesArray, new Comparator() {
public int compare(Object o1, Object o2) {
return (int) (((SimpleCycle)o1).weight() - ((SimpleCycle)o2).weight());

SimpleCycle[] cyclesArray = cycles.toArray(new SimpleCycle[cycles.size()]);
Arrays.sort(cyclesArray, new Comparator<SimpleCycle>() {
public int compare(SimpleCycle o1, SimpleCycle o2) {
return (int) ((o1).weight() - (o2).weight());
}
});

Expand Down
6 changes: 3 additions & 3 deletions src/main/org/openscience/cdk/smiles/SmilesGenerator.java
Expand Up @@ -651,11 +651,11 @@ private List getCanNeigh(final IAtom a, final IAtomContainer container)
if (v.size() > 1)
{
Collections.sort(v,
new Comparator()
new Comparator<IAtom>()
{
public int compare(Object o1, Object o2)
public int compare(IAtom o1, IAtom o2)
{
return (int) ((Long) ((IAtom) o1).getProperty("CanonicalLable") - (Long) ((IAtom) o2).getProperty("CanonicalLable"));
return (int) ((Long) ( o1).getProperty("CanonicalLable") - (Long) (o2).getProperty("CanonicalLable"));
}
});
}
Expand Down
Expand Up @@ -220,17 +220,16 @@ private void postFilter() {
mappings = sortedMap;
}

private <K, V> Map<K, V> sortByValue(Map<K, V> map) {
List list = new LinkedList(map.entrySet());
Collections.sort(list, new Comparator() {
private <K, V extends Comparable<V>> Map<K, V> sortByValue(Map<K, V> map) {
List<Map.Entry<K,V>> list = new LinkedList<Map.Entry<K,V>>(map.entrySet());
Collections.sort(list, new Comparator<Map.Entry<K,V>>() {

public int compare(Object object1, Object object2) {
return ((Comparable) ((Map.Entry<K, V>) (object1)).getValue()).compareTo(((Map.Entry<K, V>) (object2)).getValue());
public int compare(Map.Entry<K,V> object1, Map.Entry<K,V> object2) {
return object1.getValue().compareTo(object2.getValue());
}
});
Map<K, V> result = new LinkedHashMap<K, V>();
for (Iterator it = list.iterator(); it.hasNext();) {
Map.Entry<K, V> entry = (Map.Entry<K, V>) it.next();
for (Map.Entry<K, V> entry : list) {
result.put(entry.getKey(), entry.getValue());
}
return result;
Expand Down
8 changes: 4 additions & 4 deletions src/main/org/openscience/cdk/smsd/ring/RingFilter.java
Expand Up @@ -67,7 +67,7 @@
public class RingFilter {

private RingFinder ringFinder;
private Comparator comparator;
private Comparator<List<?>> comparator;
private AtomMatcher filter;
private IAtomContainer mol;

Expand Down Expand Up @@ -115,13 +115,13 @@ private boolean ringMatches(List<IAtom> ring) {
return true;
}

private class RingSizeComparator implements Comparator {
private class RingSizeComparator implements Comparator<List<?>> {

/**
* {@inheritDoc}
*/
public int compare(Object o1, Object o2) {
return ((List) o1).size() - ((List) o2).size();
public int compare(List<?> o1, List<?> o2) {
return o1.size() - o2.size();
}
}
}
Expand Up @@ -96,17 +96,9 @@ public int compare(IAtomContainer o1, IAtomContainer o2) {
return 1;
if (o2 == null)
return -1;

// Check for correct instances
if (!(o1 instanceof IAtomContainer) && !(o2 instanceof IAtomContainer))
return 0;
if (!(o1 instanceof IAtomContainer))
return -1;
if (!(o2 instanceof IAtomContainer))
return 1;

IAtomContainer atomContainer1 = (IAtomContainer) o1;
IAtomContainer atomContainer2 = (IAtomContainer) o2;

IAtomContainer atomContainer1 = o1;
IAtomContainer atomContainer2 = o2;

// 1. Compare atom count
if (atomContainer1.getAtomCount() > atomContainer2.getAtomCount())
Expand Down

0 comments on commit 5a5db14

Please sign in to comment.