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.io.IOException;
29 import java.io.InputStream;
30 import java.util.*;
31
32 import ca.uhn.hl7v2.model.GenericMessage;
33 import ca.uhn.hl7v2.parser.ModelClassFactory;
34 import ca.uhn.hl7v2.parser.Parser;
35
36 public enum Version {
37
38 V21("2.1"),
39 V22("2.2"),
40 V23("2.3"),
41 V231("2.3.1"),
42 V24("2.4"),
43 V25("2.5"),
44 V251("2.5.1"),
45 V26("2.6"),
46 V27("2.7"),
47 V271("2.7.1"),
48 V28("2.8"),
49 V281("2.8.1");
50
51 private final String version;
52 private static ArrayList<Version> ourVersionsOnClasspath;
53 private static final Map<Version, Boolean> ourIsOnClasspath = new HashMap<>();
54
55 Version(String version) {
56 this.version = version;
57 }
58
59
60
61
62 public String getVersion() {
63 return version;
64 }
65
66 public String getPackageVersion() {
67 return "v" + version.replace(".", "");
68 }
69
70
71
72
73
74 public static boolean supportsVersion(String theVersion) {
75 return versionOf(theVersion) != null;
76 }
77
78
79
80
81 public static Version versionOf(String version) {
82 for (Version v : Version.values()) {
83 if (v.getVersion().equals(version)) {
84 return v;
85 }
86 }
87 return null;
88 }
89
90
91
92
93
94 public static boolean allVersions(Set<Version> someVersions) {
95 return someVersions != null && someVersions.size() == values().length;
96 }
97
98
99
100
101 public boolean isGreaterThan(Version theVersion) {
102 return compareTo(theVersion) > 0;
103 }
104
105
106
107
108
109 public static Version latestVersion() {
110 Version[] versions = Version.values();
111 if (versions.length > 0) {
112 return versions[versions.length - 1];
113 } else {
114 return null;
115 }
116 }
117
118 public static Version="../../../ca/uhn/hl7v2/Version.html#Version">Version[] asOf(Version v) {
119 List<Version> versions = new ArrayList<>();
120 for (Version version : Version.values()) {
121 if (version.compareTo(v) >= 0)
122 versions.add(version);
123 }
124 return versions.toArray(new Version[0]);
125 }
126
127 public static Version[] except(Version... v) {
128 Set<Version> versions = new HashSet<>(Arrays.asList(Version.values()));
129 for (Version version : v) {
130 versions.remove(version);
131 }
132 return versions.toArray(new Version[0]);
133 }
134
135 public static Version../../../ca/uhn/hl7v2/Version.html#Version">Version[] before(Version v) {
136 List<Version> versions = new ArrayList<>();
137 for (Version version : Version.values()) {
138 if (version.compareTo(v) < 0)
139 versions.add(version);
140 }
141 return versions.toArray(new Version[0]);
142 }
143
144 public String modelPackageName() {
145 String classname = getClass().getName();
146 String p = classname.substring(0, classname.lastIndexOf("."));
147 return String
148 .format("%s.model.%s.", p, getPackageVersion());
149 }
150
151
152
153
154
155
156 public synchronized boolean available() {
157 Boolean retVal = ourIsOnClasspath.get(this);
158 if (retVal == null) {
159 String resource = "ca/uhn/hl7v2/parser/eventmap/" + getVersion() + ".properties";
160 try (InputStream in = Parser.class.getClassLoader().getResourceAsStream(resource)) {
161 retVal = in != null;
162 ourIsOnClasspath.put(this, retVal);
163 } catch (IOException e) {
164 throw new RuntimeException(e);
165 }
166
167 }
168 return retVal;
169 }
170
171
172
173
174
175 public static synchronized List<Version> availableVersions() {
176 if (ourVersionsOnClasspath == null) {
177 ourVersionsOnClasspath = new ArrayList<>();
178 for (Version next : values()) {
179 if (next.available()) {
180 ourVersionsOnClasspath.add(next);
181 }
182 }
183 }
184 return ourVersionsOnClasspath;
185 }
186
187
188
189
190
191
192
193
194
195
196
197
198 public static Version lowestAvailableVersion() {
199 List<Version> availableVersions = availableVersions();
200 if (availableVersions.size() >0) {
201 return availableVersions.get(0);
202 } else {
203 return null;
204 }
205 }
206
207
208
209
210
211
212
213
214
215
216
217
218
219 public static Version highestAvailableVersionOrDefault() {
220 List<Version> availableVersions = availableVersions();
221 if (availableVersions.size() >0) {
222 return availableVersions.get(availableVersions.size() - 1);
223 } else {
224 return Version.V25;
225 }
226 }
227
228
229
230
231 public GenericMessage newGenericMessage(ModelClassFactory mcf) {
232 switch (this) {
233 case V21:
234 return new GenericMessage.V21(mcf);
235 case V22:
236 return new GenericMessage.V22(mcf);
237 case V23:
238 return new GenericMessage.V23(mcf);
239 case V231:
240 return new GenericMessage.V231(mcf);
241 case V24:
242 return new GenericMessage.V24(mcf);
243 case V25:
244 return new GenericMessage.V25(mcf);
245 case V251:
246 return new GenericMessage.V251(mcf);
247 case V26:
248 return new GenericMessage.V26(mcf);
249 case V27:
250 return new GenericMessage.V27(mcf);
251 case V271:
252 return new GenericMessage.V271(mcf);
253 case V28:
254 return new GenericMessage.V28(mcf);
255 case V281:
256 return new GenericMessage.V281(mcf);
257 default:
258 throw new Error("Unknown version (this is a HAPI bug): " + this.getVersion());
259 }
260 }
261
262 }