SimpleAtomComparator implements Comparator<IAtom>, Serializable { @Override public int compare(IAtom o1, IAtom o2) { if (o1.getSymbol().equalsIgnoreCase(o2.getSymbol())) { if (o1.getHybridization() != null && o2.getHybridization() != null) { return o1.getHybridization().compareTo(o2.getHybridization()); } return 0; } return 10 * o1.getSymbol().compareToIgnoreCase(o2.getSymbol()); }  @Override int compare(IAtom o1, IAtom o2);  }
@Test public void testCompare_NullHybridization() throws Exception { SimpleAtomComparator comparator = new SimpleAtomComparator(); IAtom a1 = builder.newInstance(IAtom.class, "C"); IAtom a2 = builder.newInstance(IAtom.class, "C"); Assert.assertEquals("Null hybridzation should be equals", 0, comparator.compare(a1, a2)); }
@Test public void testCompare_SameHybridization() throws Exception { SimpleAtomComparator comparator = new SimpleAtomComparator(); IAtom a1 = builder.newInstance(IAtom.class, "C"); IAtom a2 = builder.newInstance(IAtom.class, "C"); a1.setHybridization(IAtomType.Hybridization.SP3); a2.setHybridization(IAtomType.Hybridization.SP3); Assert.assertEquals("Same hybridzation should be equals", 0, comparator.compare(a1, a2)); }
@Test public void testCompare_DifferentHybridization() throws Exception { SimpleAtomComparator comparator = new SimpleAtomComparator(); IAtom a1 = builder.newInstance(IAtom.class, "C"); IAtom a2 = builder.newInstance(IAtom.class, "C"); a1.setHybridization(IAtomType.Hybridization.SP2); a2.setHybridization(IAtomType.Hybridization.SP3); Assert.assertEquals("Atom 2 should have priority", -1, comparator.compare(a1, a2)); }
@Test public void testCompare_DifferentSymbol() throws Exception { SimpleAtomComparator comparator = new SimpleAtomComparator(); IAtom a1 = builder.newInstance(IAtom.class, "C"); IAtom a2 = builder.newInstance(IAtom.class, "O"); Assert.assertTrue("oxygen should rank above carbon", comparator.compare(a1, a2) < 0); Assert.assertTrue("oxygen should rank above carbon (inverse)", comparator.compare(a2, a1) > 0); }