as an exercise I have been given the core of some functions and I have to implements those missing. We're working on Scheduler and Actions :
Action class :
public abstract class Action {
private ActionState state;
public Action() {
this.state = ActionState.READY;
}
/**
* make one step if the action is in state READY
* @throws ActionFinishedException if the state is FINISHED
*/
public void doStep() throws ActionFinishedException{
if (this.isFinished()) {
throw new ActionFinishedException("Action is finished");
}
if (this.state == ActionState.READY) {
this.state = ActionState.IN_PROGRESS;
}
this.makeOneStep();
if (this.stopCondition()) {
this.state = ActionState.FINISHED;
}
}
protected abstract void makeOneStep() throws ActionFinishedException;
protected abstract boolean stopCondition();
/**
* @return the state
*/
protected ActionState getState() {
return this.state;
}
/**
* @return true if the state is FINISHED, false otherwise
*/
public boolean isFinished() {
return this.state == ActionState.FINISHED;
}
}
Scheduler class :
public abstract class Scheduler extends Action {
protected List<Action> theActions;
public Scheduler() {
this.theActions = new ArrayList<Action>();
}
@Override
protected void makeOneStep() throws ActionFinishedException {
Action action = this.nextAction();
action.doStep();
if (action.isFinished()) {
this.removeAction(action);
}
}
protected List<Action> actions() {
return this.theActions;
}
public abstract void removeAction(Action action);
protected abstract Action nextAction();
public void addAction(Action action) throws ActionFinishedException, SchedulerStartedException {
if (this.getState() != ActionState.READY) {
throw new SchedulerStartedException("Can't add when scheduler is in progress");
}
if (action.isFinished()) {
throw new ActionFinishedException("Can't add an already finished action");
} else {
this.theActions.add(action);
}
}
@Override
protected boolean stopCondition() {
return this.theActions.isEmpty();
}
}
I'm having trouble implementing nextAction() since the signature that was given doesn't take any parameters I can't access the next element using .get(index+1) and creating an iterator seems like a lot for such a minor task
I'm implementing nextAction() in fairScheduler class :
public class FairScheduler extends Scheduler {
@Override
/** removes a given action from the scheduler
* @param action the action to remove
*/
public void removeAction(Action action) {
this.theActions.remove(action);
}
/** returns the nextAction in the scheduler,
* if the current action is the last element of the scheduler
* the first action of the scheduler is returned instead
*
* @return an Action, the next in the scheduler from given index
*/
@Override
protected Action nextAction() {
return null;
}
}