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.model; |
28 | |
|
29 | |
import ca.uhn.hl7v2.AcknowledgmentCode; |
30 | |
import ca.uhn.hl7v2.HL7Exception; |
31 | |
import ca.uhn.hl7v2.HapiContext; |
32 | |
import ca.uhn.hl7v2.Location; |
33 | |
import ca.uhn.hl7v2.parser.Parser; |
34 | |
|
35 | |
import java.io.IOException; |
36 | |
|
37 | |
|
38 | |
|
39 | |
|
40 | |
|
41 | |
|
42 | |
|
43 | |
|
44 | |
|
45 | |
|
46 | |
|
47 | |
|
48 | 2920 | public final class Unmodifiable { |
49 | |
|
50 | 0 | private Unmodifiable() { |
51 | 0 | } |
52 | |
|
53 | |
|
54 | |
|
55 | |
|
56 | |
|
57 | |
|
58 | |
|
59 | |
|
60 | |
|
61 | |
|
62 | |
public static Message unmodifiableMessage(Message msg) { |
63 | 10 | return isUnmodifiable(msg) ? msg : new UnmodifiableMessage(msg); |
64 | |
} |
65 | |
|
66 | |
|
67 | |
|
68 | |
|
69 | |
|
70 | |
|
71 | |
|
72 | |
|
73 | |
|
74 | |
|
75 | |
|
76 | |
public static Message unmodifiableMessage(HapiContext context, String s) throws HL7Exception { |
77 | 55 | Message msg = context.getGenericParser().parse(s); |
78 | 55 | return new UnmodifiableMessage(msg, s); |
79 | |
} |
80 | |
|
81 | |
|
82 | |
|
83 | |
|
84 | |
|
85 | |
|
86 | |
|
87 | |
public static boolean isUnmodifiable(Object o) { |
88 | 4500 | return o instanceof UnmodifiableModel; |
89 | |
} |
90 | |
|
91 | |
|
92 | |
@SuppressWarnings("unchecked") |
93 | |
private static <T extends Type> T unmodifiableType(T type) { |
94 | 1755 | if (isUnmodifiable(type)) return type; |
95 | 1755 | if (type instanceof Primitive) return (T) new UnmodifiablePrimitive((Primitive) type); |
96 | 520 | if (type instanceof Composite) return (T) new UnmodifiableComposite((Composite) type); |
97 | 0 | return (T) new UnmodifiableVaries((Variable) type); |
98 | |
} |
99 | |
|
100 | |
@SuppressWarnings("unchecked") |
101 | |
private static <T extends Structure> T unmodifiableStructure(T structure) { |
102 | 1155 | if (isUnmodifiable(structure)) return structure; |
103 | 1155 | if (structure instanceof Message) return (T) new UnmodifiableMessage((Message) structure); |
104 | 925 | if (structure instanceof Group) return (T) new UnmodifiableGroup((Group) structure); |
105 | 345 | return (T) new UnmodifiableSegment((Segment) structure); |
106 | |
} |
107 | |
|
108 | |
@SuppressWarnings("unchecked") |
109 | |
private static <T extends MessageVisitor> T unmodifiableVisitor(T visitor) { |
110 | 5 | return isUnmodifiable(visitor) ? visitor : (T) new UnmodifiableMessageVisitor(visitor); |
111 | |
} |
112 | |
|
113 | |
@SuppressWarnings("unchecked") |
114 | |
private static ExtraComponents unmodifiableExtraComponents(ExtraComponents ec) { |
115 | 5 | return isUnmodifiable(ec) ? ec : new UnmodifiableExtraComponents(ec); |
116 | |
} |
117 | |
|
118 | |
|
119 | |
|
120 | |
|
121 | |
private static interface UnmodifiableModel { |
122 | |
} |
123 | |
|
124 | |
|
125 | |
private static class Delegating<S> { |
126 | |
private S delegate; |
127 | |
|
128 | 2980 | protected Delegating(S delegate) { |
129 | 2980 | this.delegate = delegate; |
130 | 2980 | } |
131 | |
|
132 | |
public S getDelegate() { |
133 | 6065 | return delegate; |
134 | |
} |
135 | |
|
136 | |
@Override |
137 | |
public String toString() { |
138 | 0 | return delegate.toString(); |
139 | |
} |
140 | |
|
141 | |
|
142 | |
|
143 | |
|
144 | |
|
145 | |
|
146 | |
|
147 | |
|
148 | |
|
149 | |
@Override |
150 | |
public boolean equals(Object o) { |
151 | 480 | if (this == o) return true; |
152 | 475 | if (o instanceof Delegating) { |
153 | 475 | Delegating that = (Delegating) o; |
154 | 475 | return delegate.equals(that.delegate); |
155 | |
} |
156 | 0 | if (o.getClass().isAssignableFrom(delegate.getClass())) { |
157 | 0 | return delegate.equals(o); |
158 | |
} |
159 | 0 | return false; |
160 | |
} |
161 | |
|
162 | |
@Override |
163 | |
public int hashCode() { |
164 | 0 | return delegate.hashCode(); |
165 | |
} |
166 | |
} |
167 | |
|
168 | |
private static class UnmodifiableVisitable<S extends Visitable> extends Delegating<S> implements Visitable, UnmodifiableModel { |
169 | |
|
170 | |
public UnmodifiableVisitable(S delegate) { |
171 | 2975 | super(delegate); |
172 | 2975 | } |
173 | |
|
174 | |
public boolean accept(MessageVisitor visitor, Location currentLocation) throws HL7Exception { |
175 | 5 | return getDelegate().accept(unmodifiableVisitor(visitor), currentLocation); |
176 | |
} |
177 | |
|
178 | |
public Location provideLocation(Location parentLocation, int index, int repetition) { |
179 | 0 | return getDelegate().provideLocation(parentLocation, index, repetition); |
180 | |
} |
181 | |
|
182 | |
public boolean isEmpty() throws HL7Exception { |
183 | 0 | return getDelegate().isEmpty(); |
184 | |
} |
185 | |
} |
186 | |
|
187 | |
|
188 | 1220 | private abstract static class UnmodifiableStructure<S extends Structure> extends UnmodifiableVisitable<S> |
189 | |
implements Structure { |
190 | |
|
191 | |
private UnmodifiableStructure(S delegate) { |
192 | 1220 | super(delegate); |
193 | 1220 | } |
194 | |
|
195 | |
public Message getMessage() { |
196 | 0 | return getDelegate().getMessage(); |
197 | |
} |
198 | |
|
199 | |
public String getName() { |
200 | 385 | return getDelegate().getName(); |
201 | |
} |
202 | |
|
203 | |
public Group getParent() { |
204 | 380 | return unmodifiableStructure(getDelegate().getParent()); |
205 | |
} |
206 | |
|
207 | |
} |
208 | |
|
209 | |
private static class UnmodifiableSegment<S extends Segment> extends UnmodifiableStructure<S> implements Segment { |
210 | |
|
211 | |
public UnmodifiableSegment(S delegate) { |
212 | 345 | super(delegate); |
213 | 345 | } |
214 | |
|
215 | |
public String encode() throws HL7Exception { |
216 | 0 | return getDelegate().encode(); |
217 | |
} |
218 | |
|
219 | |
public Type[] getField(int number) throws HL7Exception { |
220 | 25 | if (number < 1 || number > numFields()) { |
221 | 10 | throw new IllegalArgumentException(String.format("Cannot add field with index %d to unmodifiable segment %s " + |
222 | 5 | " - there are currently only %d fields.", number, getName(), numFields())); |
223 | |
} |
224 | 20 | Type[] types = getDelegate().getField(number); |
225 | 20 | Type[] unmodifiableTypes = new Type[types.length]; |
226 | 20 | if (types.length > 0) { |
227 | 40 | for (int i = 0; i < types.length; i++) { |
228 | 20 | unmodifiableTypes[i] = unmodifiableType(types[i]); |
229 | |
} |
230 | |
} |
231 | 20 | return unmodifiableTypes; |
232 | |
} |
233 | |
|
234 | |
public Type getField(int number, int rep) throws HL7Exception { |
235 | 20 | Type[] types = getField(number); |
236 | 20 | if (rep >= types.length) { |
237 | 10 | throw new IllegalArgumentException(String.format("Cannot add repetition with index %d to unmodifiable field %d " + |
238 | 5 | " - there are currently only %d fields.", rep, number, types.length)); |
239 | |
} |
240 | 15 | return types[rep]; |
241 | |
} |
242 | |
|
243 | |
public int getLength(int number) throws HL7Exception { |
244 | 0 | return getDelegate().getLength(number); |
245 | |
} |
246 | |
|
247 | |
public int getMaxCardinality(int number) throws HL7Exception { |
248 | 0 | return getDelegate().getMaxCardinality(number); |
249 | |
} |
250 | |
|
251 | |
public String[] getNames() { |
252 | 0 | return getDelegate().getNames(); |
253 | |
} |
254 | |
|
255 | |
public boolean isRequired(int number) throws HL7Exception { |
256 | 0 | return getDelegate().isRequired(number); |
257 | |
} |
258 | |
|
259 | |
public int numFields() { |
260 | 30 | return getDelegate().numFields(); |
261 | |
} |
262 | |
|
263 | |
public void parse(String string) throws HL7Exception { |
264 | 0 | throw new UnsupportedOperationException("This segment is unmodifiable"); |
265 | |
} |
266 | |
} |
267 | |
|
268 | |
private static class UnmodifiableGroup<S extends Group> extends UnmodifiableStructure<S> implements Group { |
269 | |
public UnmodifiableGroup(S delegate) { |
270 | 875 | super(delegate); |
271 | 875 | } |
272 | |
|
273 | |
public Structure[] getAll(String name) throws HL7Exception { |
274 | 690 | Structure[] structures = getDelegate().getAll(name); |
275 | 690 | Structure[] unmodifiableStructures = new Structure[structures.length]; |
276 | 690 | if (structures.length > 0) { |
277 | 1310 | for (int i = 0; i < structures.length; i++) { |
278 | 655 | unmodifiableStructures[i] = unmodifiableStructure(structures[i]); |
279 | |
} |
280 | |
} |
281 | 690 | return unmodifiableStructures; |
282 | |
} |
283 | |
|
284 | |
public Structure get(String name) throws HL7Exception { |
285 | 85 | return get(name, 0); |
286 | |
} |
287 | |
|
288 | |
|
289 | |
|
290 | |
|
291 | |
|
292 | |
|
293 | |
|
294 | |
|
295 | |
|
296 | |
|
297 | |
public Structure get(String name, int rep) throws HL7Exception { |
298 | 395 | return getAll(name)[rep]; |
299 | |
} |
300 | |
|
301 | |
public boolean isRequired(String name) throws HL7Exception { |
302 | 0 | return getDelegate().isRequired(name); |
303 | |
} |
304 | |
|
305 | |
public boolean isRepeating(String name) throws HL7Exception { |
306 | 25 | return getDelegate().isRepeating(name); |
307 | |
} |
308 | |
|
309 | |
public boolean isChoiceElement(String name) throws HL7Exception { |
310 | 0 | return getDelegate().isChoiceElement(name); |
311 | |
} |
312 | |
|
313 | |
public boolean isGroup(String name) throws HL7Exception { |
314 | 0 | return getDelegate().isGroup(name); |
315 | |
} |
316 | |
|
317 | |
public String[] getNames() { |
318 | 765 | return getDelegate().getNames(); |
319 | |
} |
320 | |
|
321 | |
public Class<? extends Structure> getClass(String name) { |
322 | 0 | return getDelegate().getClass(name); |
323 | |
} |
324 | |
|
325 | |
public String addNonstandardSegment(String name) throws HL7Exception { |
326 | 5 | throw new UnsupportedOperationException("This group is unmodifiable"); |
327 | |
} |
328 | |
|
329 | |
public String addNonstandardSegment(String name, int theIndex) throws HL7Exception { |
330 | 0 | throw new UnsupportedOperationException("This group is unmodifiable"); |
331 | |
} |
332 | |
} |
333 | |
|
334 | |
private static class UnmodifiableMessage extends UnmodifiableGroup<Message> implements Message { |
335 | |
|
336 | |
private String originalMessage; |
337 | |
|
338 | |
public UnmodifiableMessage(Message delegate, String originalMessage) { |
339 | 55 | this(delegate); |
340 | 55 | this.originalMessage = originalMessage; |
341 | 55 | } |
342 | |
|
343 | |
public UnmodifiableMessage(Message delegate) { |
344 | 295 | super(delegate); |
345 | 295 | } |
346 | |
|
347 | |
public String getVersion() { |
348 | 0 | return getDelegate().getVersion(); |
349 | |
} |
350 | |
|
351 | |
public Character getFieldSeparatorValue() throws HL7Exception { |
352 | 0 | return getDelegate().getFieldSeparatorValue(); |
353 | |
} |
354 | |
|
355 | |
public String getEncodingCharactersValue() throws HL7Exception { |
356 | 0 | return getDelegate().getEncodingCharactersValue(); |
357 | |
} |
358 | |
|
359 | |
public void setParser(Parser parser) { |
360 | 0 | throw new UnsupportedOperationException("This message is unmodifiable"); |
361 | |
} |
362 | |
|
363 | |
public Parser getParser() { |
364 | 0 | return getDelegate().getParser(); |
365 | |
} |
366 | |
|
367 | |
public void parse(String string) throws HL7Exception { |
368 | 0 | throw new UnsupportedOperationException("This message is unmodifiable"); |
369 | |
} |
370 | |
|
371 | |
public String encode() throws HL7Exception { |
372 | 5 | return originalMessage != null ? originalMessage : getDelegate().encode(); |
373 | |
} |
374 | |
|
375 | |
public Message generateACK() throws HL7Exception, IOException { |
376 | 0 | return getDelegate().generateACK(); |
377 | |
} |
378 | |
|
379 | |
public Message generateACK(String theAcknowlegementCode, HL7Exception theException) throws HL7Exception, IOException { |
380 | 0 | return getDelegate().generateACK(theAcknowlegementCode, theException); |
381 | |
} |
382 | |
|
383 | |
public Message generateACK(AcknowledgmentCode theAcknowlegementCode, HL7Exception theException) throws HL7Exception, IOException { |
384 | 0 | return getDelegate().generateACK(theAcknowlegementCode, theException); |
385 | |
} |
386 | |
|
387 | |
public String printStructure() throws HL7Exception { |
388 | 0 | return getDelegate().printStructure(); |
389 | |
} |
390 | |
} |
391 | |
|
392 | |
private abstract static class UnmodifiableType<T extends Type> extends UnmodifiableVisitable<T> |
393 | |
implements Type { |
394 | |
|
395 | |
public UnmodifiableType(T delegate) { |
396 | 1755 | super(delegate); |
397 | 1755 | } |
398 | |
|
399 | |
public String getName() { |
400 | 0 | return getDelegate().getName(); |
401 | |
} |
402 | |
|
403 | |
public ExtraComponents getExtraComponents() { |
404 | 5 | return unmodifiableExtraComponents(getDelegate().getExtraComponents()); |
405 | |
} |
406 | |
|
407 | |
public Message getMessage() { |
408 | 0 | return unmodifiableMessage(getDelegate().getMessage()); |
409 | |
} |
410 | |
|
411 | |
public void parse(String string) throws HL7Exception { |
412 | 0 | throw new UnsupportedOperationException("This type is unmodifiable"); |
413 | |
} |
414 | |
|
415 | |
public String encode() throws HL7Exception { |
416 | 0 | return getDelegate().encode(); |
417 | |
} |
418 | |
|
419 | |
public void clear() { |
420 | 0 | throw new UnsupportedOperationException("This type is unmodifiable"); |
421 | |
} |
422 | |
|
423 | |
public Location provideLocation(Location parentLocation, int index, int repetition) { |
424 | 0 | return getDelegate().provideLocation(parentLocation, index, repetition); |
425 | |
} |
426 | |
|
427 | |
} |
428 | |
|
429 | |
private static class UnmodifiablePrimitive extends UnmodifiableType<Primitive> implements Primitive { |
430 | |
|
431 | |
public UnmodifiablePrimitive(Primitive delegate) { |
432 | 1235 | super(delegate); |
433 | 1235 | } |
434 | |
|
435 | |
public String getValue() { |
436 | 5 | return getDelegate().getValue(); |
437 | |
} |
438 | |
|
439 | |
public void setValue(String value) throws DataTypeException { |
440 | 5 | throw new UnsupportedOperationException("This Primitive is unmodifiable"); |
441 | |
} |
442 | |
} |
443 | |
|
444 | |
private static class UnmodifiableComposite extends UnmodifiableType<Composite> implements Composite { |
445 | |
|
446 | |
public UnmodifiableComposite(Composite delegate) { |
447 | 520 | super(delegate); |
448 | 520 | } |
449 | |
|
450 | |
public Type[] getComponents() { |
451 | 20 | Type[] types = getDelegate().getComponents(); |
452 | 20 | Type[] unmodifiableTypes = new Type[types.length]; |
453 | 20 | if (types.length > 0) { |
454 | 140 | for (int i = 0; i < types.length; i++) { |
455 | 120 | unmodifiableTypes[i] = unmodifiableType(types[i]); |
456 | |
} |
457 | |
} |
458 | 20 | return unmodifiableTypes; |
459 | |
} |
460 | |
|
461 | |
public Type getComponent(int number) throws DataTypeException { |
462 | 10 | Type type = getDelegate().getComponent(number); |
463 | 10 | return unmodifiableType(type); |
464 | |
} |
465 | |
|
466 | |
} |
467 | |
|
468 | |
private static class UnmodifiableVaries extends UnmodifiableType<Variable> implements Variable { |
469 | |
|
470 | |
public UnmodifiableVaries(Variable delegate) { |
471 | 0 | super(delegate); |
472 | 0 | } |
473 | |
|
474 | |
public Type getData() { |
475 | 0 | return unmodifiableType(getDelegate().getData()); |
476 | |
} |
477 | |
|
478 | |
public void setData(Type data) throws DataTypeException { |
479 | 0 | throw new UnsupportedOperationException("This Varies is unmodifiable"); |
480 | |
} |
481 | |
|
482 | |
} |
483 | |
|
484 | |
private static class UnmodifiableExtraComponents extends ExtraComponents { |
485 | |
|
486 | |
private ExtraComponents delegate; |
487 | |
|
488 | |
public UnmodifiableExtraComponents(ExtraComponents delegate) { |
489 | 5 | super(delegate.getMessage()); |
490 | 5 | this.delegate = delegate; |
491 | 5 | } |
492 | |
|
493 | |
@Override |
494 | |
public int numComponents() { |
495 | 5 | return delegate.numComponents(); |
496 | |
} |
497 | |
|
498 | |
@Override |
499 | |
public boolean isEmpty() throws HL7Exception { |
500 | 0 | return delegate.isEmpty(); |
501 | |
} |
502 | |
|
503 | |
@Override |
504 | |
public Message getMessage() { |
505 | 0 | return unmodifiableMessage(delegate.getMessage()); |
506 | |
} |
507 | |
|
508 | |
@Override |
509 | |
public String toString() { |
510 | 0 | return delegate.toString(); |
511 | |
} |
512 | |
|
513 | |
@Override |
514 | |
public Variable getComponent(int comp) { |
515 | 5 | if (comp >= numComponents()) { |
516 | 10 | throw new IllegalArgumentException(String.format( |
517 | 5 | "Extra Component with index %d is not available and cannot be added to unmodifiable type", comp)); |
518 | |
} |
519 | 0 | return unmodifiableType(delegate.getComponent(comp)); |
520 | |
} |
521 | |
|
522 | |
@Override |
523 | |
void clear() { |
524 | 0 | throw new UnsupportedOperationException("This ExtraComponents is unmodifiable"); |
525 | |
} |
526 | |
} |
527 | |
|
528 | |
|
529 | |
private static class UnmodifiableMessageVisitor extends Delegating<MessageVisitor> implements MessageVisitor, UnmodifiableModel { |
530 | |
|
531 | |
|
532 | |
public UnmodifiableMessageVisitor(MessageVisitor delegate) { |
533 | 5 | super(delegate); |
534 | 5 | } |
535 | |
|
536 | |
public boolean start(Message message) throws HL7Exception { |
537 | 5 | return getDelegate().start(unmodifiableMessage(message)); |
538 | |
} |
539 | |
|
540 | |
public boolean end(Message message) throws HL7Exception { |
541 | 5 | return getDelegate().end(unmodifiableMessage(message)); |
542 | |
} |
543 | |
|
544 | |
public boolean start(Group group, Location location) throws HL7Exception { |
545 | 25 | return getDelegate().start(unmodifiableStructure(group), location); |
546 | |
} |
547 | |
|
548 | |
public boolean end(Group group, Location location) throws HL7Exception { |
549 | 25 | return getDelegate().end(unmodifiableStructure(group), location); |
550 | |
} |
551 | |
|
552 | |
public boolean start(Segment segment, Location location) throws HL7Exception { |
553 | 35 | return getDelegate().start(unmodifiableStructure(segment), location); |
554 | |
} |
555 | |
|
556 | |
public boolean end(Segment segment, Location location) throws HL7Exception { |
557 | 35 | return getDelegate().end(unmodifiableStructure(segment), location); |
558 | |
} |
559 | |
|
560 | |
public boolean start(Field field, Location location) throws HL7Exception { |
561 | |
|
562 | 995 | return getDelegate().start(field, location); |
563 | |
} |
564 | |
|
565 | |
public boolean end(Field field, Location location) throws HL7Exception { |
566 | |
|
567 | 995 | return getDelegate().end(field, location); |
568 | |
} |
569 | |
|
570 | |
public boolean start(Composite type, Location location) throws HL7Exception { |
571 | 230 | return getDelegate().start(unmodifiableType(type), location); |
572 | |
} |
573 | |
|
574 | |
public boolean end(Composite type, Location location) throws HL7Exception { |
575 | 230 | return getDelegate().end(unmodifiableType(type), location); |
576 | |
} |
577 | |
|
578 | |
public boolean visit(Primitive type, Location location) throws HL7Exception { |
579 | 1145 | return getDelegate().visit(unmodifiableType(type), location); |
580 | |
} |
581 | |
} |
582 | |
|
583 | |
} |