class 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();

}