View Javadoc
1   /**
2   The contents of this file are subject to the Mozilla Public License Version 1.1 
3   (the "License"); you may not use this file except in compliance with the License. 
4   You may obtain a copy of the License at http://www.mozilla.org/MPL/ 
5   Software distributed under the License is distributed on an "AS IS" basis, 
6   WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License for the 
7   specific language governing rights and limitations under the License. 
8   
9   The Original Code is "Version.java".  Description: 
10  "An enumeration of supported HL7 versions" 
11  
12  The Initial Developer of the Original Code is University Health Network. Copyright (C) 
13  2012.  All Rights Reserved. 
14  
15  Contributor(s): ______________________________________. 
16  
17  Alternatively, the contents of this file may be used under the terms of the 
18  GNU General Public License (the  "GPL"), in which case the provisions of the GPL are 
19  applicable instead of those above.  If you wish to allow use of your version of this 
20  file only under the terms of the GPL and not to allow others to use your version 
21  of this file under the MPL, indicate your decision by deleting  the provisions above 
22  and replace  them with the notice and other provisions required by the GPL License.  
23  If you do not delete the provisions above, a recipient may use your version of 
24  this file under either the MPL or the GPL. 
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  	 * Returns a version string (e.g. "2.1", or "2.5.1")
61  	 */
62  	public String getVersion() {
63  		return version;
64  	}
65  
66  	public String getPackageVersion() {
67  		return "v" + version.replace(".", "");
68  	}
69  
70  	/**
71  	 * Returns <code>true</code> if theVersion is a valid HL7
72  	 * version string representing a known version, e.g. "2.4", "2.6"
73  	 */
74  	public static boolean supportsVersion(String theVersion) {
75  		return versionOf(theVersion) != null;
76  	}
77  
78  	/**
79  	 * @param version The version string, e.g. "2.1" or "2.6"
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  	 * @param someVersions set of versions to be tested
92  	 * @return <code>true</code> if someVersions contain all supported HL7 versions
93  	 */
94  	public static boolean allVersions(Set<Version> someVersions) {
95  		return someVersions != null && someVersions.size() == values().length;
96  	}
97  
98  	/**
99  	 * Returns true if this version is greater than the specified version
100 	 */
101 	public boolean isGreaterThan(Version theVersion) {
102 		return compareTo(theVersion) > 0;
103 	}
104 
105 	/**
106 	 * Returns the newest available version of the message structure classes
107 	 * on the classpath, or <code>null</code> if none are found
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 	 * Returns <code>true</code> if the structure
153 	 * classes for this particular version are available
154 	 * on the classpath.
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 			// Ignore
167 		}
168 		return retVal;
169 	}
170 	
171 	/**
172 	 * Returns a list of all versions for which the structure JARs have been
173 	 * found on the classpath.
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 	 * <p>
189 	 * Returns the lowest version for which the structure classes are found
190 	 * on the classes. For instance, if <code>hapi-structures-v24-[version].jar</code>
191 	 * is the only structure JAR on the current JVM classpath, {@link Version#V24} will
192 	 * be returned.
193 	 * <p>
194 	 * <p>
195 	 * Returns <code>null</code> if none are found
196 	 * </p>
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 	 * <p>
209 	 * Returns the highest version for which the structure classes are found
210 	 * on the classes. For instance, if <code>hapi-structures-v24-[version].jar</code>
211 	 * is the only structure JAR on the current JVM classpath, {@link Version#V24} will
212 	 * be returned.
213 	 * <p>
214 	 * </>
215 	 * If no structure JARs at all are found, returns a default value of
216 	 * {@link Version#V25}
217 	 * </p>
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 	 * Construct and return a new {@link GenericMessage} for the given version
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 }