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