-
Notifications
You must be signed in to change notification settings - Fork 7
/
Container.java
320 lines (246 loc) · 9.82 KB
/
Container.java
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
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
package com.bladecoder.ink.runtime;
import com.bladecoder.ink.runtime.Path.Component;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map.Entry;
public class Container extends RTObject implements INamedContent {
private String name;
private final List<RTObject> content;
private HashMap<String, INamedContent> namedContent;
private boolean visitsShouldBeCounted;
private boolean turnIndexShouldBeCounted;
private boolean countingAtStartOnly;
public Container() {
content = new ArrayList<>();
setNamedContent(new HashMap<>());
}
@Override
public String getName() {
return name;
}
public void setName(String value) {
name = value;
}
public List<RTObject> getContent() {
return content;
}
public HashMap<String, INamedContent> getNamedContent() {
return namedContent;
}
public void setNamedContent(HashMap<String, INamedContent> value) {
namedContent = value;
}
public HashMap<String, RTObject> getNamedOnlyContent() {
HashMap<String, RTObject> namedOnlyContentDict = new HashMap<String, RTObject>();
for (Entry<String, INamedContent> kvPair : getNamedContent().entrySet()) {
namedOnlyContentDict.put(kvPair.getKey(), (RTObject) kvPair.getValue());
}
for (RTObject c : getContent()) {
INamedContent named = c instanceof INamedContent ? (INamedContent) c : null;
if (named != null && named.hasValidName()) {
namedOnlyContentDict.remove(named.getName());
}
}
return namedOnlyContentDict;
}
public void setNamedOnlyContent(HashMap<String, RTObject> value) {
HashMap<String, RTObject> existingNamedOnly = getNamedOnlyContent();
for (Entry<String, RTObject> kvPair : existingNamedOnly.entrySet()) {
getNamedContent().remove(kvPair.getKey());
}
for (Entry<String, RTObject> kvPair : value.entrySet()) {
INamedContent named = kvPair.getValue() instanceof INamedContent
? (INamedContent) kvPair.getValue()
: (INamedContent) null;
if (named != null) addToNamedContentOnly(named);
}
}
public boolean getVisitsShouldBeCounted() {
return visitsShouldBeCounted;
}
public void setVisitsShouldBeCounted(boolean value) {
visitsShouldBeCounted = value;
}
public boolean getTurnIndexShouldBeCounted() {
return turnIndexShouldBeCounted;
}
public void setTurnIndexShouldBeCounted(boolean value) {
turnIndexShouldBeCounted = value;
}
public boolean getCountingAtStartOnly() {
return countingAtStartOnly;
}
public void setCountingAtStartOnly(boolean value) {
countingAtStartOnly = value;
}
public static final int COUNTFLAGS_VISITS = 1;
public static final int COUNTFLAGS_TURNS = 2;
public static final int COUNTFLAGS_COUNTSTARTONLY = 4;
public int getCountFlags() {
int flags = 0;
if (getVisitsShouldBeCounted()) flags |= COUNTFLAGS_VISITS;
if (getTurnIndexShouldBeCounted()) flags |= COUNTFLAGS_TURNS;
if (getCountingAtStartOnly()) flags |= COUNTFLAGS_COUNTSTARTONLY;
// If we're only storing CountStartOnly, it serves no purpose,
// since it's dependent on the other two to be used at all.
// (e.g. for setting the fact that *if* a gather or choice's
// content is counted, then is should only be counter at the start)
// So this is just an optimisation for storage.
if (flags == COUNTFLAGS_COUNTSTARTONLY) {
flags = 0;
}
return flags;
}
public void setCountFlags(int value) {
if ((value & COUNTFLAGS_VISITS) > 0) setVisitsShouldBeCounted(true);
if ((value & COUNTFLAGS_TURNS) > 0) setTurnIndexShouldBeCounted(true);
if ((value & COUNTFLAGS_COUNTSTARTONLY) > 0) setCountingAtStartOnly(true);
}
@Override
public boolean hasValidName() {
return getName() != null && !getName().isEmpty();
}
public void addContents(List<RTObject> contentList) throws Exception {
for (RTObject c : contentList) {
addContent(c);
}
}
private void addContent(RTObject contentObj) throws Exception {
getContent().add(contentObj);
if (contentObj.getParent() != null) {
throw new Exception("content is already in " + contentObj.getParent());
}
contentObj.setParent(this);
tryAddNamedContent(contentObj);
}
private void tryAddNamedContent(RTObject contentObj) {
INamedContent namedContentObj = contentObj instanceof INamedContent ? (INamedContent) contentObj : null;
if (namedContentObj != null && namedContentObj.hasValidName()) {
addToNamedContentOnly(namedContentObj);
}
}
public void addToNamedContentOnly(INamedContent namedContentObj) {
// Debug.Assert(namedContentObj instanceof RTObject, "Can only add
// Runtime.RTObjects to a Runtime.Container");
RTObject runtimeObj = (RTObject) namedContentObj;
runtimeObj.setParent(this);
getNamedContent().put(namedContentObj.getName(), namedContentObj);
}
private RTObject contentWithPathComponent(Path.Component component) {
if (component.isIndex()) {
if (component.getIndex() >= 0 && component.getIndex() < getContent().size()) {
return getContent().get(component.getIndex());
} else {
return null;
}
} else if (component.isParent()) {
// When path is out of range, quietly return nil
// (useful as we step/increment forwards through content)
return this.getParent();
} else {
INamedContent foundContent = getNamedContent().get(component.getName());
if (foundContent != null) {
return (RTObject) foundContent;
} else {
return null;
}
}
}
public SearchResult contentAtPath(Path path) {
return contentAtPath(path, 0, -1);
}
public SearchResult contentAtPath(Path path, int partialPathStart, int partialPathLength) {
if (partialPathLength == -1) partialPathLength = path.getLength();
SearchResult result = new SearchResult();
result.approximate = false;
Container currentContainer = this;
RTObject currentObj = this;
for (int i = partialPathStart; i < partialPathLength; ++i) {
Component comp = path.getComponent(i);
// Path component was wrong type
if (currentContainer == null) {
result.approximate = true;
break;
}
RTObject foundObj = currentContainer.contentWithPathComponent(comp);
// Couldn't resolve entire path?
if (foundObj == null) {
result.approximate = true;
break;
}
currentObj = foundObj;
currentContainer = foundObj instanceof Container ? (Container) foundObj : null;
}
result.obj = currentObj;
return result;
}
private static final int spacesPerIndent = 4;
private void appendIndentation(StringBuilder sb, int indentation) {
for (int i = 0; i < spacesPerIndent * indentation; ++i) {
sb.append(" ");
}
}
public void buildStringOfHierarchy(StringBuilder sb, int indentation, RTObject pointedObj) {
appendIndentation(sb, indentation);
sb.append("[");
if (this.hasValidName()) {
sb.append(" ({");
sb.append(this.getName());
sb.append("})");
}
if (this == pointedObj) {
sb.append(" <---");
}
sb.append("\n");
indentation++;
for (int i = 0; i < getContent().size(); ++i) {
RTObject obj = getContent().get(i);
if (obj instanceof Container) {
Container container = (Container) obj;
container.buildStringOfHierarchy(sb, indentation, pointedObj);
} else {
appendIndentation(sb, indentation);
if (obj instanceof StringValue) {
sb.append("\"");
sb.append(obj.toString().replace("\n", "\\n"));
sb.append("\"");
} else {
sb.append(obj.toString());
}
}
if (i != getContent().size() - 1) {
sb.append(",");
}
if (!(obj instanceof Container) && obj == pointedObj) {
sb.append(" <---");
}
sb.append("\n");
}
HashMap<String, INamedContent> onlyNamed = new HashMap<>();
for (Entry<String, INamedContent> objKV : getNamedContent().entrySet()) {
if (!getContent().contains(objKV.getValue())) {
onlyNamed.put(objKV.getKey(), objKV.getValue());
}
}
if (!onlyNamed.isEmpty()) {
appendIndentation(sb, indentation);
sb.append("-- named: --\n");
for (Entry<String, INamedContent> objKV : onlyNamed.entrySet()) {
// Debug.Assert(objKV.Value instanceof Container, "Can only
// print out named Containers");
Container container = (Container) objKV.getValue();
container.buildStringOfHierarchy(sb, indentation, pointedObj);
sb.append("\n");
}
}
indentation--;
appendIndentation(sb, indentation);
sb.append("]");
}
public String buildStringOfHierarchy() {
StringBuilder sb = new StringBuilder();
buildStringOfHierarchy(sb, 0, null);
return sb.toString();
}
}