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