package edu.hawaii.ics.yucheng;
import java.util.ArrayList;
/**
* An abstract path that provides base functionality for graph solvers.
*/
abstract class GraphSolver {
/** A flag indicating if the solution is canceled. */
private boolean myCanceled;
/** A collection of listeners. */
private final ArrayList<GraphListener> myListeners;
/**
* Initializes a new instance of the class.
*/
public GraphSolver() {
myListeners = new ArrayList<GraphListener>();
}
/**
* Adds a graph listener.
*
* @param listener The listener.
*/
public void addGraphListener(final GraphListener listener) {
if (listener == null)
throw new NullPointerException("listener");
myListeners.add(listener);
}
/**
* Returns true if the solver has been canceled.
*
* @return True if the solver has been canceled.
*/
public boolean isCanceled() {
synchronized (this) {
return myCanceled;
}
}
/**
* Removes a graph listener.
*
* @param listener The graph listener.
*/
public void removeGraphListener(final GraphListener listener) {
if (listener == null)
throw new NullPointerException("listener");
myListeners.remove(listener);
}
/**
* Reports progress from the solver to the listeners.
*
* @param percent The percent complete.
*
* @param solution The solution.
*/
protected void reportProgress(final Integer percent,
final GraphSolution solution) {
for (final GraphListener listener : myListeners)
listener.progress(percent, solution);
}
/**
* Sets the canceled flag of the solver.
*
* @param canceled True if the solver is canceled.
*/
public void setCanceled(final boolean canceled) {
synchronized (this) {
myCanceled = canceled;
}
}
/**
* Solves a specified graph and returns the solution.
*
* @param graph The graph input.
*
* @return The solution.
*/
protected abstract GraphSolution solve(final Graph graph);
}