Coverage Report - ca.uhn.hl7v2.Version
 
Classes in this File Line Coverage Branch Coverage Complexity
Version
84%
62/73
71%
35/49
3.176
Version$1
100%
1/1
N/A
3.176
 
 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.InputStream;
 29  
 import java.util.ArrayList;
 30  
 import java.util.HashMap;
 31  
 import java.util.List;
 32  
 import java.util.Map;
 33  
 import java.util.Set;
 34  
 
 35  
 import ca.uhn.hl7v2.model.GenericMessage;
 36  
 import ca.uhn.hl7v2.parser.ModelClassFactory;
 37  
 import ca.uhn.hl7v2.parser.Parser;
 38  
 
 39  12103
 public enum Version {
 40  
 
 41  1
         V21("2.1"), // -
 42  1
         V22("2.2"), // -
 43  1
         V23("2.3"), // -
 44  1
         V231("2.3.1"), // -
 45  1
         V24("2.4"), // -
 46  1
         V25("2.5"), // -
 47  1
         V251("2.5.1"), // -
 48  1
         V26("2.6"); // -
 49  
 
 50  
         private String version;
 51  
         private static ArrayList<Version> ourVersionsOnClasspath;
 52  1
         private static final Map<Version, Boolean> ourIsOnClasspath = new HashMap<Version, Boolean>();
 53  
 
 54  8
         Version(String version) {
 55  8
                 this.version = version;
 56  8
         }
 57  
 
 58  
         /**
 59  
          * Returns a version string (e.g. "2.1", or "2.5.1")
 60  
          */
 61  
         public String getVersion() {
 62  25758
                 return version;
 63  
         }
 64  
 
 65  
         public String getPackageVersion() {
 66  751
                 return "v" + version.replace(".", "");
 67  
         }
 68  
 
 69  
         /**
 70  
          * Returns <code>true</code> if theVersion is a valid HL7
 71  
          * version string representing a known version, e.g. "2.4", "2.6"
 72  
          */
 73  
         public static boolean supportsVersion(String theVersion) {
 74  1847
                 return versionOf(theVersion) != null;
 75  
         }
 76  
 
 77  
         /**
 78  
          * @param version The version string, e.g. "2.1" or "2.6"
 79  
          */
 80  
         public static Version versionOf(String version) {
 81  18861
                 for (Version v : Version.values()) {
 82  18844
                         if (v.getVersion().equals(version)) {
 83  3546
                                 return v;
 84  
                         }
 85  
                 }
 86  17
                 return null;
 87  
         }
 88  
 
 89  
         /**
 90  
          * @param someVersions set of versions to be tested
 91  
          * @return <code>true</code> if someVersions contain all supported HL7 versions
 92  
          */
 93  
         public static boolean allVersions(Set<Version> someVersions) {
 94  6521
                 return someVersions != null && someVersions.size() == values().length;
 95  
         }
 96  
 
 97  
         /**
 98  
          * Returns true if this version is greater than the specified version
 99  
          */
 100  
         public boolean isGreaterThan(Version theVersion) {
 101  293
                 return compareTo(theVersion) > 0;
 102  
         }
 103  
 
 104  
         /**
 105  
          * Returns the newest available version of the message structure classes
 106  
          * on the classpath, or <code>null</code> if none are found
 107  
          */
 108  
         public static Version latestVersion() {
 109  2
                 Version[] versions = Version.values();
 110  2
                 if (versions.length > 0) {
 111  2
                         return versions[versions.length - 1];
 112  
                 } else {
 113  0
                         return null;
 114  
                 }
 115  
         }
 116  
 
 117  
         public static Version[] asOf(Version v) {
 118  461
                 List<Version> versions = new ArrayList<Version>();
 119  4149
                 for (Version version : Version.values()) {
 120  3688
                         if (version.compareTo(v) >= 0)
 121  1383
                                 versions.add(version);
 122  
                 }
 123  461
                 return versions.toArray(new Version[versions.size()]);
 124  
         }
 125  
 
 126  
         public static Version[] except(Version v) {
 127  6
                 List<Version> versions = new ArrayList<Version>();
 128  54
                 for (Version version : Version.values()) {
 129  48
                         if (version.compareTo(v) != 0)
 130  42
                                 versions.add(version);
 131  
                 }
 132  6
                 return versions.toArray(new Version[versions.size()]);
 133  
         }
 134  
 
 135  
         public static Version[] before(Version v) {
 136  464
                 List<Version> versions = new ArrayList<Version>();
 137  4176
                 for (Version version : Version.values()) {
 138  3712
                         if (version.compareTo(v) < 0)
 139  2320
                                 versions.add(version);
 140  
                 }
 141  464
                 return versions.toArray(new Version[versions.size()]);
 142  
         }
 143  
 
 144  
         public String modelPackageName() {
 145  735
                 return String
 146  
                                 .format("%s.model.%s.", getClass().getPackage().getName(), getPackageVersion());
 147  
         }
 148  
 
 149  
         /**
 150  
          * Returns <code>true</code> if the structure
 151  
          * classes for this particular version are available
 152  
          * on the classpath.
 153  
          */
 154  
         public synchronized boolean available() {
 155  154
                 Boolean retVal = ourIsOnClasspath.get(this);
 156  154
                 if (retVal == null) {
 157  8
                         String resource = "ca/uhn/hl7v2/parser/eventmap/" + getVersion() + ".properties";
 158  8
                         InputStream in = Parser.class.getClassLoader().getResourceAsStream(resource);
 159  8
                         retVal = in != null;
 160  8
                         ourIsOnClasspath.put(this, retVal);
 161  
                 }
 162  154
                 return retVal;
 163  
         }
 164  
         
 165  
         /**
 166  
          * Returns a list of all versions for which the structure JARs have been
 167  
          * found on the classpath.
 168  
          */
 169  
         public static synchronized List<Version> availableVersions() {
 170  7
                 if (ourVersionsOnClasspath == null) {
 171  1
                         ourVersionsOnClasspath = new ArrayList<Version>();
 172  9
                         for (Version next : values()) {
 173  8
                                 if (next.available()) {
 174  8
                                         ourVersionsOnClasspath.add(next);
 175  
                                 }
 176  
                         }
 177  
                 }
 178  7
                 return ourVersionsOnClasspath;
 179  
         }
 180  
         
 181  
         /**
 182  
          * <p>
 183  
          * Returns the lowest version for which the structure classes are found
 184  
          * on the classes. For instance, if <code>hapi-structures-v24-[version].jar</code>
 185  
          * is the only structure JAR on the current JVM classpath, {@link Version#V24} will
 186  
          * be returned.
 187  
          * <p>
 188  
          * <p>
 189  
          * Returns <code>null</code> if none are found
 190  
          * </p>
 191  
          */
 192  
         public static Version lowestAvailableVersion() {
 193  1
                 List<Version> availableVersions = availableVersions();
 194  1
                 if (availableVersions.size() >0) {
 195  1
                         return availableVersions.get(0);
 196  
                 } else {
 197  0
                         return null;
 198  
                 }
 199  
         }
 200  
 
 201  
         /**
 202  
          * <p>
 203  
          * Returns the highest version for which the structure classes are found
 204  
          * on the classes. For instance, if <code>hapi-structures-v24-[version].jar</code>
 205  
          * is the only structure JAR on the current JVM classpath, {@link Version#V24} will
 206  
          * be returned.
 207  
          * <p>
 208  
          * </>
 209  
          * If no structure JARs at all are found, returns a default value of
 210  
          * {@link Version#V25}
 211  
          * </p>
 212  
          */
 213  
         public static Version highestAvailableVersionOrDefault() {
 214  4
                 List<Version> availableVersions = availableVersions();
 215  4
                 if (availableVersions.size() >0) {
 216  4
                         return availableVersions.get(availableVersions.size() - 1);
 217  
                 } else {
 218  0
                         return Version.V25;
 219  
                 }
 220  
         }
 221  
 
 222  
         /**
 223  
          * Construct and return a new {@link GenericMessage} for the given version
 224  
          */
 225  
         public GenericMessage newGenericMessage(ModelClassFactory mcf) {
 226  1
                 switch (this) {
 227  
                 case V21:
 228  0
                         return new GenericMessage.V21(mcf);
 229  
                 case V22:
 230  0
                         return new GenericMessage.V22(mcf);
 231  
                 case V23:
 232  0
                         return new GenericMessage.V23(mcf);
 233  
                 case V231:
 234  0
                         return new GenericMessage.V231(mcf);
 235  
                 case V24:
 236  0
                         return new GenericMessage.V24(mcf);
 237  
                 case V25:
 238  0
                         return new GenericMessage.V25(mcf);
 239  
                 case V251:
 240  0
                         return new GenericMessage.V251(mcf);
 241  
                 case V26:
 242  1
                         return new GenericMessage.V26(mcf);
 243  
                 default:
 244  0
                         throw new Error("Unknown version (this is a HAPI bug): " + this.getVersion());
 245  
                 }
 246  
         }
 247  
         
 248  
 }