001package org.hl7.fhir.utilities; 002 003/*- 004 * #%L 005 * org.hl7.fhir.r5 006 * %% 007 * Copyright (C) 2014 - 2019 Health Level 7 008 * %% 009 * Licensed under the Apache License, Version 2.0 (the "License"); 010 * you may not use this file except in compliance with the License. 011 * You may obtain a copy of the License at 012 * 013 * http://www.apache.org/licenses/LICENSE-2.0 014 * 015 * Unless required by applicable law or agreed to in writing, software 016 * distributed under the License is distributed on an "AS IS" BASIS, 017 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 018 * See the License for the specific language governing permissions and 019 * limitations under the License. 020 * #L% 021 */ 022 023public class VersionUtilities { 024 025 026 public static final String CURRENT_VERSION = "4.2"; 027 public static final String CURRENT_FULL_VERSION = "4.2.0"; 028 029 public static String packageForVersion(String v) { 030 if (isR2Ver(v)) { 031 return "hl7.fhir.r2.core"; 032 } 033 if (isR2BVer(v)) { 034 return "hl7.fhir.r2b.core"; 035 } 036 if (isR3Ver(v)) { 037 return "hl7.fhir.r3.core"; 038 } 039 if (isR4Ver(v)) { 040 return "hl7.fhir.r4.core"; 041 } 042 if ("current".equals(v)) { 043 return "hl7.fhir.r5.core"; 044 } 045 if (v != null && v.startsWith(CURRENT_VERSION)) { 046 return "hl7.fhir.r5.core"; 047 } 048 return null; 049 } 050 051 public static String getCurrentVersion(String v) { 052 if (isR2Ver(v)) { 053 return "1.0.2"; 054 } 055 if (isR2BVer(v)) { 056 return "1.4.0"; 057 } 058 if (isR3Ver(v)) { 059 return "3.0.2"; 060 } 061 if (isR4Ver(v)) { 062 return "4.0.1"; 063 } 064 if (v != null && v.startsWith(CURRENT_VERSION)) { 065 return "current"; 066 } 067 return v; 068 } 069 070 public static String getCurrentPackageVersion(String v) { 071 if (isR2Ver(v)) { 072 return "1.0"; 073 } 074 if (isR2BVer(v)) { 075 return "1.4"; 076 } 077 if (isR3Ver(v)) { 078 return "3.0"; 079 } 080 if (isR4Ver(v)) { 081 return "4.0"; 082 } 083 if (v != null && v.startsWith(CURRENT_VERSION)) { 084 return "current"; 085 } 086 return v; 087 } 088 089 public static boolean isSupportedVersion(String version) { 090 return Utilities.existsInList(version, "1.0.2", "1.4.0", "3.0.2", "4.0.1", CURRENT_FULL_VERSION); 091 } 092 093 public static String listSupportedVersions() { 094 return "1.0.2, 1.4.0, 3.0.2, 4.0.1, "+CURRENT_FULL_VERSION; 095 } 096 097 public static boolean isR5Ver(String ver) { 098 return ver != null && ver.startsWith(CURRENT_VERSION); 099 } 100 101 public static boolean isR4Ver(String ver) { 102 return ver != null && ver.startsWith("4.0"); 103 } 104 105 public static boolean isR3Ver(String ver) { 106 return ver != null && ver.startsWith("3.0"); 107 } 108 109 public static boolean isR2BVer(String ver) { 110 return ver != null && ver.startsWith("1.4"); 111 } 112 113 public static boolean isR2Ver(String ver) { 114 return ver != null && ver.startsWith("1.0"); 115 } 116 117 public static boolean versionsCompatible(String v1, String v2) { 118 String mm1 = getMajMin(v1); 119 String mm2 = getMajMin(v2); 120 if (mm1 == null || mm2 == null) { 121 return false; 122 } else { 123 return mm1.equals(mm2); 124 } 125 } 126 127 public static boolean isCorePackage(String s) { 128 if (s.contains("#")) { 129 s = s.substring(0, s.indexOf("#")); 130 } 131 return Utilities.existsInList(s, "hl7.fhir.core","hl7.fhir.r2.core", "hl7.fhir.r2b.core", "hl7.fhir.r3.core", "hl7.fhir.r4.core"); 132 } 133 134 public static String getMajMin(String version) { 135 if (version == null) 136 return null; 137 138 if (Utilities.charCount(version, '.') == 1) { 139 String[] p = version.split("\\."); 140 return p[0]+"."+p[1]; 141 } else if (Utilities.charCount(version, '.') == 2) { 142 String[] p = version.split("\\."); 143 return p[0]+"."+p[1]; 144 } else { 145 return null; 146 } 147 } 148 149 public static boolean isSemVer(String version) { 150 if (Utilities.charCount(version, '.') != 2) { 151 return false; 152 } 153 String[] p = version.split("\\."); 154 return Utilities.isInteger(p[0]) && Utilities.isInteger(p[1]) && Utilities.isInteger(p[2]); 155 } 156 157 /** 158 * return true if the current vresion equals test, or later 159 * 160 * so if a feature is defined in 4.0, if (VersionUtilities.isThisOrLater("4.0", version))... 161 * 162 * @param test 163 * @param current 164 * @return 165 */ 166 public static boolean isThisOrLater(String test, String current) { 167 String t = getMajMin(test); 168 String c = getMajMin(current); 169 boolean ok = c.compareTo(t) >= 0; 170 return ok; 171 } 172 173}