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 package ca.uhn.hl7v2.model;
27
28 import org.slf4j.Logger;
29 import org.slf4j.LoggerFactory;
30
31 import ca.uhn.hl7v2.HL7Exception;
32 import ca.uhn.hl7v2.Location;
33
34
35 public abstract class AbstractComposite extends AbstractType implements
36 Composite {
37
38 private static final long serialVersionUID = -2657103285266475699L;
39
40 private static final Logger log = LoggerFactory.getLogger(AbstractComposite.class);
41
42 public AbstractComposite(Message message) {
43 super(message);
44 }
45
46 @Override
47 public void clear() {
48 super.clear();
49 for (Type component : getComponents()) {
50 component.clear();
51 }
52 }
53
54 protected <T extends Type> T getTyped(int idx, Class<T> type) {
55 try {
56 @SuppressWarnings("unchecked") T ret = (T)getComponent(idx);
57 return ret;
58 } catch (HL7Exception e) {
59 log.error("Unexpected problem accessing known data type component - this is a bug. Class is " + getClass().getName(), e);
60 throw new RuntimeException(e);
61 }
62 }
63
64 @Override
65 public boolean isEmpty() throws HL7Exception {
66 for (Type type : getComponents()) {
67 if (!type.isEmpty()) return false;
68 }
69 return super.isEmpty();
70 }
71
72 public boolean accept(MessageVisitor visitor, Location location) throws HL7Exception {
73 if (visitor.start(this, location)) {
74 Type[] types = getComponents();
75 for (int i = 0; i < types.length; i++) {
76 Type t = getComponent(i);
77 Location nextLocation = t.provideLocation(location, i + 1, location.getFieldRepetition());
78 if (!t.accept(visitor, nextLocation)) break;
79 }
80 ExtraComponents ec = getExtraComponents();
81 for (int i = 0; i < ec.numComponents(); i++) {
82 Variable v = ec.getComponent(i);
83 Location nextLocation = v.provideLocation(location, i + types.length, -1);
84 if (!v.accept(visitor, nextLocation)) break;
85 }
86 }
87 return visitor.end(this, location);
88 }
89
90 @Override
91 public Location/../ca/uhn/hl7v2/Location.html#Location">Location provideLocation(Location location, int index, int repetition) {
92 return super.provideLocation(location, index, repetition).atComponentLevel(!location.isComponentLevel());
93 }
94 }