001package ca.uhn.fhir.rest.gclient;
002
003/*
004 * #%L
005 * HAPI FHIR - Core Library
006 * %%
007 * Copyright (C) 2014 - 2023 Smile CDR, Inc.
008 * %%
009 * Licensed under the Apache License, Version 2.0 (the "License");
010 * you may not use this file except in compliance with the License.
011 * You may obtain a copy of the License at
012 *
013 *      http://www.apache.org/licenses/LICENSE-2.0
014 *
015 * Unless required by applicable law or agreed to in writing, software
016 * distributed under the License is distributed on an "AS IS" BASIS,
017 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
018 * See the License for the specific language governing permissions and
019 * limitations under the License.
020 * #L%
021 */
022import static org.apache.commons.lang3.StringUtils.defaultString;
023
024import ca.uhn.fhir.rest.gclient.NumberClientParam.IMatches;
025import ca.uhn.fhir.rest.param.ParamPrefixEnum;
026
027/**
028 * Quantity parameter type for use in fluent client interfaces
029 */
030@SuppressWarnings("deprecation")
031public class QuantityClientParam extends BaseClientParam  implements IParam {
032
033        private String myParamName;
034
035        public QuantityClientParam(String theParamName) {
036                myParamName = theParamName;
037        }
038
039        public IMatches<IAndUnits> approximately() {
040                return new NumberClientParam.IMatches<IAndUnits>() {
041                        @Override
042                        public IAndUnits number(long theNumber) {
043                                return new AndUnits(ParamPrefixEnum.APPROXIMATE, Long.toString(theNumber));
044                        }
045
046                        @Override
047                        public IAndUnits number(String theNumber) {
048                                return new AndUnits(ParamPrefixEnum.APPROXIMATE, theNumber);
049                        }
050                };
051        }
052
053        public IMatches<IAndUnits> exactly() {
054                return new NumberClientParam.IMatches<IAndUnits>() {
055                        @Override
056                        public IAndUnits number(long theNumber) {
057                                return new AndUnits(null, Long.toString(theNumber));
058                        }
059
060                        @Override
061                        public IAndUnits number(String theNumber) {
062                                return new AndUnits(null, theNumber);
063                        }
064                };
065        }
066
067        @Override
068        public String getParamName() {
069                return myParamName;
070        }
071
072        public IMatches<IAndUnits> greaterThan() {
073                return new NumberClientParam.IMatches<IAndUnits>() {
074                        @Override
075                        public IAndUnits number(long theNumber) {
076                                return new AndUnits(ParamPrefixEnum.GREATERTHAN, Long.toString(theNumber));
077                        }
078
079                        @Override
080                        public IAndUnits number(String theNumber) {
081                                return new AndUnits(ParamPrefixEnum.GREATERTHAN, theNumber);
082                        }
083                };
084        }
085
086        public IMatches<IAndUnits> greaterThanOrEquals() {
087                return new NumberClientParam.IMatches<IAndUnits>() {
088                        @Override
089                        public IAndUnits number(long theNumber) {
090                                return new AndUnits(ParamPrefixEnum.GREATERTHAN_OR_EQUALS, Long.toString(theNumber));
091                        }
092
093                        @Override
094                        public IAndUnits number(String theNumber) {
095                                return new AndUnits(ParamPrefixEnum.GREATERTHAN_OR_EQUALS, theNumber);
096                        }
097                };
098        }
099
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}