Classes in this File | Line Coverage | Branch Coverage | Complexity | ||||
ExtraComponents |
|
| 1.5;1.5 |
1 | package ca.uhn.hl7v2.model; | |
2 | ||
3 | import java.io.Serializable; | |
4 | import java.util.ArrayList; | |
5 | import java.util.List; | |
6 | ||
7 | import ca.uhn.hl7v2.HL7Exception; | |
8 | ||
9 | /** | |
10 | * A set of "extra" components (sub-components) that are not a standard part | |
11 | * of a field (component) but have been added at runtime. The purpose is to allow | |
12 | * processing of locally-defined extensions to datatypes without the need for a | |
13 | * custom message definition. | |
14 | * Extra components are not treated uniformly with standard components (e.g. | |
15 | * they are not accessible through methods like Primitive.getValue() and | |
16 | * Composite.getComponent()). To do so would blur the distinction between | |
17 | * primitive and composite types (i.e. leaf and non-leaf nodes), which seems | |
18 | * nice and polymorphic for a moment but actually isn't helpful. | |
19 | * Furthermore, the auto-generated classes do not define accessors to extra | |
20 | * components, because they are meant to encourage and enforce use of the standard | |
21 | * message structure -- stepping outside the standard structure must be | |
22 | * deliberate. | |
23 | * Note that a uniformity of access to standard and extra components is provided | |
24 | * by Terser. | |
25 | * @author Bryan Tripp | |
26 | */ | |
27 | public class ExtraComponents implements Serializable { | |
28 | ||
29 | ||
30 | private static final long serialVersionUID = -2614683870975956395L; | |
31 | ||
32 | private List<Variable> comps; | |
33 | private Message message; | |
34 | ||
35 | 778832 | public ExtraComponents(Message message) { |
36 | 778832 | this.comps = new ArrayList<Variable>(); |
37 | 778832 | this.message = message; |
38 | 778832 | } |
39 | ||
40 | /** | |
41 | * Returns the number of existing extra components | |
42 | * @return number of existing extra components | |
43 | */ | |
44 | public int numComponents() { | |
45 | 592388 | return comps.size(); |
46 | } | |
47 | ||
48 | /** | |
49 | * Returns true if extra components are empty | |
50 | * @return true if extra components are empty, false otherwise | |
51 | * @throws HL7Exception | |
52 | */ | |
53 | public boolean isEmpty() throws HL7Exception { | |
54 | 2270 | for (Variable varies : comps) { |
55 | 20 | if (!varies.isEmpty()) return false; |
56 | 20 | } |
57 | 2270 | return true; |
58 | } | |
59 | ||
60 | public Message getMessage() { | |
61 | 5 | return message; |
62 | } | |
63 | ||
64 | /** | |
65 | * Returns the component at the given location, creating it | |
66 | * and all preceeding components if necessary. | |
67 | * | |
68 | * @param comp the extra component number starting at 0 (i.e. 0 is the first | |
69 | * extra component) | |
70 | * @return component at the given index | |
71 | */ | |
72 | public Variable getComponent(int comp) { | |
73 | 2250 | ensureComponentAndPredecessorsExist(comp); |
74 | 2250 | return this.comps.get(comp); |
75 | } | |
76 | ||
77 | /** | |
78 | * Checks that the component at the given location exists, and that | |
79 | * all preceding components exist, creating any missing ones. | |
80 | */ | |
81 | private void ensureComponentAndPredecessorsExist(int comp) { | |
82 | 3160 | for (int i = this.comps.size(); i <= comp; i++) { |
83 | 910 | this.comps.add(new Varies(message)); |
84 | } | |
85 | 2250 | } |
86 | ||
87 | ||
88 | /** | |
89 | * Clears all extra components | |
90 | */ | |
91 | void clear() { | |
92 | 1420 | comps.clear(); |
93 | 1420 | } |
94 | ||
95 | /** | |
96 | * {@inheritDoc} | |
97 | */ | |
98 | @Override | |
99 | public String toString() { | |
100 | 0 | return "ExtraComponents" + comps; |
101 | } | |
102 | ||
103 | ||
104 | ||
105 | ||
106 | } |