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
27 package ca.uhn.hl7v2.parser;
28
29 import ca.uhn.hl7v2.HL7Exception;
30 import ca.uhn.hl7v2.model.Message;
31
32
33
34
35
36
37
38
39 public class EncodingCharacters implements Cloneable {
40
41 private char fieldSep;
42 private final char[] encChars;
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57 public EncodingCharacters(char fieldSeparator, String encodingCharacters) {
58 this.fieldSep = fieldSeparator;
59 this.encChars = new char[5];
60
61 if (encodingCharacters == null) {
62 setComponentSeparator('^');
63 setRepetitionSeparator('~');
64 setEscapeCharacter('\\');
65 setSubcomponentSeparator('&');
66 setTruncationCharacter('#');
67 } else {
68 encodingCharacters.getChars(0, 4, this.encChars, 0);
69
70 if (encodingCharacters.length() > 4) {
71 char extraChar = encodingCharacters.charAt(4);
72 if (extraChar != fieldSeparator) {
73 setTruncationCharacter(extraChar);
74 }
75 }
76 }
77
78 }
79
80
81
82
83
84
85
86
87
88 public static EncodingCharacters getInstance(Message message) throws HL7Exception {
89
90 final String encodingCharactersValue = message.getEncodingCharactersValue();
91 if (encodingCharactersValue == null || encodingCharactersValue.length() == 0) {
92 throw new HL7Exception("encoding characters not populated");
93 }
94
95 final Character fieldSeparatorValue = message.getFieldSeparatorValue();
96 if (fieldSeparatorValue == null) {
97 throw new HL7Exception("Field separator not populated");
98 }
99
100 return new EncodingCharacters(fieldSeparatorValue, encodingCharactersValue);
101 }
102
103
104
105 public EncodingCharacters(char fieldSeparator, char componentSeparator, char repetitionSeparator,
106 char escapeCharacter, char subcomponentSeparator) {
107 this(fieldSeparator, String.valueOf(componentSeparator) + repetitionSeparator +
108 escapeCharacter + subcomponentSeparator);
109 }
110
111 public EncodingCharacters(char fieldSeparator, char componentSeparator, char repetitionSeparator,
112 char escapeCharacter, char subcomponentSeparator, char truncationCharacter) {
113 this(fieldSeparator, String.valueOf(componentSeparator) + repetitionSeparator +
114 escapeCharacter + subcomponentSeparator + truncationCharacter);
115 }
116
117
118
119 public EncodingCharacters/../../../ca/uhn/hl7v2/parser/EncodingCharacters.html#EncodingCharacters">EncodingCharacters(EncodingCharacters other) {
120 this.fieldSep = other.getFieldSeparator();
121 this.encChars = new char[5];
122 setComponentSeparator(other.getComponentSeparator());
123 setRepetitionSeparator(other.getRepetitionSeparator());
124 setEscapeCharacter(other.getEscapeCharacter());
125 setSubcomponentSeparator(other.getSubcomponentSeparator());
126 setTruncationCharacter(other.getTruncationCharacter());
127 }
128
129
130
131
132
133
134 public char getFieldSeparator() {
135 return this.fieldSep;
136 }
137
138
139
140
141
142
143 public char getComponentSeparator() {
144 return this.encChars[0];
145 }
146
147
148
149
150
151
152 public char getRepetitionSeparator() {
153 return this.encChars[1];
154 }
155
156
157
158
159
160
161 public char getEscapeCharacter() {
162 return this.encChars[2];
163 }
164
165
166
167
168
169
170 public char getSubcomponentSeparator() {
171 return this.encChars[3];
172 }
173
174
175
176
177
178
179 public char getTruncationCharacter() {
180 return this.encChars[4];
181 }
182
183
184
185
186
187 public String toString() {
188 return String.valueOf(encChars);
189 }
190
191 public Object clone() throws CloneNotSupportedException
192 {
193 super.clone();
194 return new EncodingCharacters(this);
195 }
196
197 public void setFieldSeparator(char newFieldSep) {
198 this.fieldSep = newFieldSep;
199 }
200
201 public void setComponentSeparator(char newComponentSep) {
202 this.encChars[0] = newComponentSep;
203 }
204
205 public void setRepetitionSeparator(char newRepetitionSep) {
206 this.encChars[1] = newRepetitionSep;
207 }
208
209 public void setEscapeCharacter(char newEscapeChar) {
210 this.encChars[2] = newEscapeChar;
211 }
212
213 public void setSubcomponentSeparator(char newSubcomponentSep) {
214 this.encChars[3] = newSubcomponentSep;
215 }
216
217 public void setTruncationCharacter(char newTruncationChar) {
218 this.encChars[4] = newTruncationChar;
219 }
220
221
222 public boolean equals(Object o) {
223 if (o instanceof EncodingCharacters) {
224 EncodingCharacters/ca/uhn/hl7v2/parser/EncodingCharacters.html#EncodingCharacters">EncodingCharacters other = (EncodingCharacters) o;
225 return (this.getFieldSeparator() == other.getFieldSeparator()
226 && this.getComponentSeparator() == other.getComponentSeparator()
227 && this.getEscapeCharacter() == other.getEscapeCharacter()
228 && this.getRepetitionSeparator() == other.getRepetitionSeparator()
229 && this.getSubcomponentSeparator() == other.getSubcomponentSeparator()
230 && this.getTruncationCharacter() == other.getTruncationCharacter());
231 } else {
232 return false;
233 }
234 }
235
236
237 public int hashCode() {
238 return 7 * (int) this.getComponentSeparator()
239 * (int) this.getEscapeCharacter()
240 * (int) this.getFieldSeparator()
241 * (int) this.getRepetitionSeparator()
242 * (int) this.getSubcomponentSeparator()
243 * (int) this.getTruncationCharacter();
244 }
245
246
247
248
249
250
251
252 public static EncodingCharacters defaultInstance() {
253 return new EncodingCharacters('|', null);
254 }
255
256 }
257