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; |
27 | |
|
28 | |
import java.util.Stack; |
29 | |
|
30 | |
|
31 | |
|
32 | |
|
33 | |
|
34 | |
|
35 | |
public class Location { |
36 | |
|
37 | 3725 | private Stack<GroupLocation> groups = new Stack<GroupLocation>(); |
38 | 3725 | private String segmentName = null; |
39 | 3725 | private int segmentRepetition = 0; |
40 | 3725 | private int field = -1; |
41 | 3725 | private int fieldRepetition = 0; |
42 | 3725 | private int component = -1; |
43 | 3725 | private int subcomponent = -1; |
44 | |
|
45 | |
private boolean componentLevel; |
46 | |
|
47 | 5 | public static final Location UNKNOWN = new Location(); |
48 | |
|
49 | 360 | public Location() { |
50 | 360 | } |
51 | |
|
52 | 3365 | public Location(Location l) { |
53 | 3365 | this.groups = new Stack<GroupLocation>(); |
54 | 3365 | groups.addAll(l.groups); |
55 | 3365 | this.segmentName = l.segmentName; |
56 | 3365 | this.segmentRepetition = l.segmentRepetition; |
57 | 3365 | this.field = l.field; |
58 | 3365 | this.fieldRepetition = l.fieldRepetition; |
59 | 3365 | this.component = l.component; |
60 | 3365 | this.subcomponent = l.subcomponent; |
61 | 3365 | } |
62 | |
|
63 | |
|
64 | |
|
65 | |
public boolean isUnknown() { |
66 | 675 | return this == UNKNOWN; |
67 | |
} |
68 | |
|
69 | |
public Location pushGroup(String name, int rep) { |
70 | 50 | groups.push(new Location.GroupLocation(name, rep)); |
71 | 50 | return this; |
72 | |
} |
73 | |
|
74 | |
public void popGroup() { |
75 | 0 | groups.pop(); |
76 | 0 | } |
77 | |
|
78 | |
public GroupLocation getGroupLocation() { |
79 | 0 | return groups.peek(); |
80 | |
} |
81 | |
|
82 | |
public String getSegmentName() { |
83 | 90 | return segmentName; |
84 | |
} |
85 | |
|
86 | |
public Location withSegmentName(String segmentName) { |
87 | 425 | this.segmentName = segmentName; |
88 | 425 | return this; |
89 | |
} |
90 | |
|
91 | |
public boolean isComponentLevel() { |
92 | 100 | return componentLevel; |
93 | |
} |
94 | |
|
95 | |
public Location atComponentLevel(boolean componentLevel) { |
96 | 100 | this.componentLevel = componentLevel; |
97 | 100 | return this; |
98 | |
} |
99 | |
|
100 | |
public int getSegmentRepetition() { |
101 | 30 | return segmentRepetition; |
102 | |
} |
103 | |
|
104 | |
public Location withSegmentRepetition(int segmentRepetition) { |
105 | 80 | this.segmentRepetition = segmentRepetition; |
106 | 80 | return this; |
107 | |
} |
108 | |
|
109 | |
public int getField() { |
110 | 1470 | return field; |
111 | |
} |
112 | |
|
113 | |
public Location withField(int field) { |
114 | 2185 | this.field = field; |
115 | 2185 | return this; |
116 | |
} |
117 | |
|
118 | |
public int getFieldRepetition() { |
119 | 1410 | return fieldRepetition; |
120 | |
} |
121 | |
|
122 | |
public Location withFieldRepetition(int fieldRepetition) { |
123 | 0 | this.fieldRepetition = fieldRepetition; |
124 | 0 | return this; |
125 | |
} |
126 | |
|
127 | |
public int getComponent() { |
128 | 1410 | return component; |
129 | |
} |
130 | |
|
131 | |
public Location withComponent(int component) { |
132 | 1090 | this.component = component; |
133 | 1090 | return this; |
134 | |
} |
135 | |
|
136 | |
public int getSubcomponent() { |
137 | 30 | return subcomponent; |
138 | |
} |
139 | |
|
140 | |
public Location withSubcomponent(int subcomponent) { |
141 | 290 | this.subcomponent = subcomponent; |
142 | 290 | return this; |
143 | |
} |
144 | |
|
145 | |
|
146 | |
|
147 | |
|
148 | |
|
149 | |
|
150 | |
public Location withFieldIndizes(int[] indices) { |
151 | 5 | field = indices[0]; |
152 | 5 | fieldRepetition = indices[1]; |
153 | 5 | component = indices[2]; |
154 | 5 | subcomponent = indices[3]; |
155 | 5 | return this; |
156 | |
} |
157 | |
|
158 | |
|
159 | |
|
160 | |
@Override |
161 | |
public String toString() { |
162 | 295 | StringBuilder sb = new StringBuilder(); |
163 | 295 | if (!groups.isEmpty()) { |
164 | 145 | sb.append('/'); |
165 | 145 | for (GroupLocation gl : groups) { |
166 | 240 | sb.append(gl.groupName); |
167 | 240 | if (gl.repetition >= 0) { |
168 | 105 | sb.append('(') |
169 | 105 | .append(gl.repetition) |
170 | 105 | .append(")"); |
171 | |
} |
172 | 240 | sb.append("/"); |
173 | 240 | } |
174 | |
} |
175 | 295 | if (segmentName != null) { |
176 | 295 | sb.append(segmentName); |
177 | 295 | if (segmentRepetition > 0) { |
178 | 45 | sb.append("(").append(segmentRepetition).append(")"); |
179 | |
} |
180 | 295 | if (field > 0) { |
181 | 290 | sb.append("-").append(field); |
182 | 290 | if (fieldRepetition >= 0) { |
183 | 290 | sb.append("(").append(fieldRepetition).append(")"); |
184 | |
} |
185 | 290 | if (component > 0) { |
186 | 145 | sb.append("-").append(component); |
187 | 145 | if (subcomponent > 0) { |
188 | 15 | sb.append("-").append(subcomponent); |
189 | |
} |
190 | |
} |
191 | |
} |
192 | |
} else { |
193 | 0 | if (sb.length() == 0) sb.append("unknown location"); |
194 | |
} |
195 | 295 | return sb.toString(); |
196 | |
} |
197 | |
|
198 | |
@Override |
199 | |
public int hashCode() { |
200 | 440 | final int prime = 31; |
201 | 440 | int result = 1; |
202 | 440 | result = prime * result + ((groups == null) ? 0 : groups.hashCode()); |
203 | 440 | result = prime * result + component; |
204 | 440 | result = prime * result + field; |
205 | 440 | result = prime * result + fieldRepetition; |
206 | 440 | result = prime * result + ((segmentName == null) ? 0 : segmentName.hashCode()); |
207 | 440 | result = prime * result + segmentRepetition; |
208 | 440 | result = prime * result + subcomponent; |
209 | 440 | return result; |
210 | |
} |
211 | |
|
212 | |
@Override |
213 | |
public boolean equals(Object obj) { |
214 | 0 | if (this == obj) |
215 | 0 | return true; |
216 | 0 | if (obj == null) |
217 | 0 | return false; |
218 | 0 | if (getClass() != obj.getClass()) |
219 | 0 | return false; |
220 | 0 | Location other = (Location) obj; |
221 | 0 | if (groups == null) { |
222 | 0 | if (other.groups != null) |
223 | 0 | return false; |
224 | 0 | } else if (!groups.equals(other.groups)) |
225 | 0 | return false; |
226 | 0 | if (component != other.component) |
227 | 0 | return false; |
228 | 0 | if (field != other.field) |
229 | 0 | return false; |
230 | 0 | if (fieldRepetition != other.fieldRepetition) |
231 | 0 | return false; |
232 | 0 | if (segmentName == null) { |
233 | 0 | if (other.segmentName != null) |
234 | 0 | return false; |
235 | 0 | } else if (!segmentName.equals(other.segmentName)) |
236 | 0 | return false; |
237 | 0 | if (segmentRepetition != other.segmentRepetition) |
238 | 0 | return false; |
239 | 0 | if (subcomponent != other.subcomponent) |
240 | 0 | return false; |
241 | 0 | return true; |
242 | |
} |
243 | |
|
244 | 50 | private static class GroupLocation { |
245 | |
String groupName; |
246 | |
int repetition; |
247 | |
|
248 | 50 | private GroupLocation(String groupName, int repetition) { |
249 | 50 | this.groupName = groupName; |
250 | 50 | this.repetition = repetition; |
251 | 50 | } |
252 | |
|
253 | |
public String getGroupName() { |
254 | 0 | return groupName; |
255 | |
} |
256 | |
|
257 | |
public int getRepetition() { |
258 | 0 | return repetition; |
259 | |
} |
260 | |
} |
261 | |
|
262 | |
} |