class MolecularFormulaSetManipulator {

    public static IMolecularFormulaSet remove(IMolecularFormulaSet formulaSet, IMolecularFormula formulaMin,
            IMolecularFormula formulaMax) {

        // prove the correlation between maximum and minimum molecularFormula
        if (!validCorrelation(formulaMin, formulaMax)) return null;

        IMolecularFormulaSet newFormulaSet = formulaSet.getBuilder().newInstance(IMolecularFormulaSet.class);

        for (IMolecularFormula formula : formulaSet.molecularFormulas()) {
            boolean flagPass = true;

            // the formula must contain all element found into the formulaMin
            if (!validCorrelation(formula, formulaMin)) continue;

            for (IElement element : MolecularFormulaManipulator.elements(formulaMin)) {
                int occur = MolecularFormulaManipulator.getElementCount(formula, element);
                int occurMax = MolecularFormulaManipulator.getElementCount(formulaMax, element);
                int occurMin = MolecularFormulaManipulator.getElementCount(formulaMin, element);

                if (!(occurMin <= occur) || !(occur <= occurMax)) {
                    flagPass = false;
                    break;
                }

            }
            if (flagPass) // stored if each IElement occurrence is into the limits
                newFormulaSet.addMolecularFormula(formula);

        }
        return newFormulaSet;
    }

}