001package ca.uhn.fhir.context; 002 003/* 004 * #%L 005 * HAPI FHIR - Core Library 006 * %% 007 * Copyright (C) 2014 - 2016 University Health Network 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 023import ca.uhn.fhir.model.api.IFhirVersion; 024import ca.uhn.fhir.rest.server.exceptions.InternalErrorException; 025 026public enum FhirVersionEnum { 027 028 /* 029 * *********************** 030 * Don't auto-sort this type!!! 031 * 032 * Or more accurately, entries should be sorted from OLDEST FHIR release 033 * to NEWEST FHIR release instead of alphabetically 034 * *********************** 035 */ 036 037 DSTU1("ca.uhn.fhir.model.dstu.FhirDstu1", null, false), 038 039 DSTU2("ca.uhn.fhir.model.dstu2.FhirDstu2", null, false), 040 041 DSTU3("org.hl7.fhir.dstu3.hapi.ctx.FhirDstu3", null, true), 042 043 DSTU2_HL7ORG("org.hl7.fhir.instance.FhirDstu2Hl7Org", DSTU2, true); 044 045 private final FhirVersionEnum myEquivalent; 046 private final boolean myIsRi; 047 private volatile Boolean myPresentOnClasspath; 048 private final String myVersionClass; 049 private volatile IFhirVersion myVersionImplementation; 050 051 FhirVersionEnum(String theVersionClass, FhirVersionEnum theEquivalent, boolean theIsRi) { 052 myVersionClass = theVersionClass; 053 myEquivalent = theEquivalent; 054 myIsRi = theIsRi; 055 } 056 057 public IFhirVersion getVersionImplementation() { 058 if (!isPresentOnClasspath()) { 059 throw new IllegalStateException("Version " + name() + " is not present on classpath"); 060 } 061 if (myVersionImplementation == null) { 062 try { 063 myVersionImplementation = (IFhirVersion) Class.forName(myVersionClass).newInstance(); 064 } catch (Exception e) { 065 throw new InternalErrorException("Failed to instantiate FHIR version " + name(), e); 066 } 067 } 068 return myVersionImplementation; 069 } 070 071 public boolean isEquivalentTo(FhirVersionEnum theVersion) { 072 if (this.equals(theVersion)) { 073 return true; 074 } 075 if (myEquivalent != null) { 076 return myEquivalent.equals(theVersion); 077 } 078 return false; 079 } 080 081 public boolean isNewerThan(FhirVersionEnum theVersion) { 082 return ordinal() > theVersion.ordinal(); 083 } 084 085 /** 086 * Returns true if the given version is present on the classpath 087 */ 088 public boolean isPresentOnClasspath() { 089 Boolean retVal = myPresentOnClasspath; 090 if (retVal==null) { 091 try { 092 Class.forName(myVersionClass); 093 retVal= true; 094 } catch (Exception e) { 095 retVal= false; 096 } 097 myPresentOnClasspath = retVal; 098 } 099 return retVal; 100 } 101 102 /** 103 * Is this version using the HL7.org RI structures? 104 */ 105 public boolean isRi() { 106 return myIsRi; 107 } 108 109}