View Javadoc
1   package ca.uhn.fhir.rest.gclient;
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.defaultString;
23  
24  import ca.uhn.fhir.rest.gclient.NumberClientParam.IMatches;
25  import ca.uhn.fhir.rest.param.ParamPrefixEnum;
26  
27  /**
28   * Token parameter type for use in fluent client interfaces
29   */
30  @SuppressWarnings("deprecation")
31  public class QuantityClientParam extends BaseClientParam  implements IParam {
32  
33  	private String myParamName;
34  
35  	public QuantityClientParam(String theParamName) {
36  		myParamName = theParamName;
37  	}
38  
39  	public IMatches<IAndUnits> approximately() {
40  		return new NumberClientParam.IMatches<IAndUnits>() {
41  			@Override
42  			public IAndUnits number(long theNumber) {
43  				return new AndUnits(ParamPrefixEnum.APPROXIMATE, Long.toString(theNumber));
44  			}
45  
46  			@Override
47  			public IAndUnits number(String theNumber) {
48  				return new AndUnits(ParamPrefixEnum.APPROXIMATE, theNumber);
49  			}
50  		};
51  	}
52  
53  	public IMatches<IAndUnits> exactly() {
54  		return new NumberClientParam.IMatches<IAndUnits>() {
55  			@Override
56  			public IAndUnits number(long theNumber) {
57  				return new AndUnits(null, Long.toString(theNumber));
58  			}
59  
60  			@Override
61  			public IAndUnits number(String theNumber) {
62  				return new AndUnits(null, theNumber);
63  			}
64  		};
65  	}
66  
67  	@Override
68  	public String getParamName() {
69  		return myParamName;
70  	}
71  
72  	public IMatches<IAndUnits> greaterThan() {
73  		return new NumberClientParam.IMatches<IAndUnits>() {
74  			@Override
75  			public IAndUnits number(long theNumber) {
76  				return new AndUnits(ParamPrefixEnum.GREATERTHAN, Long.toString(theNumber));
77  			}
78  
79  			@Override
80  			public IAndUnits number(String theNumber) {
81  				return new AndUnits(ParamPrefixEnum.GREATERTHAN, theNumber);
82  			}
83  		};
84  	}
85  
86  	public IMatches<IAndUnits> greaterThanOrEquals() {
87  		return new NumberClientParam.IMatches<IAndUnits>() {
88  			@Override
89  			public IAndUnits number(long theNumber) {
90  				return new AndUnits(ParamPrefixEnum.GREATERTHAN_OR_EQUALS, Long.toString(theNumber));
91  			}
92  
93  			@Override
94  			public IAndUnits number(String theNumber) {
95  				return new AndUnits(ParamPrefixEnum.GREATERTHAN_OR_EQUALS, theNumber);
96  			}
97  		};
98  	}
99  
100 	public IMatches<IAndUnits> lessThan() {
101 		return new NumberClientParam.IMatches<IAndUnits>() {
102 			@Override
103 			public IAndUnits number(long theNumber) {
104 				return new AndUnits(ParamPrefixEnum.LESSTHAN, Long.toString(theNumber));
105 			}
106 
107 			@Override
108 			public IAndUnits number(String theNumber) {
109 				return new AndUnits(ParamPrefixEnum.LESSTHAN, theNumber);
110 			}
111 		};
112 	}
113 
114 	public IMatches<IAndUnits> lessThanOrEquals() {
115 		return new NumberClientParam.IMatches<IAndUnits>() {
116 			@Override
117 			public IAndUnits number(long theNumber) {
118 				return new AndUnits(ParamPrefixEnum.LESSTHAN_OR_EQUALS, Long.toString(theNumber));
119 			}
120 
121 			@Override
122 			public IAndUnits number(String theNumber) {
123 				return new AndUnits(ParamPrefixEnum.LESSTHAN_OR_EQUALS, theNumber);
124 			}
125 		};
126 	}
127 
128 	/**
129 	 * Use the given quantity prefix
130 	 * 
131 	 * @param thePrefix The prefix, or <code>null</code> for no prefix
132 	 */
133 	public IMatches<IAndUnits> withPrefix(final ParamPrefixEnum thePrefix) {
134 		return new NumberClientParam.IMatches<IAndUnits>() {
135 			@Override
136 			public IAndUnits number(long theNumber) {
137 				return new AndUnits(thePrefix, Long.toString(theNumber));
138 			}
139 
140 			@Override
141 			public IAndUnits number(String theNumber) {
142 				return new AndUnits(thePrefix, theNumber);
143 			}
144 		};
145 	}
146 
147 	public interface IAndUnits {
148 
149 		ICriterion<QuantityClientParam> andNoUnits();
150 
151 		ICriterion<QuantityClientParam> andUnits(String theUnits);
152 
153 		ICriterion<QuantityClientParam> andUnits(String theSystem, String theUnits);
154 	}
155 
156 	private class AndUnits implements IAndUnits {
157 
158 		private ParamPrefixEnum myPrefix;
159 		private String myValue;
160 
161 		public AndUnits(ParamPrefixEnum thePrefix, String theNumber) {
162 			myPrefix = thePrefix;
163 			myValue = theNumber;
164 		}
165 
166 		@Override
167 		public ICriterion<QuantityClientParam> andNoUnits() {
168 			return andUnits(null, null);
169 		}
170 
171 		@Override
172 		public ICriterion<QuantityClientParam> andUnits(String theUnits) {
173 			return andUnits(null, theUnits);
174 		}
175 
176 		@Override
177 		public ICriterion<QuantityClientParam> andUnits(String theSystem, String theUnits) {
178 			return new QuantityCriterion(getParamName(), myPrefix, myValue , defaultString(theSystem) , defaultString(theUnits));
179 		}
180 
181 	}
182 
183 }