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