001package org.hl7.fhir.r5.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  @Override
122  public Enumeration<T> copy() {
123    Enumeration dst= new Enumeration(this.myEnumFactory, (Enum)this.getValue());
124    //Copy the Extension
125    if (extension != null) {
126      dst.extension = new ArrayList();
127      for (Extension i : extension)
128        dst.extension.add(i.copy());
129    };
130    return dst;
131  }
132
133        @Override
134        protected String encode(T theValue) {
135                return myEnumFactory.toCode(theValue);
136        }
137
138        public String fhirType() {
139                return "code";
140        }
141
142        /**
143         * Provides the enum factory which binds this enumeration to a specific ValueSet
144         */
145        public EnumFactory<T> getEnumFactory() {
146                return myEnumFactory;
147        }
148
149        @Override
150        protected T parse(String theValue) {
151                if (myEnumFactory != null) {
152                        return myEnumFactory.fromCode(theValue);
153                }
154                return null;
155        }
156
157        @SuppressWarnings("unchecked")
158        @Override
159        public void readExternal(ObjectInput theIn) throws IOException, ClassNotFoundException {
160                myEnumFactory = (EnumFactory<T>) theIn.readObject();
161                super.readExternal(theIn);
162        }
163
164        public String toSystem() {
165                return getEnumFactory().toSystem(getValue());
166        }
167
168        @Override
169        public void writeExternal(ObjectOutput theOut) throws IOException {
170                theOut.writeObject(myEnumFactory);
171                super.writeExternal(theOut);
172        }
173
174  @Override
175  public String getSystem() {
176    return myEnumFactory.toSystem(myEnumFactory.fromCode(asStringValue()));
177  }
178
179  @Override
180  public boolean hasSystem() {
181    return myEnumFactory.toSystem(myEnumFactory.fromCode(asStringValue())) != null;
182  }
183
184  @Override
185  public String getVersion() {
186    return null;
187  }
188
189  @Override
190  public boolean hasVersion() {
191    return false;
192  }
193
194  @Override
195  public boolean supportsVersion() {
196    return false;
197  }
198
199  @Override
200  public String getCode() {
201    return asStringValue();
202  }
203
204  @Override
205  public boolean hasCode() {
206    return asStringValue() != null;
207  }
208
209  @Override
210  public String getDisplay() {
211    return null;
212  }
213
214  @Override
215  public boolean hasDisplay() {
216    return false;
217  }
218
219  @Override
220  public boolean supportsDisplay() {
221    return false;
222  }
223}