public synchronized final void schedule(Algorithm alg, ServiceReference ref, Calendar time) {
AlgorithmTask task = this.tasksByAlgorithms.get(alg);
// If alg already exists, do some checks...
if (task != null) {
AlgorithmState state = task.getState();
// If its still running, we can't schedule it again.
if (state.equals(AlgorithmState.RUNNING)) {
throw new RuntimeException(
"Cannot schedule running algorithm. Check state of algorithm first.");
}
// If its new or waiting to run, we refuse to schedule it to force
// user to explicitly
// cancel and reschedule.
else if (state.equals(AlgorithmState.NEW)) {
throw new RuntimeException(
"Algorithm is already scheduled to run. Cancel existing schedule first.");
}
else if (state.equals(AlgorithmState.STOPPED)) {
// If it was stopped but not cleaned up yet, clean it up
purgeFinished();
}
else {
throw new IllegalStateException(