001/*
002 * #%L
003 * HAPI FHIR - Core Library
004 * %%
005 * Copyright (C) 2014 - 2023 Smile CDR, Inc.
006 * %%
007 * Licensed under the Apache License, Version 2.0 (the "License");
008 * you may not use this file except in compliance with the License.
009 * You may obtain a copy of the License at
010 *
011 *      http://www.apache.org/licenses/LICENSE-2.0
012 *
013 * Unless required by applicable law or agreed to in writing, software
014 * distributed under the License is distributed on an "AS IS" BASIS,
015 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
016 * See the License for the specific language governing permissions and
017 * limitations under the License.
018 * #L%
019 */
020package ca.uhn.fhir.model.primitive;
021
022import java.io.IOException;
023import java.io.ObjectInput;
024import java.io.ObjectOutput;
025
026import org.apache.commons.lang3.Validate;
027
028import ca.uhn.fhir.model.api.IValueSetEnumBinder;
029import ca.uhn.fhir.model.api.annotation.DatatypeDef;
030
031@DatatypeDef(name = "code", isSpecialization = true)
032public class BoundCodeDt<T extends Enum<?>> extends CodeDt {
033
034        private IValueSetEnumBinder<T> myBinder;
035
036        /**
037         * @deprecated This constructor is provided only for serialization support. Do not call it directly!
038         */
039        @Deprecated
040        public BoundCodeDt() {
041                // nothing
042        }
043
044        public BoundCodeDt(IValueSetEnumBinder<T> theBinder) {
045                Validate.notNull(theBinder, "theBinder must not be null");
046                myBinder = theBinder;
047        }
048
049        public BoundCodeDt(IValueSetEnumBinder<T> theBinder, T theValue) {
050                Validate.notNull(theBinder, "theBinder must not be null");
051                myBinder = theBinder;
052                setValueAsEnum(theValue);
053        }
054
055        public IValueSetEnumBinder<T> getBinder() {
056                return myBinder;
057        }
058        
059        public T getValueAsEnum() {
060                Validate.notNull(myBinder, "This object does not have a binder. Constructor BoundCodeDt() should not be called!");
061                T retVal = myBinder.fromCodeString(getValue());
062                if (retVal == null) {
063                        // TODO: throw special exception type?
064                }
065                return retVal;
066        }
067
068        @SuppressWarnings("unchecked")
069        @Override
070        public void readExternal(ObjectInput theIn) throws IOException, ClassNotFoundException {
071                super.readExternal(theIn);
072                myBinder = (IValueSetEnumBinder<T>) theIn.readObject();
073        }
074
075        public void setValueAsEnum(T theValue) {
076                Validate.notNull(myBinder, "This object does not have a binder. Constructor BoundCodeDt() should not be called!");
077                if (theValue==null) {
078                        setValue(null);
079                } else {
080                        setValue(myBinder.toCodeString(theValue));
081                }
082        }
083
084        @Override
085        public void writeExternal(ObjectOutput theOut) throws IOException {
086                super.writeExternal(theOut);
087                theOut.writeObject(myBinder);
088        }
089}