MolecularFormulaRangeManipulator { public static MolecularFormulaRange getRange(IMolecularFormulaSet mfSet) { MolecularFormulaRange mfRange = new MolecularFormulaRange(); for (IMolecularFormula mf : mfSet.molecularFormulas()) { for (IIsotope isotope : mf.isotopes()) { int occur_new = mf.getIsotopeCount(isotope); if (!mfRange.contains(isotope)) { mfRange.addIsotope(isotope, occur_new, occur_new); } else { int occur_old_Max = mfRange.getIsotopeCountMax(isotope); int occur_old_Min = mfRange.getIsotopeCountMin(isotope); if (occur_new > occur_old_Max) { mfRange.removeIsotope(isotope); mfRange.addIsotope(isotope, occur_old_Min, occur_new); } else if (occur_new < occur_old_Min) { mfRange.removeIsotope(isotope); mfRange.addIsotope(isotope, occur_new, occur_old_Max); } } } } for (IMolecularFormula mf : mfSet.molecularFormulas()) { if (mf.getIsotopeCount() != mfRange.getIsotopeCount()) { for (IIsotope isotope : mfRange.isotopes()) { if (!mf.contains(isotope)) { int occurMax = mfRange.getIsotopeCountMax(isotope); mfRange.addIsotope(isotope, 0, occurMax); } } } } return mfRange; }  static MolecularFormulaRange getRange(IMolecularFormulaSet mfSet); static IMolecularFormula getMaximalFormula(MolecularFormulaRange mfRange, IChemObjectBuilder builder); static IMolecularFormula getMinimalFormula(MolecularFormulaRange mfRange, IChemObjectBuilder builder);  }
@Test public void testGetRange_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); MolecularFormulaRange mfRange = MolecularFormulaRangeManipulator.getRange(mfSet); Assert.assertEquals(4, mfRange.getIsotopeCount()); Assert.assertEquals(4, mfRange.getIsotopeCountMin(builder.newInstance(IIsotope.class, "C"))); Assert.assertEquals(9, mfRange.getIsotopeCountMax(builder.newInstance(IIsotope.class, "C"))); Assert.assertEquals(5, mfRange.getIsotopeCountMin(builder.newInstance(IIsotope.class, "H"))); Assert.assertEquals(20, mfRange.getIsotopeCountMax(builder.newInstance(IIsotope.class, "H"))); Assert.assertEquals(0, mfRange.getIsotopeCountMin(builder.newInstance(IIsotope.class, "N"))); Assert.assertEquals(4, mfRange.getIsotopeCountMax(builder.newInstance(IIsotope.class, "N"))); Assert.assertEquals(2, mfRange.getIsotopeCountMin(builder.newInstance(IIsotope.class, "O"))); Assert.assertEquals(7, mfRange.getIsotopeCountMax(builder.newInstance(IIsotope.class, "O"))); }