001package ca.uhn.fhir.rest.param;
002
003/*
004 * #%L
005 * HAPI FHIR - Core Library
006 * %%
007 * Copyright (C) 2014 - 2017 University Health Network
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 BigDecimal myQuantity;
036
037        /**
038         * Constructor
039         */
040        public NumberParam() {
041                super();
042        }
043
044        /**
045         * Constructor
046         * 
047         * @param theValue
048         *            A string value, e.g. "&gt;5.0"
049         */
050        public NumberParam(String theValue) {
051                setValueAsQueryToken(null, null, null, theValue);
052        }
053
054        @Override
055        String doGetQueryParameterQualifier() {
056                return null;
057        }
058
059        @Override
060        String doGetValueAsQueryToken(FhirContext theContext) {
061                StringBuilder b = new StringBuilder();
062                if (getPrefix() != null) {
063                        b.append(ParameterUtil.escapeWithDefault(getPrefix().getValueForContext(theContext)));
064                }
065                b.append(ParameterUtil.escapeWithDefault(myQuantity.toPlainString()));
066                return b.toString();
067        }
068        
069        @Override
070        void doSetValueAsQueryToken(FhirContext theContext, String theParamName, String theQualifier, String theValue) {
071                if (getMissing() != null && isBlank(theValue)) {
072                        return;
073                }
074                String value = super.extractPrefixAndReturnRest(theValue);
075                myQuantity = null;
076                if (isNotBlank(value)) {
077                        myQuantity = new BigDecimal(value);
078                }
079        }
080        
081        
082        @Override
083        public String toString() {
084                ToStringBuilder b = new ToStringBuilder(this, ToStringStyle.SIMPLE_STYLE);
085                b.append("prefix", getPrefix());
086                b.append("value", myQuantity);
087                return b.build();
088        }
089
090        public BigDecimal getValue() {
091                return myQuantity;
092        }
093        
094        public NumberParam setValue(BigDecimal theValue) {
095                myQuantity = theValue;
096                return this;
097        }
098
099}