001package algs13.xbacktrack.xframework;
002
003import algs13.Stack;
004
005import java.util.Iterator;
006
007/**
008 * A successful solution to a backtracking problem. The choices
009 * are provided in a stack, in reverse order from which they were
010 * determined.
011 *
012 * @param <T> The type of the choices made in the backtracking problem.
013 */
014public final class XBacktrackSuccess<T> implements XBacktrackResult {
015    private final Stack<T> choices;
016    public XBacktrackSuccess(Stack<T> choices) {
017        this.choices = choices;
018    }
019
020    @Override
021    public boolean isSuccess() {
022        return true;
023    }
024
025    @Override
026    public String toString() {
027        StringBuilder builder = new StringBuilder();
028        builder.append("Solution: [ ");
029        Iterator<T> it = choices.iterator();
030        while(it.hasNext()) {
031            builder.append(it.next());
032            if(it.hasNext()) {
033                builder.append(", ");
034            }
035        }
036        builder.append(" ]");
037        return builder.toString();
038    }
039}