001package ca.uhn.fhir.rest.gclient; 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.defaultString; 023 024import ca.uhn.fhir.model.dstu.valueset.QuantityCompararatorEnum; 025import ca.uhn.fhir.rest.gclient.NumberClientParam.IMatches; 026 027/** 028 * Token parameter type for use in fluent client interfaces 029 */ 030public class QuantityClientParam extends BaseClientParam implements IParam { 031 032 private String myParamName; 033 034 public QuantityClientParam(String theParamName) { 035 myParamName = theParamName; 036 } 037 038 public IMatches<IAndUnits> approximately() { 039 return new NumberClientParam.IMatches<IAndUnits>() { 040 @Override 041 public IAndUnits number(long theNumber) { 042 return new AndUnits("~", Long.toString(theNumber)); 043 } 044 045 @Override 046 public IAndUnits number(String theNumber) { 047 return new AndUnits("~", theNumber); 048 } 049 }; 050 } 051 052 public IMatches<IAndUnits> exactly() { 053 return new NumberClientParam.IMatches<IAndUnits>() { 054 @Override 055 public IAndUnits number(long theNumber) { 056 return new AndUnits("", Long.toString(theNumber)); 057 } 058 059 @Override 060 public IAndUnits number(String theNumber) { 061 return new AndUnits("", theNumber); 062 } 063 }; 064 } 065 066 @Override 067 public String getParamName() { 068 return myParamName; 069 } 070 071 public IMatches<IAndUnits> greaterThan() { 072 return new NumberClientParam.IMatches<IAndUnits>() { 073 @Override 074 public IAndUnits number(long theNumber) { 075 return new AndUnits(">", Long.toString(theNumber)); 076 } 077 078 @Override 079 public IAndUnits number(String theNumber) { 080 return new AndUnits(">", theNumber); 081 } 082 }; 083 } 084 085 public IMatches<IAndUnits> greaterThanOrEquals() { 086 return new NumberClientParam.IMatches<IAndUnits>() { 087 @Override 088 public IAndUnits number(long theNumber) { 089 return new AndUnits(">=", Long.toString(theNumber)); 090 } 091 092 @Override 093 public IAndUnits number(String theNumber) { 094 return new AndUnits(">=", theNumber); 095 } 096 }; 097 } 098 099 public IMatches<IAndUnits> lessThan() { 100 return new NumberClientParam.IMatches<IAndUnits>() { 101 @Override 102 public IAndUnits number(long theNumber) { 103 return new AndUnits("<", Long.toString(theNumber)); 104 } 105 106 @Override 107 public IAndUnits number(String theNumber) { 108 return new AndUnits("<", theNumber); 109 } 110 }; 111 } 112 113 public IMatches<IAndUnits> lessThanOrEquals() { 114 return new NumberClientParam.IMatches<IAndUnits>() { 115 @Override 116 public IAndUnits number(long theNumber) { 117 return new AndUnits("<=", Long.toString(theNumber)); 118 } 119 120 @Override 121 public IAndUnits number(String theNumber) { 122 return new AndUnits("<=", theNumber); 123 } 124 }; 125 } 126 127 public IMatches<IAndUnits> withComparator(QuantityCompararatorEnum theComparator) { 128 final String cmp = theComparator != null ? theComparator.getCode() : ""; 129 return new NumberClientParam.IMatches<IAndUnits>() { 130 @Override 131 public IAndUnits number(long theNumber) { 132 return new AndUnits(cmp, Long.toString(theNumber)); 133 } 134 135 @Override 136 public IAndUnits number(String theNumber) { 137 return new AndUnits(cmp, theNumber); 138 } 139 }; 140 } 141 142 public interface IAndUnits { 143 144 ICriterion<QuantityClientParam> andNoUnits(); 145 146 ICriterion<QuantityClientParam> andUnits(String theUnits); 147 148 ICriterion<QuantityClientParam> andUnits(String theSystem, String theUnits); 149 } 150 151 private class AndUnits implements IAndUnits { 152 153 private String myToken1; 154 155 public AndUnits(String theComparator, String theNumber) { 156 myToken1 = defaultString(theComparator) + defaultString(theNumber); 157 } 158 159 @Override 160 public ICriterion<QuantityClientParam> andNoUnits() { 161 return andUnits(null, null); 162 } 163 164 @Override 165 public ICriterion<QuantityClientParam> andUnits(String theUnits) { 166 return andUnits(theUnits, null); 167 } 168 169 @Override 170 public ICriterion<QuantityClientParam> andUnits(String theSystem, String theUnits) { 171 return new QuantityCriterion(getParamName(), myToken1 , defaultString(theSystem) , defaultString(theUnits)); 172 } 173 174 } 175 176}