001package org.hl7.fhir.dstu3.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;
037
038import org.hl7.fhir.instance.model.api.IBaseEnumeration;
039
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> {
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        @Override
121        public Enumeration<T> copy() {
122                return new Enumeration<T>(myEnumFactory, getValue());
123        }
124
125        @Override
126        protected String encode(T theValue) {
127                return myEnumFactory.toCode(theValue);
128        }
129
130        public String fhirType() {
131                return "code";
132        }
133
134        /**
135         * Provides the enum factory which binds this enumeration to a specific ValueSet
136         */
137        public EnumFactory<T> getEnumFactory() {
138                return myEnumFactory;
139        }
140
141        @Override
142        protected T parse(String theValue) {
143                if (myEnumFactory != null) {
144                        return myEnumFactory.fromCode(theValue);
145                }
146                return null;
147        }
148        
149        @SuppressWarnings("unchecked")
150        @Override
151        public void readExternal(ObjectInput theIn) throws IOException, ClassNotFoundException {
152                myEnumFactory = (EnumFactory<T>) theIn.readObject();
153                super.readExternal(theIn);
154        }
155
156        public String toSystem() {
157                return getEnumFactory().toSystem(getValue());
158        }
159
160        @Override
161        public void writeExternal(ObjectOutput theOut) throws IOException {
162                theOut.writeObject(myEnumFactory);
163                super.writeExternal(theOut);
164        }
165}