Coverage Report - ca.uhn.hl7v2.Version
 
Classes in this File Line Coverage Branch Coverage Complexity
Version
79%
69/87
67%
36/53
3.706
Version$1
0%
0/1
N/A
3.706
 
 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.*;
 30  
 
 31  
 import ca.uhn.hl7v2.model.GenericMessage;
 32  
 import ca.uhn.hl7v2.parser.ModelClassFactory;
 33  
 import ca.uhn.hl7v2.parser.Parser;
 34  
 
 35  118736
 public enum Version {
 36  
 
 37  5
         V21("2.1"), // -
 38  5
         V22("2.2"), // -
 39  5
         V23("2.3"), // -
 40  5
         V231("2.3.1"), // -
 41  5
         V24("2.4"), // -
 42  5
         V25("2.5"), // -
 43  5
         V251("2.5.1"), // -
 44  5
         V26("2.6"), // -
 45  5
         V27("2.7"), // -
 46  5
         V271("2.7.1"), // -
 47  5
         V28("2.8"), // -
 48  5
         V281("2.8.1"); // -
 49  
 
 50  
         private String version;
 51  
         private static ArrayList<Version> ourVersionsOnClasspath;
 52  5
         private static final Map<Version, Boolean> ourIsOnClasspath = new HashMap<Version, Boolean>();
 53  
 
 54  60
         Version(String version) {
 55  60
                 this.version = version;
 56  60
         }
 57  
 
 58  
         /**
 59  
          * Returns a version string (e.g. "2.1", or "2.5.1")
 60  
          */
 61  
         public String getVersion() {
 62  191413
                 return version;
 63  
         }
 64  
 
 65  
         public String getPackageVersion() {
 66  1045
                 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  9778
                 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  103660
                 for (Version v : Version.values()) {
 82  103575
                         if (v.getVersion().equals(version)) {
 83  19066
                                 return v;
 84  
                         }
 85  
                 }
 86  85
                 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  76520
                 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  4899
                 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  10
                 Version[] versions = Version.values();
 110  10
                 if (versions.length > 0) {
 111  10
                         return versions[versions.length - 1];
 112  
                 } else {
 113  0
                         return null;
 114  
                 }
 115  
         }
 116  
 
 117  
         public static Version[] asOf(Version v) {
 118  5430
                 List<Version> versions = new ArrayList<Version>();
 119  70590
                 for (Version version : Version.values()) {
 120  65160
                         if (version.compareTo(v) >= 0)
 121  38010
                                 versions.add(version);
 122  
                 }
 123  5430
                 return versions.toArray(new Version[versions.size()]);
 124  
         }
 125  
 
 126  
     public static Version[] except(Version... v) {
 127  40
         Set<Version> versions = new HashSet<Version>(Arrays.asList(Version.values()));
 128  140
         for (Version version : v) {
 129  100
             versions.remove(version);
 130  
         }
 131  40
         return versions.toArray(new Version[versions.size()]);
 132  
     }
 133  
 
 134  
         public static Version[] before(Version v) {
 135  5445
                 List<Version> versions = new ArrayList<Version>();
 136  70785
                 for (Version version : Version.values()) {
 137  65340
                         if (version.compareTo(v) < 0)
 138  27225
                                 versions.add(version);
 139  
                 }
 140  5445
                 return versions.toArray(new Version[versions.size()]);
 141  
         }
 142  
 
 143  
         public String modelPackageName() {
 144  925
         String classname = getClass().getName();
 145  925
         String p = classname.substring(0, classname.lastIndexOf("."));
 146  1850
                 return String
 147  925
                                 .format("%s.model.%s.", p, getPackageVersion());
 148  
         }
 149  
 
 150  
         /**
 151  
          * Returns <code>true</code> if the structure
 152  
          * classes for this particular version are available
 153  
          * on the classpath.
 154  
          */
 155  
         public synchronized boolean available() {
 156  863
                 Boolean retVal = ourIsOnClasspath.get(this);
 157  863
                 if (retVal == null) {
 158  60
                         String resource = "ca/uhn/hl7v2/parser/eventmap/" + getVersion() + ".properties";
 159  60
             InputStream in = Parser.class.getClassLoader().getResourceAsStream(resource);
 160  
             try {
 161  60
                             retVal = in != null;
 162  60
                             ourIsOnClasspath.put(this, retVal);
 163  
             } finally {
 164  60
                 if (in != null) {
 165  
                     try {
 166  55
                         in.close();
 167  0
                     } catch (Exception e) {
 168  
                         // Ignore
 169  55
                     }
 170  
                 }
 171  
             }
 172  
                 }
 173  863
                 return retVal;
 174  
         }
 175  
         
 176  
         /**
 177  
          * Returns a list of all versions for which the structure JARs have been
 178  
          * found on the classpath.
 179  
          */
 180  
         public static synchronized List<Version> availableVersions() {
 181  30
                 if (ourVersionsOnClasspath == null) {
 182  5
                         ourVersionsOnClasspath = new ArrayList<Version>();
 183  65
                         for (Version next : values()) {
 184  60
                                 if (next.available()) {
 185  55
                                         ourVersionsOnClasspath.add(next);
 186  
                                 }
 187  
                         }
 188  
                 }
 189  30
                 return ourVersionsOnClasspath;
 190  
         }
 191  
         
 192  
         /**
 193  
          * <p>
 194  
          * Returns the lowest version for which the structure classes are found
 195  
          * on the classes. For instance, if <code>hapi-structures-v24-[version].jar</code>
 196  
          * is the only structure JAR on the current JVM classpath, {@link Version#V24} will
 197  
          * be returned.
 198  
          * <p>
 199  
          * <p>
 200  
          * Returns <code>null</code> if none are found
 201  
          * </p>
 202  
          */
 203  
         public static Version lowestAvailableVersion() {
 204  5
                 List<Version> availableVersions = availableVersions();
 205  5
                 if (availableVersions.size() >0) {
 206  5
                         return availableVersions.get(0);
 207  
                 } else {
 208  0
                         return null;
 209  
                 }
 210  
         }
 211  
 
 212  
         /**
 213  
          * <p>
 214  
          * Returns the highest version for which the structure classes are found
 215  
          * on the classes. For instance, if <code>hapi-structures-v24-[version].jar</code>
 216  
          * is the only structure JAR on the current JVM classpath, {@link Version#V24} will
 217  
          * be returned.
 218  
          * <p>
 219  
          * </>
 220  
          * If no structure JARs at all are found, returns a default value of
 221  
          * {@link Version#V25}
 222  
          * </p>
 223  
          */
 224  
         public static Version highestAvailableVersionOrDefault() {
 225  15
                 List<Version> availableVersions = availableVersions();
 226  15
                 if (availableVersions.size() >0) {
 227  15
                         return availableVersions.get(availableVersions.size() - 1);
 228  
                 } else {
 229  0
                         return Version.V25;
 230  
                 }
 231  
         }
 232  
 
 233  
         /**
 234  
          * Construct and return a new {@link GenericMessage} for the given version
 235  
          */
 236  
         public GenericMessage newGenericMessage(ModelClassFactory mcf) {
 237  0
                 switch (this) {
 238  
                 case V21:
 239  0
                         return new GenericMessage.V21(mcf);
 240  
                 case V22:
 241  0
                         return new GenericMessage.V22(mcf);
 242  
                 case V23:
 243  0
                         return new GenericMessage.V23(mcf);
 244  
                 case V231:
 245  0
                         return new GenericMessage.V231(mcf);
 246  
                 case V24:
 247  0
                         return new GenericMessage.V24(mcf);
 248  
                 case V25:
 249  0
                         return new GenericMessage.V25(mcf);
 250  
                 case V251:
 251  0
                         return new GenericMessage.V251(mcf);
 252  
                 case V26:
 253  0
                         return new GenericMessage.V26(mcf);
 254  
                 case V27:
 255  0
                         return new GenericMessage.V27(mcf);
 256  
                 case V271:
 257  0
                         return new GenericMessage.V271(mcf);
 258  
                 case V28:
 259  0
                         return new GenericMessage.V28(mcf);
 260  
                 case V281:
 261  0
                         return new GenericMessage.V281(mcf);
 262  
                 default:
 263  0
                         throw new Error("Unknown version (this is a HAPI bug): " + this.getVersion());
 264  
                 }
 265  
         }
 266  
         
 267  
 }