code: public synchronized IMolecularFormula getNextFormula() {
  
          // Main cycle iterating through element counters
          mainCycle: while (searchRunning) {
  
              double currentMass = calculateCurrentMass();
  
              // Heuristics: if we are over the mass, it is meaningless to add
              // more atoms, so let's jump directly to the maximum count at the
              // position we increased last time.
              if (currentMass > maxMass) {
                  // Keep a lock on the currentCounts, because
                  // getFinishedPercentage() might be called from another
                  // thread
                  synchronized (currentCounts) {
                      currentCounts[lastIncreasedPosition] = maxCounts[lastIncreasedPosition];
                      increaseCounter(lastIncreasedPosition);
                  }
                  continue mainCycle;
              }
  
              // If the current formula mass fits in the target mass range, let's
              // return it
              if ((currentMass >= minMass) && (currentMass <= maxMass)) {
                  IMolecularFormula cdkFormula = generateFormulaObject();
                  increaseCounter(0);
                  return cdkFormula;
              }
  
              // Increase the counter
              increaseCounter(0);
          }
  
          // All combinations tested, return null
          return null;
  
      }
docs: Returns next generated formula or null in case no new formula was found
  (search is finished). There is no guaranteed order in which the formulas
  are generated.