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  import static org.apache.commons.lang3.StringUtils.isBlank;
23  import static org.apache.commons.lang3.StringUtils.isNotBlank;
24  
25  import java.math.BigDecimal;
26  
27  import org.apache.commons.lang3.builder.ToStringBuilder;
28  import org.apache.commons.lang3.builder.ToStringStyle;
29  
30  import ca.uhn.fhir.context.FhirContext;
31  import ca.uhn.fhir.model.api.IQueryParameterType;
32  
33  public class NumberParam extends BaseParamWithPrefix<NumberParam> implements IQueryParameterType {
34  
35  	private static final long serialVersionUID = 1L;
36  	private BigDecimal myQuantity;
37  
38  	/**
39  	 * Constructor
40  	 */
41  	public NumberParam() {
42  		super();
43  	}
44  
45  	/**
46  	 * Constructor
47  	 * 
48  	 * @param theValue
49  	 *            A value, e.g. "10"
50  	 */
51  	public NumberParam(int theValue) {
52  		setValue(new BigDecimal(theValue));
53  	}
54  
55  	/**
56  	 * Constructor
57  	 * 
58  	 * @param theValue
59  	 *            A string value, e.g. "&gt;5.0"
60  	 */
61  	public NumberParam(String theValue) {
62  		setValueAsQueryToken(null, null, null, theValue);
63  	}
64  
65  	@Override
66  	String doGetQueryParameterQualifier() {
67  		return null;
68  	}
69  
70  	@Override
71  	String doGetValueAsQueryToken(FhirContext theContext) {
72  		StringBuilder b = new StringBuilder();
73  		if (getPrefix() != null) {
74  			b.append(ParameterUtil.escapeWithDefault(getPrefix().getValue()));
75  		}
76  		b.append(ParameterUtil.escapeWithDefault(myQuantity.toPlainString()));
77  		return b.toString();
78  	}
79  	
80  	@Override
81  	void doSetValueAsQueryToken(FhirContext theContext, String theParamName, String theQualifier, String theValue) {
82  		if (getMissing() != null && isBlank(theValue)) {
83  			return;
84  		}
85  		String value = super.extractPrefixAndReturnRest(theValue);
86  		myQuantity = null;
87  		if (isNotBlank(value)) {
88  			myQuantity = new BigDecimal(value);
89  		}
90  	}
91  	
92  	
93  	public BigDecimal getValue() {
94  		return myQuantity;
95  	}
96  
97  	public NumberParam setValue(BigDecimal theValue) {
98  		myQuantity = theValue;
99  		return this;
100 	}
101 	
102 	@Override
103 	public String toString() {
104 		ToStringBuilder b = new ToStringBuilder(this, ToStringStyle.SIMPLE_STYLE);
105 		b.append("prefix", getPrefix());
106 		b.append("value", myQuantity);
107 		return b.build();
108 	}
109 
110 }