
001package org.hl7.fhir.r4.model; 002 003/* 004 Copyright (c) 2011+, HL7, Inc. 005 All rights reserved. 006 007 Redistribution and use in source and binary forms, with or without modification, 008 are permitted provided that the following conditions are met: 009 010 * Redistributions of source code must retain the above copyright notice, this 011 list of conditions and the following disclaimer. 012 * Redistributions in binary form must reproduce the above copyright notice, 013 this list of conditions and the following disclaimer in the documentation 014 and/or other materials provided with the distribution. 015 * Neither the name of HL7 nor the names of its contributors may be used to 016 endorse or promote products derived from this software without specific 017 prior written permission. 018 019 THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND 020 ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED 021 WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. 022 IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, 023 INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT 024 NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR 025 PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, 026 WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) 027 ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE 028 POSSIBILITY OF SUCH DAMAGE. 029 030 */ 031 032 033 034import java.io.IOException; 035import java.io.ObjectInput; 036import java.io.ObjectOutput; 037import java.util.ArrayList; 038 039import org.hl7.fhir.instance.model.api.IBaseEnumeration; 040 041import ca.uhn.fhir.model.api.annotation.DatatypeDef; 042 043/* 044Copyright (c) 2011+, HL7, Inc 045All rights reserved. 046 047Redistribution and use in source and binary forms, with or without modification, 048are permitted provided that the following conditions are met: 049 050 * Redistributions of source code must retain the above copyright notice, this 051 list of conditions and the following disclaimer. 052 * Redistributions in binary form must reproduce the above copyright notice, 053 this list of conditions and the following disclaimer in the documentation 054 and/or other materials provided with the distribution. 055 * Neither the name of HL7 nor the names of its contributors may be used to 056 endorse or promote products derived from this software without specific 057 prior written permission. 058 059THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND 060ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED 061WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. 062IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, 063INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT 064NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR 065PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, 066WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) 067ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE 068POSSIBILITY OF SUCH DAMAGE. 069 070*/ 071 072/** 073 * Primitive type "code" in FHIR, where the code is tied to an enumerated list of possible values 074 * 075 */ 076@DatatypeDef(name = "code", isSpecialization = true) 077public class Enumeration<T extends Enum<?>> extends PrimitiveType<T> implements IBaseEnumeration<T>, ICoding { 078 079 private static final long serialVersionUID = 1L; 080 private EnumFactory<T> myEnumFactory; 081 082 /** 083 * Constructor 084 * 085 * @deprecated This no-arg constructor is provided for serialization only - Do not use 086 */ 087 @Deprecated 088 public Enumeration() { 089 // nothing 090 } 091 092 /** 093 * Constructor 094 */ 095 public Enumeration(EnumFactory<T> theEnumFactory) { 096 if (theEnumFactory == null) 097 throw new IllegalArgumentException("An enumeration factory must be provided"); 098 myEnumFactory = theEnumFactory; 099 } 100 101 /** 102 * Constructor 103 */ 104 public Enumeration(EnumFactory<T> theEnumFactory, String theValue) { 105 if (theEnumFactory == null) 106 throw new IllegalArgumentException("An enumeration factory must be provided"); 107 myEnumFactory = theEnumFactory; 108 setValueAsString(theValue); 109 } 110 111 /** 112 * Constructor 113 */ 114 public Enumeration(EnumFactory<T> theEnumFactory, T theValue) { 115 if (theEnumFactory == null) 116 throw new IllegalArgumentException("An enumeration factory must be provided"); 117 myEnumFactory = theEnumFactory; 118 setValue(theValue); 119 } 120 121 /** 122 * Constructor 123 */ 124 public Enumeration(EnumFactory<T> theEnumFactory, T theValue, Element source) { 125 if (theEnumFactory == null) 126 throw new IllegalArgumentException("An enumeration factory must be provided"); 127 myEnumFactory = theEnumFactory; 128 setValue(theValue); 129 setId(source.getId()); 130 getExtension().addAll(source.getExtension()); 131 } 132 133 @Override 134 public Enumeration<T> copy() { 135 Enumeration dst= new Enumeration(this.myEnumFactory, (Enum)this.getValue()); 136 //Copy the Extension 137 if (extension != null) { 138 dst.extension = new ArrayList(); 139 for (Extension i : extension) 140 dst.extension.add(i.copy()); 141 }; 142 return dst; 143 } 144 145 @Override 146 protected String encode(T theValue) { 147 return myEnumFactory.toCode(theValue); 148 } 149 150 public String fhirType() { 151 return "code"; 152 } 153 154 /** 155 * Provides the enum factory which binds this enumeration to a specific ValueSet 156 */ 157 public EnumFactory<T> getEnumFactory() { 158 return myEnumFactory; 159 } 160 161 @Override 162 protected T parse(String theValue) { 163 if (myEnumFactory != null) { 164 return myEnumFactory.fromCode(theValue); 165 } 166 return null; 167 } 168 169 @SuppressWarnings("unchecked") 170 @Override 171 public void readExternal(ObjectInput theIn) throws IOException, ClassNotFoundException { 172 myEnumFactory = (EnumFactory<T>) theIn.readObject(); 173 super.readExternal(theIn); 174 } 175 176 public String toSystem() { 177 return getEnumFactory().toSystem(getValue()); 178 } 179 180 @Override 181 public void writeExternal(ObjectOutput theOut) throws IOException { 182 theOut.writeObject(myEnumFactory); 183 super.writeExternal(theOut); 184 } 185 186 @Override 187 public String getSystem() { 188 return myEnumFactory.toSystem(myEnumFactory.fromCode(asStringValue())); 189 } 190 191 @Override 192 public boolean hasSystem() { 193 return myEnumFactory.toSystem(myEnumFactory.fromCode(asStringValue())) != null; 194 } 195 196 @Override 197 public String getVersion() { 198 return null; 199 } 200 201 @Override 202 public boolean hasVersion() { 203 return false; 204 } 205 206 @Override 207 public boolean supportsVersion() { 208 return false; 209 } 210 211 @Override 212 public String getCode() { 213 return asStringValue(); 214 } 215 216 @Override 217 public boolean hasCode() { 218 return asStringValue() != null; 219 } 220 221 @Override 222 public String getDisplay() { 223 return null; 224 } 225 226 @Override 227 public boolean hasDisplay() { 228 return false; 229 } 230 231 @Override 232 public boolean supportsDisplay() { 233 return false; 234 } 235}