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.testpanel.model.conf;
27
28 import java.util.ArrayList;
29 import java.util.List;
30
31 import ca.uhn.hl7v2.HL7Exception;
32 import ca.uhn.hl7v2.conf.spec.message.Seg;
33 import ca.uhn.hl7v2.model.AbstractSegment;
34 import ca.uhn.hl7v2.model.Type;
35
36
37 public class ConformanceSegment extends AbstractSegment implements ConformanceStructure<ConformanceSegment> {
38
39 private static final Object[] EMPTY_OBJECT_ARRAY = new Object[0];
40 private Seg myConfDefinition;
41 private List<Integer> myMinReps = new ArrayList<Integer>();
42 private List<Boolean> myPrototypeHasBeenUsed = new ArrayList<Boolean>();
43 private List<ConformanceType<?>> myPrototypes = new ArrayList<ConformanceType<?>>();
44
45 public ConformanceSegment(ConformanceStructureHolder theParent, Seg theConfDefinition) {
46 super(theParent, ((ConformanceMessage)theParent.getMessage()).getModelClassFactory());
47
48 myConfDefinition = theConfDefinition;
49 }
50
51 @Override
52 public ConformanceMessage getMessage() {
53 return (ConformanceMessage) super.getMessage();
54 }
55
56
57
58
59 void addChild(ConformanceType<?> theChild, String theName, int theMinReps, int theMaxReps, int theLength) throws HL7Exception {
60 super.add(theChild.getClass(), theMinReps > 0, theMaxReps, theLength, EMPTY_OBJECT_ARRAY, theName);
61
62 myMinReps.add(theMinReps);
63 myPrototypes.add(theChild);
64 myPrototypeHasBeenUsed.add(Boolean.FALSE);
65 }
66
67
68
69
70 @Override
71 public String getName() {
72 return myConfDefinition.getName();
73 }
74
75
76
77
78 @Override
79 protected Type createNewTypeWithoutReflection(int theField) {
80 if (Boolean.TRUE.equals(myPrototypeHasBeenUsed.get(theField))) {
81 return myPrototypes.get(theField).instantiateClone();
82 }
83
84 myPrototypeHasBeenUsed.set(theField, Boolean.TRUE);
85 return myPrototypes.get(theField);
86 }
87
88
89
90
91 public Seg getConfDefinition() {
92 return myConfDefinition;
93 }
94
95 public int getMinReps(int theFieldNum) {
96 return myMinReps.get(theFieldNum-1);
97 }
98
99
100
101
102 public ConformanceSegment instantiateClone() throws HL7Exception {
103 ConformanceSegment retVal = new ConformanceSegment((ConformanceStructureHolder) getParent(), myConfDefinition);
104
105 int index = 0;
106 for (ConformanceType<?> next : myPrototypes) {
107 ConformanceType<?> clone = next.instantiateClone();
108 index++;
109 retVal.addChild(clone, getNames()[index-1], getMinReps(index), getMaxCardinality(index), getLength(index));
110 }
111
112 return retVal;
113 }
114
115 public short getMinReps() {
116 return myConfDefinition.getMin();
117 }
118
119 public short getMaxReps() {
120 return myConfDefinition.getMax();
121 }
122
123 }