package nodebox.node;
import java.io.ByteArrayOutputStream;
import java.io.PrintStream;
import java.util.HashMap;
import java.util.Map;
import java.util.Set;
/**
* The processing context contains metadata about the processing operation.
*
* Note: the context is currently empty. Later on, we will add the frame number etc.
*/
public class ProcessingContext {
private HashMap valueMap = new HashMap();
private ByteArrayOutputStream outputBytes;
private ByteArrayOutputStream errorBytes;
private PrintStream outputStream;
private PrintStream errorStream;
private enum State {
UPDATING, PROCESSED
}
/**
* A list of parameters that have already been started updating.
* This set is used to detect cycles.
*/
private Map updatedParameters = new HashMap();
public ProcessingContext() {
put("FRAME", 1);
outputBytes = new ByteArrayOutputStream();
outputStream = new PrintStream(outputBytes);
errorBytes = new ByteArrayOutputStream();
errorStream = new PrintStream(errorBytes);
}
//// Map operations ////
public void put(String key, Object value) {
valueMap.put(key, value);
}
public Object get(String key) {
return valueMap.get(key);
}
public boolean containsKey(String key) {
return valueMap.containsKey(key);
}
public Set keySet() {
return valueMap.keySet();
}
//// Map shortcuts ////
public int getFrame() {
return (Integer) get("FRAME");
}
//// Output/error streams ////
public PrintStream getOutputStream() {
return outputStream;
}
public PrintStream getErrorStream() {
return errorStream;
}
public String getOutput() {
return outputBytes.toString();
}
public String getError() {
return errorBytes.toString();
}
// TODO: These are no longer used. Check and remove.
public void beginUpdating(Parameter parameter) {
State state = updatedParameters.get(parameter);
if (state == null) {
updatedParameters.put(parameter, State.UPDATING);
} else if (state == State.UPDATING) {
throw new AssertionError("You should check beforehand if a parameter was updating.");
} else if (state == State.PROCESSED) {
throw new RuntimeException("Parameter " + parameter + " has already been processed: infinite recursion detected.");
}
}
public void endUpdating(Parameter parameter) {
State state = updatedParameters.get(parameter);
if (state == null) {
throw new AssertionError("You should have called beginUpdating first.");
} else if (state == State.UPDATING) {
updatedParameters.put(parameter, State.PROCESSED);
} else if (state == State.PROCESSED) {
throw new AssertionError("You should only call endUpdating once.");
}
}
public boolean isUpdating(Parameter p) {
return updatedParameters.get(p) == State.UPDATING;
}
public boolean hasProcessed(Parameter p) {
return updatedParameters.get(p) == State.PROCESSED;
}
}