001package org.hl7.fhir.dstu2.model;
002
003/*-
004 * #%L
005 * org.hl7.fhir.dstu2
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 ca.uhn.fhir.model.api.annotation.DatatypeDef;
025import org.hl7.fhir.instance.model.api.IBaseEnumFactory;
026import org.hl7.fhir.instance.model.api.IBaseEnumeration;
027
028/*
029Copyright (c) 2011+, HL7, Inc
030All rights reserved.
031
032Redistribution and use in source and binary forms, with or without modification, 
033are permitted provided that the following conditions are met:
034
035 * Redistributions of source code must retain the above copyright notice, this 
036   list of conditions and the following disclaimer.
037 * Redistributions in binary form must reproduce the above copyright notice, 
038   this list of conditions and the following disclaimer in the documentation 
039   and/or other materials provided with the distribution.
040 * Neither the name of HL7 nor the names of its contributors may be used to 
041   endorse or promote products derived from this software without specific 
042   prior written permission.
043
044THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND 
045ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED 
046WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. 
047IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, 
048INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT 
049NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR 
050PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, 
051WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) 
052ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE 
053POSSIBILITY OF SUCH DAMAGE.
054
055*/
056
057/**
058 * Primitive type "code" in FHIR, where the code is tied to an enumerated list of possible values
059 * 
060 */
061@DatatypeDef(name="code", isSpecialization=true) 
062public class Enumeration<T extends Enum<?>> extends PrimitiveType<T> implements IBaseEnumeration<T> {
063
064        private static final long serialVersionUID = 1L;
065        private EnumFactory<T> myEnumFactory;
066
067        /**
068         * Constructor
069         */
070        public Enumeration(EnumFactory<T> theEnumFactory) {
071                if (theEnumFactory == null)
072                        throw new IllegalArgumentException("An enumeration factory must be provided");
073                myEnumFactory = theEnumFactory;
074        }
075
076        /**
077         * Constructor
078         */
079        public Enumeration(EnumFactory<T> theEnumFactory, T theValue) {
080                if (theEnumFactory == null)
081                        throw new IllegalArgumentException("An enumeration factory must be provided");
082                myEnumFactory = theEnumFactory;
083                setValue(theValue);
084        }
085
086        /**
087         * Constructor
088         */
089        public Enumeration(EnumFactory<T> theEnumFactory, String theValue) {
090                if (theEnumFactory == null)
091                        throw new IllegalArgumentException("An enumeration factory must be provided");
092                myEnumFactory = theEnumFactory;
093                setValueAsString(theValue);
094        }
095
096        @Override
097        protected T parse(String theValue) {
098                if (myEnumFactory != null) {
099                        return myEnumFactory.fromCode(theValue);
100                }
101                return null;
102        }
103
104        @Override
105        protected String encode(T theValue) {
106                return myEnumFactory.toCode(theValue);
107        }
108
109        @Override
110        public Enumeration<T> copy() {
111                return new Enumeration<T>(myEnumFactory, getValue());
112        }
113
114
115        public String fhirType() {
116                return "code";          
117        }
118
119  @Override
120  public IBaseEnumFactory<T> getEnumFactory() {
121    return myEnumFactory;
122  }
123}