class MassToFormulaTool {

    public IMolecularFormulaSet generate(double mass) {

        if (mass <= 0.0) {
            logger.error("Proposed mass is not valid: ", mass);
            return null;
        }
        IMolecularFormula minimalMF = MolecularFormulaRangeManipulator.getMinimalFormula(mfRange, builder);
        IMolecularFormula maximalMF = MolecularFormulaRangeManipulator.getMaximalFormula(mfRange, builder);
        double massMim = MolecularFormulaManipulator.getTotalExactMass(minimalMF) - tolerance;
        double massMap = MolecularFormulaManipulator.getTotalExactMass(maximalMF) + tolerance;
        if (massMim > mass || massMap < mass) {
            logger.error("Proposed mass is out of the range: ", mass);
            return null;
        }

        IMolecularFormulaSet molecularFormulaSet = builder.newInstance(IMolecularFormulaSet.class);

        int[][] matrix = this.matrix_Base;
        int numberElements = mfRange.getIsotopeCount();

        // put IIsotope into a list
        List<IIsotope> isotopes_TO = new ArrayList<IIsotope>();
        Iterator<IIsotope> isIt = mfRange.isotopes().iterator();
        while (isIt.hasNext())
            isotopes_TO.add(isIt.next());

        isotopes_TO = orderList(isotopes_TO);

        for (int i = 0; i < matrix.length; i++) {

            /* constructing initial combinations */
            int[] value_In = new int[numberElements];
            for (int j = 0; j < numberElements; j++) {
                if (matrix[i][j] == 0)
                    value_In[j] = 0;
                else
                    value_In[j] = 1;
            }

            /* find number of element to combine */
            int count_E = 0;
            ArrayList<Integer> elem_Pos = new ArrayList<Integer>();
            for (int j = 0; j < matrix[1].length; j++)
                if (value_In[j] != 0) {
                    count_E++;
                    elem_Pos.add(j);
                }

            boolean flag = true;
            /* first position those first starting at the left */
            int possChan = 0;
            String lastMFString = "";
            while (flag) {

                //				// print all combinations --------------------------------------------------
                //				System.out.print(elem_Pos.get(possChan).intValue()+">");
                //				for(int j= 0 ; j< matrix[1].length; j++)
                //					System.out.print(isotopes_TO.get(j).getSymbol()+value_In[j]+"-");
                //				System.out.println();
                //				// print all combinations --------------------------------------------------

                // control if some of the element is contained. E.g. C(1-3)H(1-3)
                // the matrix 01 or 10 can not exist
                boolean flagBreak = false;
                for (int j = 0; j < matrix[1].length; j++) {
                    int min = mfRange.getIsotopeCountMin(isotopes_TO.get(j));
                    if (value_In[j] == 0) if (min != 0) flagBreak = true;
                }
                if (flagBreak) break;

                /*
                 * Find max occurence given a mass for a element with minimal
                 * elements
                 */
                int occurence = getMaxOccurence(mass, elem_Pos.get(possChan).intValue(), value_In, isotopes_TO);

                /* at least one */
                if (occurence == 0) break;

                int maxx = mfRange.getIsotopeCountMax(isotopes_TO.get(elem_Pos.get(possChan).intValue()));
                int minn = mfRange.getIsotopeCountMin(isotopes_TO.get(elem_Pos.get(possChan).intValue()));

                /* restriction of the number of max and min number for a element */
                if (occurence < minn | maxx < occurence) {
                    /*
                     * when is not in the occurrence that means that we have to
                     * restart one value to the predecessor.
                     */

                    if (possChan < elem_Pos.size() - 1) {
                        /* Means that is possible to fit the next */
                        if (maxx < occurence) value_In[elem_Pos.get(possChan).intValue()] = maxx;
                        possChan++;

                    } else {
                        boolean foundZ = false;
                        for (int z = possChan - 1; z >= 0; z--) {
                            if (value_In[elem_Pos.get(z).intValue()] != 1) {
                                possChan = z;
                                foundZ = true;
                                int newValue = value_In[elem_Pos.get(possChan).intValue()] - 1;

                                value_In[elem_Pos.get(possChan).intValue()] = newValue;
                                for (int j = possChan + 1; j < elem_Pos.size(); j++) {
                                    int p = elem_Pos.get(j).intValue();
                                    value_In[p] = 1;
                                }
                                possChan++;
                                break;
                            }
                        }
                        if (!foundZ) break;

                    }

                    continue;
                } /* final not occurrence */

                /* set the occurrence into the matrix */
                value_In[elem_Pos.get(possChan).intValue()] = occurence;

                double massT = calculateMassT(isotopes_TO, value_In);
                double diff_new = Math.abs(mass - (massT));

                if (diff_new < tolerance) {
                    IMolecularFormula myMF = getFormula(isotopes_TO, value_In);
                    String newMFString = MolecularFormulaManipulator.getString(myMF);

                    if (!newMFString.equals(lastMFString)) {
                        molecularFormulaSet.addMolecularFormula(myMF);
                        lastMFString = newMFString;
                    }
                }

                if (count_E == 1) /* only valid for the first random 1000 */
                break;

                if (possChan < elem_Pos.size() - 1) {
                    /* Means that is possible to fit the next */
                    //					value_In[elem_Pos.get(possChan).intValue()] = maxx;
                    possChan++;

                } else {
                    boolean foundZ = false;
                    for (int z = possChan - 1; z >= 0; z--) {
                        if (value_In[elem_Pos.get(z).intValue()] != 1) {
                            possChan = z;
                            foundZ = true;
                            int newValue = value_In[elem_Pos.get(possChan).intValue()] - 1;

                            value_In[elem_Pos.get(possChan).intValue()] = newValue;
                            for (int j = possChan + 1; j < elem_Pos.size(); j++) {
                                int p = elem_Pos.get(j).intValue();
                                value_In[p] = 1;
                            }
                            possChan++;
                            break;
                        }
                    }
                    if (!foundZ) break;

                }
            }

        }

        return returnOrdered(mass, molecularFormulaSet);
    }

    public  MassToFormulaTool(IChemObjectBuilder builder);

}