001package ca.uhn.fhir.rest.param;
002
003/*
004 * #%L
005 * HAPI FHIR - Core Library
006 * %%
007 * Copyright (C) 2014 - 2021 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.isBlank;
023import static org.apache.commons.lang3.StringUtils.isNotBlank;
024
025import java.math.BigDecimal;
026
027import org.apache.commons.lang3.builder.ToStringBuilder;
028import org.apache.commons.lang3.builder.ToStringStyle;
029
030import ca.uhn.fhir.context.FhirContext;
031import ca.uhn.fhir.model.api.IQueryParameterType;
032
033public class NumberParam extends BaseParamWithPrefix<NumberParam> implements IQueryParameterType {
034
035        private static final long serialVersionUID = 1L;
036        private BigDecimal myQuantity;
037
038        /**
039         * Constructor
040         */
041        public NumberParam() {
042                super();
043        }
044
045        /**
046         * Constructor
047         * 
048         * @param theValue
049         *            A value, e.g. "10"
050         */
051        public NumberParam(int theValue) {
052                setValue(new BigDecimal(theValue));
053        }
054
055        /**
056         * Constructor
057         * 
058         * @param theValue
059         *            A string value, e.g. "gt5.0"
060         */
061        public NumberParam(String theValue) {
062                setValueAsQueryToken(null, null, null, theValue);
063        }
064
065        @Override
066        String doGetQueryParameterQualifier() {
067                return null;
068        }
069
070        @Override
071        String doGetValueAsQueryToken(FhirContext theContext) {
072                StringBuilder b = new StringBuilder();
073                if (getPrefix() != null) {
074                        b.append(ParameterUtil.escapeWithDefault(getPrefix().getValue()));
075                }
076                b.append(ParameterUtil.escapeWithDefault(myQuantity.toPlainString()));
077                return b.toString();
078        }
079        
080        @Override
081        void doSetValueAsQueryToken(FhirContext theContext, String theParamName, String theQualifier, String theValue) {
082                if (getMissing() != null && isBlank(theValue)) {
083                        return;
084                }
085                String value = super.extractPrefixAndReturnRest(theValue);
086                myQuantity = null;
087                if (isNotBlank(value)) {
088                        myQuantity = new BigDecimal(value);
089                }
090        }
091        
092        
093        public BigDecimal getValue() {
094                return myQuantity;
095        }
096
097        public NumberParam setValue(BigDecimal theValue) {
098                myQuantity = theValue;
099                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}