rendered paste bodypackage patterns.request;
import java.lang.IllegalStateException;
abstract public class GenericRequest {
boolean _wasExecuted = false;
abstract public Object getReturn() throws IllegalStateException;
public final void execute() throws ExecutionFailedException {
// implements lazy execution
if (!wasExecuted()) {
enterExecutedState();
}
}
public final void unexecute() throws UnexecutionFailedException {
if (wasExecuted()) {
enterUnexecutedState();
}
}
public boolean supportsUnexecution() { return false;}
public final Object evaluate() throws ExecutionFailedException, IllegalStateException {
execute();
return getReturn();
}
protected void enterExecutedState() throws ExecutionFailedException {
_wasExecuted = true;
}
protected void enterUnexecutedState() throws UnexecutionFailedException {
if (!supportsExecution()) throw new UnsupportedOperationException();
else _wasExecuted = false;
}
public boolean wasExecuted() {
return _wasExecuted;
}
}
Example implementation:
import patterns.request.*;
import hep.aida.ITree;
import java.util.HashMap;
public class AidaTreeRequest extends GenericRequest {
private static HashMap<Object, ITree> _treeMap;
Object _caller;
ITree _cachedTree;
public AidaTreeRequest() {
super(); init(null);
}
public AidaTreeRequest(Object caller) {
super(); init(caller);
}
private void init(Object caller) {
_caller = caller;
}
public ITree getReturn() throws IllegalStateException {
return getCachedTree();
}
protected void enterExecutedState() throws IllegalStateException {
if (wasExecuted()) throw new IllegalStateException();
}
// accessor methods
protected Object getCaller() { return _caller;}
private Object getTreeMap() { return _treeMap;}
protected static void cacheTree(Object caller, ITree tree) {
getTreeMap().put(caller, tree);
}
}