MolecularFormulaSetManipulator { public static IMolecularFormula getMaxOccurrenceElements(IMolecularFormulaSet mfSet) { IMolecularFormula molecularFormula = mfSet.getBuilder().newInstance(IMolecularFormula.class); for (IMolecularFormula mf : mfSet.molecularFormulas()) { for (IIsotope isotope : mf.isotopes()) { IElement element = mfSet.getBuilder().newInstance(IElement.class, isotope); int occur_new = MolecularFormulaManipulator.getElementCount(mf, element); if (!MolecularFormulaManipulator.containsElement(molecularFormula, element)) { molecularFormula.addIsotope(mfSet.getBuilder().newInstance(IIsotope.class, element), occur_new); } else { int occur_old = MolecularFormulaManipulator.getElementCount(molecularFormula, element); if (occur_new > occur_old) { MolecularFormulaManipulator.removeElement(molecularFormula, element); molecularFormula.addIsotope(mfSet.getBuilder().newInstance(IIsotope.class, element), occur_new); } } } } return molecularFormula; }  static IMolecularFormula getMaxOccurrenceElements(IMolecularFormulaSet mfSet); static IMolecularFormula getMinOccurrenceElements(IMolecularFormulaSet mfSet); static IMolecularFormulaSet remove(IMolecularFormulaSet formulaSet, IMolecularFormula formulaMin,
            IMolecularFormula formulaMax); static boolean contains(IMolecularFormulaSet formulaSet, IMolecularFormula formula); static IMolecularFormulaSet remove(IMolecularFormulaSet formulaSet, MolecularFormulaRange formulaRange);  }
@Test public void testGetMaxOccurrenceElements_IMolecularFormulaSet() { IMolecularFormula mf1 = new MolecularFormula(); mf1.addIsotope(builder.newInstance(IIsotope.class, "C"), 4); mf1.addIsotope(builder.newInstance(IIsotope.class, "H"), 12); mf1.addIsotope(builder.newInstance(IIsotope.class, "N"), 1); mf1.addIsotope(builder.newInstance(IIsotope.class, "O"), 4); IMolecularFormula mf2 = new MolecularFormula(); mf2.addIsotope(builder.newInstance(IIsotope.class, "C"), 7); mf2.addIsotope(builder.newInstance(IIsotope.class, "H"), 20); mf2.addIsotope(builder.newInstance(IIsotope.class, "N"), 4); mf2.addIsotope(builder.newInstance(IIsotope.class, "O"), 2); IMolecularFormula mf3 = new MolecularFormula(); mf3.addIsotope(builder.newInstance(IIsotope.class, "C"), 9); mf3.addIsotope(builder.newInstance(IIsotope.class, "H"), 5); mf3.addIsotope(builder.newInstance(IIsotope.class, "O"), 7); IMolecularFormulaSet mfSet = new MolecularFormulaSet(); mfSet.addMolecularFormula(mf1); mfSet.addMolecularFormula(mf2); mfSet.addMolecularFormula(mf3); IMolecularFormula molecularFormula = MolecularFormulaSetManipulator.getMaxOccurrenceElements(mfSet); Assert.assertEquals(40, MolecularFormulaManipulator.getAtomCount(molecularFormula)); Assert.assertEquals(4, molecularFormula.getIsotopeCount()); Assert.assertEquals(9, molecularFormula.getIsotopeCount(builder.newInstance(IIsotope.class, "C"))); Assert.assertEquals(20, molecularFormula.getIsotopeCount(builder.newInstance(IIsotope.class, "H"))); Assert.assertEquals(4, molecularFormula.getIsotopeCount(builder.newInstance(IIsotope.class, "N"))); Assert.assertEquals(7, molecularFormula.getIsotopeCount(builder.newInstance(IIsotope.class, "O"))); }