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