001package ca.uhn.fhir.rest.param; 002 003/* 004 * #%L 005 * HAPI FHIR - Core Library 006 * %% 007 * Copyright (C) 2014 - 2016 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; 023 024import java.math.BigDecimal; 025 026import ca.uhn.fhir.model.api.IQueryParameterType; 027import ca.uhn.fhir.model.dstu.valueset.QuantityCompararatorEnum; 028 029public class NumberParam extends BaseParam implements IQueryParameterType { 030 031 private InternalQuantityDt myQuantity = new InternalQuantityDt(); 032 033 public NumberParam() { 034 } 035 036 /** 037 * Constructor 038 * 039 * @param theValue 040 * A string value, e.g. ">5.0" 041 */ 042 public NumberParam(String theValue) { 043 setValueAsQueryToken(null, theValue); 044 } 045 046 @Override 047 String doGetQueryParameterQualifier() { 048 return null; 049 } 050 051 @Override 052 String doGetValueAsQueryToken() { 053 StringBuilder b = new StringBuilder(); 054 b.append(ParameterUtil.escapeWithDefault(myQuantity.getComparatorElement().getValue())); 055 b.append(ParameterUtil.escapeWithDefault(myQuantity.getValueElement().toString())); 056 return b.toString(); 057 } 058 059 @Override 060 void doSetValueAsQueryToken(String theQualifier, String theValue) { 061 if (getMissing() != null && isBlank(theValue)) { 062 return; 063 } 064 if (theValue.startsWith("<=")) { 065 myQuantity.setComparator(QuantityCompararatorEnum.LESSTHAN_OR_EQUALS); 066 myQuantity.setValue(new BigDecimal(theValue.substring(2))); 067 } else if (theValue.startsWith("<")) { 068 myQuantity.setComparator(QuantityCompararatorEnum.LESSTHAN); 069 myQuantity.setValue(new BigDecimal(theValue.substring(1))); 070 } else if (theValue.startsWith(">=")) { 071 myQuantity.setComparator(QuantityCompararatorEnum.GREATERTHAN_OR_EQUALS); 072 myQuantity.setValue(new BigDecimal(theValue.substring(2))); 073 } else if (theValue.startsWith(">")) { 074 myQuantity.setComparator(QuantityCompararatorEnum.GREATERTHAN); 075 myQuantity.setValue(new BigDecimal(theValue.substring(1))); 076 } else { 077 myQuantity.setComparator((QuantityCompararatorEnum) null); 078 myQuantity.setValue(new BigDecimal(theValue)); 079 } 080 } 081 082 083 public QuantityCompararatorEnum getComparator() { 084 return myQuantity.getComparatorElement().getValueAsEnum(); 085 } 086 087 public BigDecimal getValue() { 088 return myQuantity.getValueElement().getValue(); 089 } 090 091 @Override 092 public String toString() { 093 StringBuilder b = new StringBuilder(); 094 b.append(getClass().getSimpleName()); 095 b.append("["); 096 if (myQuantity.getComparatorElement().isEmpty() == false) { 097 b.append(myQuantity.getComparatorElement().getValue()); 098 } 099 if (myQuantity.getValueElement().isEmpty() == false) { 100 b.append(myQuantity.getValueElement().toString()); 101 } 102 b.append("]"); 103 return b.toString(); 104 } 105 106}