View Javadoc
1   package ca.uhn.fhir.model.primitive;
2   
3   import java.io.IOException;
4   import java.io.ObjectInput;
5   import java.io.ObjectOutput;
6   
7   /*
8    * #%L
9    * HAPI FHIR - Core Library
10   * %%
11   * Copyright (C) 2014 - 2018 University Health Network
12   * %%
13   * Licensed under the Apache License, Version 2.0 (the "License");
14   * you may not use this file except in compliance with the License.
15   * You may obtain a copy of the License at
16   * 
17   *      http://www.apache.org/licenses/LICENSE-2.0
18   * 
19   * Unless required by applicable law or agreed to in writing, software
20   * distributed under the License is distributed on an "AS IS" BASIS,
21   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
22   * See the License for the specific language governing permissions and
23   * limitations under the License.
24   * #L%
25   */
26  
27  import org.apache.commons.lang3.Validate;
28  
29  import ca.uhn.fhir.model.api.IValueSetEnumBinder;
30  import ca.uhn.fhir.model.api.annotation.DatatypeDef;
31  
32  @DatatypeDef(name = "code", isSpecialization = true)
33  public class BoundCodeDt<T extends Enum<?>> extends CodeDt {
34  
35  	private IValueSetEnumBinder<T> myBinder;
36  
37  	/**
38  	 * @deprecated This constructor is provided only for serialization support. Do not call it directly!
39  	 */
40  	@Deprecated
41  	public BoundCodeDt() {
42  		// nothing
43  	}
44  
45  	public BoundCodeDt(IValueSetEnumBinder<T> theBinder) {
46  		Validate.notNull(theBinder, "theBinder must not be null");
47  		myBinder = theBinder;
48  	}
49  
50  	public BoundCodeDt(IValueSetEnumBinder<T> theBinder, T theValue) {
51  		Validate.notNull(theBinder, "theBinder must not be null");
52  		myBinder = theBinder;
53  		setValueAsEnum(theValue);
54  	}
55  
56  	public IValueSetEnumBinder<T> getBinder() {
57  		return myBinder;
58  	}
59  	
60  	public T getValueAsEnum() {
61  		Validate.notNull(myBinder, "This object does not have a binder. Constructor BoundCodeDt() should not be called!");
62  		T retVal = myBinder.fromCodeString(getValue());
63  		if (retVal == null) {
64  			// TODO: throw special exception type?
65  		}
66  		return retVal;
67  	}
68  
69  	@SuppressWarnings("unchecked")
70  	@Override
71  	public void readExternal(ObjectInput theIn) throws IOException, ClassNotFoundException {
72  		super.readExternal(theIn);
73  		myBinder = (IValueSetEnumBinder<T>) theIn.readObject();
74  	}
75  
76  	public void setValueAsEnum(T theValue) {
77  		Validate.notNull(myBinder, "This object does not have a binder. Constructor BoundCodeDt() should not be called!");
78  		if (theValue==null) {
79  			setValue(null);
80  		} else {
81  			setValue(myBinder.toCodeString(theValue));
82  		}
83  	}
84  
85  	@Override
86  	public void writeExternal(ObjectOutput theOut) throws IOException {
87  		super.writeExternal(theOut);
88  		theOut.writeObject(myBinder);
89  	}
90  }