001package ca.uhn.fhir.model.primitive; 002 003/* 004 * #%L 005 * HAPI FHIR - Core Library 006 * %% 007 * Copyright (C) 2014 - 2016 University Health Network 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 023import org.apache.commons.lang3.Validate; 024 025import ca.uhn.fhir.model.api.IValueSetEnumBinder; 026import ca.uhn.fhir.model.api.annotation.DatatypeDef; 027 028@DatatypeDef(name = "code", isSpecialization = true) 029public class BoundCodeDt<T extends Enum<?>> extends CodeDt { 030 031 private IValueSetEnumBinder<T> myBinder; 032 033 /** 034 * @deprecated This constructor is provided only for serialization support. Do not call it directly! 035 */ 036 @Deprecated 037 public BoundCodeDt() { 038 // nothing 039 } 040 041 public BoundCodeDt(IValueSetEnumBinder<T> theBinder) { 042 Validate.notNull(theBinder, "theBinder must not be null"); 043 myBinder = theBinder; 044 } 045 046 public BoundCodeDt(IValueSetEnumBinder<T> theBinder, T theValue) { 047 Validate.notNull(theBinder, "theBinder must not be null"); 048 myBinder = theBinder; 049 setValueAsEnum(theValue); 050 } 051 052 public IValueSetEnumBinder<T> getBinder() { 053 return myBinder; 054 } 055 056 public T getValueAsEnum() { 057 Validate.notNull(myBinder, "This object does not have a binder. Constructor BoundCodeDt() should not be called!"); 058 T retVal = myBinder.fromCodeString(getValue()); 059 if (retVal == null) { 060 // TODO: throw special exception type? 061 } 062 return retVal; 063 } 064 065 public void setValueAsEnum(T theValue) { 066 Validate.notNull(myBinder, "This object does not have a binder. Constructor BoundCodeDt() should not be called!"); 067 if (theValue==null) { 068 setValue(null); 069 } else { 070 setValue(myBinder.toCodeString(theValue)); 071 } 072 } 073}