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