View Javadoc
1   package ca.uhn.fhir.rest.param;
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.math.BigDecimal;
24  import java.util.List;
25  
26  import org.apache.commons.lang3.StringUtils;
27  import org.apache.commons.lang3.builder.ToStringBuilder;
28  import org.apache.commons.lang3.builder.ToStringStyle;
29  import org.hl7.fhir.instance.model.api.IPrimitiveType;
30  
31  import ca.uhn.fhir.context.FhirContext;
32  import ca.uhn.fhir.model.api.IQueryParameterType;
33  import ca.uhn.fhir.model.primitive.DecimalDt;
34  import ca.uhn.fhir.model.primitive.UriDt;
35  import ca.uhn.fhir.util.CoverageIgnore;
36  
37  public class QuantityParam extends BaseParamWithPrefix<QuantityParam> implements IQueryParameterType {
38  
39  	private static final long serialVersionUID = 1L;
40  	private BigDecimal myValue;
41  	private String mySystem;
42  	private String myUnits;
43  
44  	/**
45  	 * Constructor
46  	 */
47  	public QuantityParam() {
48  		super();
49  	}
50  
51  	/**
52  	 * Constructor
53  	 * 
54  	 * @param thePrefix
55  	 *           The comparator, or <code>null</code> for an equals comparator
56  	 * @param theValue
57  	 *           A quantity value
58  	 * @param theSystem
59  	 *           The unit system
60  	 * @param theUnits
61  	 *           The unit code
62  	 */
63  	public QuantityParam(ParamPrefixEnum thePrefix, BigDecimal theValue, String theSystem, String theUnits) {
64  		setPrefix(thePrefix);
65  		setValue(theValue);
66  		setSystem(theSystem);
67  		setUnits(theUnits);
68  	}
69  
70  	/**
71  	 * Constructor
72  	 * 
73  	 * @param thePrefix
74  	 *           The comparator, or <code>null</code> for an equals comparator
75  	 * @param theValue
76  	 *           A quantity value
77  	 * @param theSystem
78  	 *           The unit system
79  	 * @param theUnits
80  	 *           The unit code
81  	 */
82  	public QuantityParam(ParamPrefixEnum thePrefix, double theValue, String theSystem, String theUnits) {
83  		setPrefix(thePrefix);
84  		setValue(theValue);
85  		setSystem(theSystem);
86  		setUnits(theUnits);
87  	}
88  
89  	/**
90  	 * Constructor
91  	 * 
92  	 * @param thePrefix
93  	 *           The comparator, or <code>null</code> for an equals comparator
94  	 * @param theValue
95  	 *           A quantity value
96  	 * @param theSystem
97  	 *           The unit system
98  	 * @param theUnits
99  	 *           The unit code
100 	 */
101 	public QuantityParam(ParamPrefixEnum thePrefix, long theValue, String theSystem, String theUnits) {
102 		setPrefix(thePrefix);
103 		setValue(theValue);
104 		setSystem(theSystem);
105 		setUnits(theUnits);
106 	}
107 
108 	/**
109 	 * Constructor
110 	 * 
111 	 * @param theQuantity
112 	 *           A quantity value (with no system or units), such as "100.0" or "gt4"
113 	 */
114 	public QuantityParam(String theQuantity) {
115 		setValueAsQueryToken(null, null, null, theQuantity);
116 	}
117 
118 	/**
119 	 * Constructor
120 	 * 
121 	 * @param theQuantity
122 	 *           A quantity value (with no system or units), such as <code>100</code>
123 	 */
124 	public QuantityParam(long theQuantity) {
125 		setValueAsQueryToken(null, null, null, Long.toString(theQuantity));
126 	}
127 
128 	/**
129 	 * Constructor
130 	 * 
131 	 * @param theQuantity
132 	 *           A quantity value (with no system or units), such as "100.0" or "&lt;=4"
133 	 * @param theSystem
134 	 *           The unit system
135 	 * @param theUnits
136 	 *           The unit code
137 	 */
138 	public QuantityParam(String theQuantity, String theSystem, String theUnits) {
139 		setValueAsQueryToken(null, null, null, theQuantity);
140 		setSystem(theSystem);
141 		setUnits(theUnits);
142 	}
143 
144 	private void clear() {
145 		setPrefix(null);
146 		setSystem((String) null);
147 		setUnits(null);
148 		setValue((BigDecimal) null);
149 	}
150 
151 	@Override
152 	String doGetQueryParameterQualifier() {
153 		return null;
154 	}
155 
156 	@Override
157 	String doGetValueAsQueryToken(FhirContext theContext) {
158 		StringBuilder b = new StringBuilder();
159 		if (getPrefix() != null) {
160 			b.append(ParameterUtil.escapeWithDefault(getPrefix().getValue()));
161 		}
162 
163 		b.append(ParameterUtil.escapeWithDefault(getValueAsString()));
164 		b.append('|');
165 		b.append(ParameterUtil.escapeWithDefault(mySystem));
166 		b.append('|');
167 		b.append(ParameterUtil.escapeWithDefault(myUnits));
168 
169 		return b.toString();
170 	}
171 
172 	public String getValueAsString() {
173 		if (myValue != null) {
174 			return myValue.toPlainString();
175 		}
176 		return null;
177 	}
178 
179 	@Override
180 	void doSetValueAsQueryToken(FhirContext theContext, String theParamName, String theQualifier, String theValue) {
181 		clear();
182 
183 		if (theValue == null) {
184 			return;
185 		}
186 		List<String> parts = ParameterUtil.splitParameterString(theValue, '|', true);
187 
188 		if (parts.size() > 0 && StringUtils.isNotBlank(parts.get(0))) {
189 			String value = super.extractPrefixAndReturnRest(parts.get(0));
190 			setValue(value);
191 		}
192 		if (parts.size() > 1 && StringUtils.isNotBlank(parts.get(1))) {
193 			setSystem(parts.get(1));
194 		}
195 		if (parts.size() > 2 && StringUtils.isNotBlank(parts.get(2))) {
196 			setUnits(parts.get(2));
197 		}
198 
199 	}
200 
201 	/**
202 	 * Returns the system, or null if none was provided
203 	 * <p>
204 	 * Note that prior to HAPI FHIR 1.5, this method returned a {@link UriDt}
205 	 * </p>
206 	 * 
207 	 * @since 1.5
208 	 */
209 	public String getSystem() {
210 		return mySystem;
211 	}
212 
213 	/**
214 	 * @deprecated Use {{@link #getSystem()}} instead
215 	 */
216 	@Deprecated
217 	@CoverageIgnore
218 	public UriDt getSystemAsUriDt() {
219 		return new UriDt(mySystem);
220 	}
221 
222 	public String getUnits() {
223 		return myUnits;
224 	}
225 
226 	/**
227 	 * Returns the quantity/value, or null if none was provided
228 	 * <p>
229 	 * Note that prior to HAPI FHIR 1.5, this method returned a {@link DecimalDt}
230 	 * </p>
231 	 * 
232 	 * @since 1.5
233 	 */
234 	public BigDecimal getValue() {
235 		return myValue;
236 	}
237 
238 	public QuantityParam setSystem(String theSystem) {
239 		mySystem = theSystem;
240 		return this;
241 	}
242 
243 	public QuantityParam setSystem(IPrimitiveType<String> theSystem) {
244 		mySystem = null;
245 		if (theSystem != null) {
246 			mySystem = theSystem.getValue();
247 		}
248 		return this;
249 	}
250 
251 	public QuantityParam setUnits(String theUnits) {
252 		myUnits = theUnits;
253 		return this;
254 	}
255 
256 	public QuantityParam setValue(BigDecimal theValue) {
257 		myValue = theValue;
258 		return this;
259 	}
260 
261 	public QuantityParam setValue(IPrimitiveType<BigDecimal> theValue) {
262 		myValue = null;
263 		if (theValue != null) {
264 			myValue = theValue.getValue();
265 		}
266 		return this;
267 	}
268 
269 	public QuantityParam setValue(String theValue) {
270 		myValue = null;
271 		if (theValue != null) {
272 			myValue = new BigDecimal(theValue);
273 		}
274 		return this;
275 	}
276 
277 	public QuantityParam setValue(double theValue) {
278 		// Use the valueOf here because the constructor gives crazy precision
279 		// changes due to the floating point conversion
280 		myValue = BigDecimal.valueOf(theValue);
281 		return this;
282 	}
283 
284 	public QuantityParam setValue(long theValue) {
285 		// Use the valueOf here because the constructor gives crazy precision
286 		// changes due to the floating point conversion
287 		myValue = BigDecimal.valueOf(theValue);
288 		return this;
289 	}
290 
291 	@Override
292 	public String toString() {
293 		ToStringBuilder b = new ToStringBuilder(this, ToStringStyle.SHORT_PREFIX_STYLE);
294 		b.append("prefix", getPrefix());
295 		b.append("value", myValue);
296 		b.append("system", mySystem);
297 		b.append("units", myUnits);
298 		if (getMissing() != null) {
299 			b.append("missing", getMissing());
300 		}
301 		return b.toString();
302 	}
303 
304 }