AtomContainerDiff { public static IDifference difference(IChemObject first, IChemObject second) { if (!(first instanceof IAtomContainer && second instanceof IAtomContainer)) { return null; } IAtomContainer firstAC = (IAtomContainer) first; IAtomContainer secondAC = (IAtomContainer) second; ChemObjectDifference totalDiff = new ChemObjectDifference("AtomContainerDiff"); totalDiff.addChild(IntegerDifference.construct("atomCount", firstAC.getAtomCount(), secondAC.getAtomCount())); if (firstAC.getAtomCount() == secondAC.getAtomCount()) { for (int i = 0; i < firstAC.getAtomCount(); i++) { totalDiff.addChild(AtomDiff.difference(firstAC.getAtom(i), secondAC.getAtom(i))); } } totalDiff.addChild(IntegerDifference.construct("electronContainerCount", firstAC.getElectronContainerCount(), secondAC.getElectronContainerCount())); if (firstAC.getElectronContainerCount() == secondAC.getElectronContainerCount()) { for (int i = 0; i < firstAC.getElectronContainerCount(); i++) { if (firstAC.getElectronContainer(i) instanceof IBond && secondAC.getElectronContainer(i) instanceof IBond) { totalDiff.addChild(BondDiff.difference(firstAC.getElectronContainer(i), secondAC.getElectronContainer(i))); } else if (firstAC.getElectronContainer(i) instanceof ILonePair && secondAC.getElectronContainer(i) instanceof ILonePair) { totalDiff.addChild(LonePairDiff.difference(firstAC.getElectronContainer(i), secondAC.getElectronContainer(i))); } else if (firstAC.getElectronContainer(i) instanceof ISingleElectron && secondAC.getElectronContainer(i) instanceof ISingleElectron) { totalDiff.addChild(SingleElectronDiff.difference(firstAC.getElectronContainer(i), secondAC.getElectronContainer(i))); } else { totalDiff.addChild(ElectronContainerDiff.difference(firstAC.getElectronContainer(i), secondAC.getElectronContainer(i))); } } } totalDiff.addChild(ChemObjectDiff.difference(first, second)); if (totalDiff.childCount() > 0) { return totalDiff; } else { return null; } } private  AtomContainerDiff(); static String diff(IChemObject first, IChemObject second); static IDifference difference(IChemObject first, IChemObject second);  }
@Test public void testDifference() { IAtom carbon = mock(IAtom.class); IAtom oxygen = mock(IAtom.class); when(carbon.getSymbol()).thenReturn("C"); when(oxygen.getSymbol()).thenReturn("O"); IBond b1 = mock(IBond.class); IBond b2 = mock(IBond.class); when(b1.getOrder()).thenReturn(IBond.Order.SINGLE); when(b2.getOrder()).thenReturn(IBond.Order.DOUBLE); when(b1.getAtomCount()).thenReturn(2); when(b2.getAtomCount()).thenReturn(2); when(b1.getBegin()).thenReturn(carbon); when(b1.getEnd()).thenReturn(carbon); when(b2.getBegin()).thenReturn(carbon); when(b2.getEnd()).thenReturn(oxygen); IAtomContainer container1 = mock(IAtomContainer.class); IAtomContainer container2 = mock(IAtomContainer.class); when(container1.getElectronContainerCount()).thenReturn(1); when(container2.getElectronContainerCount()).thenReturn(1); when(container1.getElectronContainer(0)).thenReturn(b1); when(container2.getElectronContainer(0)).thenReturn(b2); String result = AtomContainerDiff.diff(container1, container2); Assert.assertNotNull(result); }