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 package ca.uhn.hl7v2.parser;
28
29 import java.util.ArrayList;
30 import java.util.HashSet;
31 import java.util.Set;
32
33
34
35
36
37
38
39 public class StructureDefinition implements IStructureDefinition {
40
41 private HashSet<String> myAllChildrenNames;
42 private HashSet<String> myAllFirstLeafNames;
43 private final ArrayList<StructureDefinition> myChildren = new ArrayList<>();
44 private IStructureDefinition myFirstSibling;
45 private boolean myFirstSiblingIsSet;
46 private Boolean myIsFinalChildOfParent;
47 private boolean myIsRepeating;
48 private boolean myIsRequired;
49 private boolean myIsSegment;
50 private String myName;
51 private String myNameAsItAppearsInParent;
52 private volatile Set<String> myNamesOfAllPossibleFollowingLeaves;
53 private IStructureDefinition myNextLeaf;
54 private IStructureDefinition myNextSibling;
55 private IStructureDefinition myParent;
56 private int myPosition;
57 private boolean myChoiceElement;
58
59
60
61
62
63 public StructureDefinition() {
64 }
65
66
67
68
69
70 void addChild(StructureDefinition theChild) {
71 myChildren.add(theChild);
72 }
73
74
75
76
77
78 @Override
79 public boolean equals(Object theObj) {
80 if (!(theObj instanceof StructureDefinition)) {
81 return false;
82 }
83 StructureDefinition/../ca/uhn/hl7v2/parser/StructureDefinition.html#StructureDefinition">StructureDefinition o = (StructureDefinition) theObj;
84 return o.myName.equals(myName) && o.myPosition == myPosition;
85 }
86
87
88
89
90
91 @Override
92 public HashSet<String> getAllChildNames() {
93 if (myAllChildrenNames == null) {
94 myAllChildrenNames = new HashSet<>();
95 for (IStructureDefinition next : myChildren) {
96 myAllChildrenNames.add(next.getName());
97 myAllChildrenNames.addAll(next.getAllChildNames());
98 }
99 }
100
101 return myAllChildrenNames;
102 }
103
104
105
106
107
108 @Override
109 public HashSet<String> getAllPossibleFirstChildren() {
110 if (myAllFirstLeafNames == null) {
111 myAllFirstLeafNames = new HashSet<>();
112
113 boolean hasChoice = false;
114 for (IStructureDefinition next : myChildren) {
115 myAllFirstLeafNames.addAll(next.getAllPossibleFirstChildren());
116
117 if (next.isChoiceElement()) {
118 hasChoice = true;
119 continue;
120 } else if (hasChoice) {
121 break;
122 }
123
124 if (next.isRequired()) {
125 break;
126 }
127 }
128
129 myAllFirstLeafNames.add(getName());
130 }
131
132 return myAllFirstLeafNames;
133 }
134
135
136
137
138
139 @Override
140 public ArrayList<StructureDefinition> getChildren() {
141 return myChildren;
142 }
143
144
145
146
147
148 @Override
149 public IStructureDefinition getFirstChild() {
150 return myChildren.get(0);
151 }
152
153
154
155
156
157 @Override
158 public IStructureDefinition getFirstSibling() {
159 if (!myFirstSiblingIsSet) {
160 if (myParent == null) {
161 myFirstSibling = null;
162 } else if (myParent.getChildren().get(0) == this) {
163 myFirstSibling = null;
164 } else {
165 myFirstSibling = myParent.getChildren().get(0);
166 }
167 myFirstSiblingIsSet = true;
168 }
169
170 return myFirstSibling;
171 }
172
173
174
175
176
177 public String getName() {
178 return myName;
179 }
180
181
182
183
184
185 @Override
186 public String getNameAsItAppearsInParent() {
187 return myNameAsItAppearsInParent;
188 }
189
190
191
192
193
194 @Override
195 public Set<String> getNamesOfAllPossibleFollowingLeaves() {
196 if (myNamesOfAllPossibleFollowingLeaves != null) {
197 return myNamesOfAllPossibleFollowingLeaves;
198 }
199
200 HashSet<String> retVal = new HashSet<>();
201
202 IStructureDefinition nextLeaf = getNextLeaf();
203 if (nextLeaf != null) {
204 retVal.add(nextLeaf.getName());
205 Set<String> namesOfAllPossibleFollowingLeaves = nextLeaf.getNamesOfAllPossibleFollowingLeaves();
206 retVal.addAll(namesOfAllPossibleFollowingLeaves);
207 }
208
209 IStructureDefinition parent = myParent;
210 while (parent != null) {
211 if (parent.isRepeating()) {
212 retVal.addAll(parent.getAllPossibleFirstChildren());
213 }
214 parent = parent.getParent();
215 }
216
217 myNamesOfAllPossibleFollowingLeaves = retVal;
218 return retVal;
219 }
220
221
222
223
224
225 @Override
226 public IStructureDefinition getNextLeaf() {
227 return myNextLeaf;
228 }
229
230
231
232
233
234 @Override
235 public IStructureDefinition getNextSibling() {
236 if (myNextSibling != null) {
237 return myNextSibling;
238 }
239
240 if (isFinalChildOfParent()) {
241 throw new IllegalStateException("Final child");
242 }
243
244 myNextSibling = myParent.getChildren().get(myPosition + 1);
245 return myNextSibling;
246 }
247
248
249
250
251
252 @Override
253 public IStructureDefinition getParent() {
254 return myParent;
255 }
256
257
258
259
260
261 @Override
262 public int getPosition() {
263 return myPosition;
264 }
265
266
267
268
269
270 @Override
271 public boolean hasChildren() {
272 return !myChildren.isEmpty();
273 }
274
275
276
277
278
279 @Override
280 public int hashCode() {
281 return 17 * myName.hashCode() * myPosition;
282 }
283
284
285
286
287
288 @Override
289 public boolean isFinalChildOfParent() {
290 if (myIsFinalChildOfParent != null) {
291 return myIsFinalChildOfParent;
292 }
293 myIsFinalChildOfParent = myParent == null || (myPosition == (myParent.getChildren().size() - 1));
294 return myIsFinalChildOfParent;
295 }
296
297
298
299
300
301 @Override
302 public boolean isRepeating() {
303 return myIsRepeating;
304 }
305
306
307
308
309
310 @Override
311 public boolean isRequired() {
312 return myIsRequired;
313 }
314
315
316
317
318
319 @Override
320 public boolean isSegment() {
321 return myIsSegment;
322 }
323
324
325
326
327
328 void setName(String theName) {
329 myName = theName;
330 }
331
332
333
334
335
336 void setNameAsItAppearsInParent(String theName) {
337 myNameAsItAppearsInParent = theName;
338 }
339
340
341
342
343
344 void setNextLeaf(IStructureDefinition theNextLeaf) {
345 myNextLeaf = theNextLeaf;
346 }
347
348
349
350
351
352 void setParent(IStructureDefinition theParent) {
353 myParent = theParent;
354 }
355
356
357
358
359
360 void setPosition(int thePosition) {
361 myPosition = thePosition;
362 }
363
364
365
366
367
368 void setRepeating(boolean theIsRepeating) {
369 myIsRepeating = theIsRepeating;
370 }
371
372
373
374
375
376 void setRequired(boolean theIsRequired) {
377 myIsRequired = theIsRequired;
378 }
379
380
381
382
383
384 void setSegment(boolean theIsSegment) {
385 myIsSegment = theIsSegment;
386 }
387
388
389
390
391
392 @Override
393 public String toString() {
394 return "StructureDefinition[" + getName() + "]";
395 }
396
397
398
399
400
401
402 public void setChoiceElement(boolean theChoiceElement) {
403 myChoiceElement = theChoiceElement;
404 }
405
406
407
408
409
410 @Override
411 public boolean isChoiceElement() {
412 return myChoiceElement;
413 }
414
415 }