MolecularFormulaChecker { public Double isValidSum(IMolecularFormula formula) { double result = 1.0; IMolecularFormula formulaWith = isValid(formula); Map<Object, Object> properties = formulaWith.getProperties(); Iterator<IRule> iterRules = rules.iterator(); while (iterRules.hasNext()) { result *= (Double) properties.get(iterRules.next().getClass()); } return result; } MolecularFormulaChecker(List<IRule> rules); List<IRule> getRules(); Double isValidSum(IMolecularFormula formula); IMolecularFormula isValid(IMolecularFormula formula);  }
@Test public void testIsValidSum_IMolecularFormula() { IMolecularFormula formula = new MolecularFormula(); formula.addIsotope(ifac.getMajorIsotope("C"), 1); formula.addIsotope(ifac.getMajorIsotope("H"), 100); List<IRule> rules = new ArrayList<IRule>(); rules.add(new MMElementRule()); MolecularFormulaChecker MFChecker = new MolecularFormulaChecker(rules); Assert.assertEquals(0.0, MFChecker.isValidSum(formula), 0.001); }
@Test public void testIsValid_IMolecularFormula() { IMolecularFormula formula = new MolecularFormula(); formula.addIsotope(ifac.getMajorIsotope("C"), 1); formula.addIsotope(ifac.getMajorIsotope("H"), 100); formula.setCharge(0); List<IRule> rules = new ArrayList<IRule>(); rules.add(new MMElementRule()); rules.add(new ChargeRule()); MolecularFormulaChecker MFChecker = new MolecularFormulaChecker(rules); Assert.assertEquals(0.0, MFChecker.isValidSum(formula), 0.001); }
@Test public void testIsValidSum_True_2Rules() { IMolecularFormula formula = new MolecularFormula(); formula.addIsotope(ifac.getMajorIsotope("C"), 1); formula.addIsotope(ifac.getMajorIsotope("H"), 4); formula.setCharge(0); List<IRule> rules = new ArrayList<IRule>(); rules.add(new MMElementRule()); rules.add(new ChargeRule()); MolecularFormulaChecker MFChecker = new MolecularFormulaChecker(rules); Assert.assertEquals(1.0, MFChecker.isValidSum(formula), 0.001); }