javax.constraints.impl.search.SolutionIterator Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of jsr331-sugar Show documentation
Show all versions of jsr331-sugar Show documentation
This is a JSR331 interface for the open source Java constraint programming library "Sugar" v. 2.1.3
package javax.constraints.impl.search;
import javax.constraints.Solution;
/**
* This is a simple implementation of the SolutionIterator interface
* that allows a user to search and to iterate through multiple solutions.
* The expected use:
*
*
* SolutionIterator iter = solver.solutionIterator();
* while(iter.hasNext()) {
* Solution solution = iter.next();
* ...
* }
*
*
*/
public class SolutionIterator implements javax.constraints.SolutionIterator {
Solver solver;
Solution solution;
int solutionNumber;
boolean noSolutions;
public SolutionIterator(javax.constraints.Solver solver) {
this.solver = (Solver)solver;
solution = null;
noSolutions = false;
solutionNumber = 0;
}
@Override
public boolean hasNext() {
if (noSolutions)
return false;
solution = null;
if (solutionNumber == 0) {
solution = solver.findSolution();
} else {
solution = solver.findNextSolution();
}
if (solution != null) {
solutionNumber++;
return true;
} else
return false;
}
@Override
public Solution next() {
if (solution == null)
throw new RuntimeException("Cannot use SolutionIterator.next() before checking the hasNext() returned true");
return solution;
}
}