View Javadoc
1   package ca.uhn.fhir.model.api;
2   
3   /*
4    * #%L
5    * HAPI FHIR - Core Library
6    * %%
7    * Copyright (C) 2014 - 2018 University Health Network
8    * %%
9    * Licensed under the Apache License, Version 2.0 (the "License");
10   * you may not use this file except in compliance with the License.
11   * You may obtain a copy of the License at
12   * 
13   *      http://www.apache.org/licenses/LICENSE-2.0
14   * 
15   * Unless required by applicable law or agreed to in writing, software
16   * distributed under the License is distributed on an "AS IS" BASIS,
17   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
18   * See the License for the specific language governing permissions and
19   * limitations under the License.
20   * #L%
21   */
22  
23  import java.io.Externalizable;
24  import java.io.IOException;
25  import java.io.ObjectInput;
26  import java.io.ObjectOutput;
27  
28  import org.apache.commons.lang3.builder.EqualsBuilder;
29  import org.apache.commons.lang3.builder.HashCodeBuilder;
30  
31  import ca.uhn.fhir.parser.DataFormatException;
32  
33  public abstract class BasePrimitive<T> extends BaseIdentifiableElement implements IPrimitiveDatatype<T>, Externalizable {
34  
35  	private T myCoercedValue;
36  	private String myStringValue;
37  
38  	/**
39  	 * Subclasses must override to convert a "coerced" value into an encoded one.
40  	 * 
41  	 * @param theValue
42  	 *           Will not be null
43  	 * @return May return null if the value does not correspond to anything
44  	 */
45  	protected abstract String encode(T theValue);
46  
47  	@Override
48  	public boolean equals(Object theObj) {
49  		if (theObj == null) {
50  			return false;
51  		}
52  		if (!(theObj.getClass() == getClass())) {
53  			return false;
54  		}
55  
56  		BasePrimitive<?> o = (BasePrimitive<?>) theObj;
57  
58  		EqualsBuilder b = new EqualsBuilder();
59  		b.append(getValue(), o.getValue());
60  		return b.isEquals();
61  	}
62  
63  	@Override
64  	public T getValue() {
65  		return myCoercedValue;
66  	}
67  
68  	@Override
69  	public String getValueAsString() throws DataFormatException {
70  		return myStringValue;
71  	}
72  
73  	@Override
74  	public int hashCode() {
75  		return new HashCodeBuilder().append(getValue()).toHashCode();
76  	}
77  
78  	@Override
79  	public boolean isEmpty() {
80  		return super.isBaseEmpty() && getValue() == null;
81  	}
82  
83  	/**
84  	 * Subclasses must override to convert an encoded representation of this datatype into a "coerced" one
85  	 * 
86  	 * @param theValue
87  	 *           Will not be null
88  	 * @return May return null if the value does not correspond to anything
89  	 */
90  	protected abstract T parse(String theValue);
91  
92  	@Override
93  	public void readExternal(ObjectInput theIn) throws IOException, ClassNotFoundException {
94  		String object = (String) theIn.readObject();
95  		setValueAsString(object);
96  	}
97  
98  	@Override
99  	public BasePrimitive<T> setValue(T theValue) throws DataFormatException {
100 		myCoercedValue = theValue;
101 		updateStringValue();
102 		return this;
103 	}
104 
105 	@Override
106 	public void setValueAsString(String theValue) throws DataFormatException {
107 		if (theValue == null) {
108 			myCoercedValue = null;
109 		} else {
110 			// NB this might be null
111 			myCoercedValue = parse(theValue);
112 		}
113 		myStringValue = theValue;
114 	}
115 
116 	@Override
117 	public String toString() {
118 		return getClass().getSimpleName() + "[" + getValueAsString() + "]";
119 	}
120 
121 	protected void updateStringValue() {
122 		if (myCoercedValue == null) {
123 			myStringValue = null;
124 		} else {
125 			// NB this might be null
126 			myStringValue = encode(myCoercedValue);
127 		}
128 	}
129 
130 	@Override
131 	public void writeExternal(ObjectOutput theOut) throws IOException {
132 		theOut.writeObject(getValueAsString());
133 	}
134 }