Repository navigation
Expand file tree
/
Copy pathProcessingContext.java
More file actions
112 lines (90 loc) · 3.19 KB
/
Copy pathProcessingContext.java
File metadata and controls
112 lines (90 loc) · 3.19 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
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.
* <p/>
* Note: the context is currently empty. Later on, we will add the frame number etc.
*/
public class ProcessingContext {
private HashMap<String, Object> valueMap = new HashMap<String, Object>();
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<Parameter, State> updatedParameters = new HashMap<Parameter, State>();
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<String> 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;
}
}