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 | |
protected Logger log; |
41 | |
|
42 | |
public AbstractComposite(Message message) { |
43 | 137724 | super(message); |
44 | 137724 | log = LoggerFactory.getLogger(getClass()); |
45 | 137724 | } |
46 | |
|
47 | |
@Override |
48 | |
public void clear() { |
49 | 255 | super.clear(); |
50 | 1330 | for (Type component : getComponents()) { |
51 | 1075 | component.clear(); |
52 | |
} |
53 | 255 | } |
54 | |
|
55 | |
protected <T extends Type> T getTyped(int idx, Class<T> type) { |
56 | |
try { |
57 | 1310 | @SuppressWarnings("unchecked") T ret = (T)getComponent(idx); |
58 | 1310 | return ret; |
59 | 0 | } catch (HL7Exception e) { |
60 | 0 | log.error("Unexpected problem accessing known data type component - this is a bug.", e); |
61 | 0 | throw new RuntimeException(e); |
62 | |
} |
63 | |
} |
64 | |
|
65 | |
@Override |
66 | |
public boolean isEmpty() throws HL7Exception { |
67 | 3140 | for (Type type : getComponents()) { |
68 | 2820 | if (!type.isEmpty()) return false; |
69 | |
} |
70 | 320 | return super.isEmpty(); |
71 | |
} |
72 | |
|
73 | |
public boolean accept(MessageVisitor visitor, Location location) throws HL7Exception { |
74 | 360 | if (visitor.start(this, location)) { |
75 | 350 | Type[] types = getComponents(); |
76 | 1615 | for (int i = 0; i < types.length; i++) { |
77 | 1380 | Type t = getComponent(i); |
78 | 1380 | Location nextLocation = t.provideLocation(location, i + 1, location.getFieldRepetition()); |
79 | 1380 | if (!t.accept(visitor, nextLocation)) break; |
80 | |
} |
81 | 350 | ExtraComponents ec = getExtraComponents(); |
82 | 350 | for (int i = 0; i < ec.numComponents(); i++) { |
83 | 0 | Variable v = ec.getComponent(i); |
84 | 0 | Location nextLocation = v.provideLocation(location, i + types.length, -1); |
85 | 0 | if (!v.accept(visitor, nextLocation)) break; |
86 | |
} |
87 | |
} |
88 | 360 | return visitor.end(this, location); |
89 | |
} |
90 | |
|
91 | |
@Override |
92 | |
public Location provideLocation(Location location, int index, int repetition) { |
93 | 100 | return super.provideLocation(location, index, repetition).atComponentLevel(!location.isComponentLevel()); |
94 | |
} |
95 | |
} |