001package ca.uhn.fhir.model.base.composite; 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 */ 022 023import java.math.BigDecimal; 024 025import org.apache.commons.lang3.StringUtils; 026 027import ca.uhn.fhir.model.api.BaseIdentifiableElement; 028import ca.uhn.fhir.model.api.ICompositeDatatype; 029import ca.uhn.fhir.model.api.IQueryParameterType; 030import ca.uhn.fhir.model.dstu.valueset.QuantityCompararatorEnum; 031import ca.uhn.fhir.model.primitive.BoundCodeDt; 032import ca.uhn.fhir.model.primitive.CodeDt; 033import ca.uhn.fhir.model.primitive.DecimalDt; 034import ca.uhn.fhir.model.primitive.StringDt; 035import ca.uhn.fhir.model.primitive.UriDt; 036import ca.uhn.fhir.rest.param.QuantityParam; 037 038public abstract class BaseQuantityDt extends BaseIdentifiableElement implements ICompositeDatatype, IQueryParameterType { 039 040 041 /** 042 * Sets the value(s) for <b>value</b> (Numerical value (with implicit precision)) 043 * 044 * <p> 045 * <b>Definition:</b> 046 * The value of the measured amount. The value includes an implicit precision in the presentation of the value 047 * </p> 048 */ 049 public abstract BaseQuantityDt setValue(BigDecimal theValue); 050 051 052 @Override 053 public void setValueAsQueryToken(String theQualifier, String theValue) { 054 getComparatorElement().setValue(null); 055 setCode( null); 056 setSystem(null); 057 setUnits( null); 058 setValue( null); 059 060 if (theValue == null) { 061 return; 062 } 063 String[] parts = theValue.split("\\|"); 064 if (parts.length > 0 && StringUtils.isNotBlank(parts[0])) { 065 if (parts[0].startsWith("<=")) { 066 getComparatorElement().setValue(QuantityCompararatorEnum.LESSTHAN_OR_EQUALS.getCode()); 067 setValue(new BigDecimal(parts[0].substring(2))); 068 } else if (parts[0].startsWith("<")) { 069 getComparatorElement().setValue(QuantityCompararatorEnum.LESSTHAN.getCode()); 070 setValue(new BigDecimal(parts[0].substring(1))); 071 } else if (parts[0].startsWith(">=")) { 072 getComparatorElement().setValue(QuantityCompararatorEnum.GREATERTHAN_OR_EQUALS.getCode()); 073 setValue(new BigDecimal(parts[0].substring(2))); 074 } else if (parts[0].startsWith(">")) { 075 getComparatorElement().setValue(QuantityCompararatorEnum.GREATERTHAN.getCode()); 076 setValue(new BigDecimal(parts[0].substring(1))); 077 } else { 078 setValue(new BigDecimal(parts[0])); 079 } 080 } 081 if (parts.length > 1 && StringUtils.isNotBlank(parts[1])) { 082 setSystem(parts[1]); 083 } 084 if (parts.length > 2 && StringUtils.isNotBlank(parts[2])) { 085 setUnits(parts[2]); 086 } 087 088 } 089 090 /** 091 * Gets the value(s) for <b>comparator</b> (< | <= | >= | > - how to understand the value). 092 * creating it if it does 093 * not exist. Will not return <code>null</code>. 094 * 095 * <p> 096 * <b>Definition:</b> 097 * How the value should be understood and represented - whether the actual value is greater or less than 098 * the stated value due to measurement issues. E.g. if the comparator is \"<\" , then the real value is < stated value 099 * </p> 100 */ 101 public abstract BoundCodeDt<?> getComparatorElement(); 102 103 @Override 104 public String getValueAsQueryToken() { 105 StringBuilder b= new StringBuilder(); 106 if (getComparatorElement() != null) { 107 b.append(getComparatorElement().getValue()); 108 } 109 if (!getValueElement().isEmpty()) { 110 b.append(getValueElement().getValueAsString()); 111 } 112 b.append('|'); 113 if (!getSystemElement().isEmpty()) { 114 b.append(getSystemElement().getValueAsString()); 115 } 116 b.append('|'); 117 if (!getUnitsElement().isEmpty()) { 118 b.append(getUnitsElement().getValueAsString()); 119 } 120 121 return b.toString(); 122 } 123 124 125 @Override 126 public String getQueryParameterQualifier() { 127 return null; 128 } 129 130 131 132 133 134 /** 135 * Sets the value for <b>units</b> (Unit representation) 136 * 137 * <p> 138 * <b>Definition:</b> 139 * A human-readable form of the units 140 * </p> 141 */ 142 public abstract BaseQuantityDt setUnits( String theString); 143 144 145 /** 146 * Gets the value(s) for <b>system</b> (System that defines coded unit form). 147 * creating it if it does 148 * not exist. Will not return <code>null</code>. 149 * 150 * <p> 151 * <b>Definition:</b> 152 * The identification of the system that provides the coded form of the unit 153 * </p> 154 */ 155 public abstract UriDt getSystemElement(); 156 157 158 159 /** 160 * Sets the value for <b>system</b> (System that defines coded unit form) 161 * 162 * <p> 163 * <b>Definition:</b> 164 * The identification of the system that provides the coded form of the unit 165 * </p> 166 */ 167 public abstract BaseQuantityDt setSystem( String theUri); 168 169 /** 170 * Gets the value(s) for <b>code</b> (Coded form of the unit). 171 * creating it if it does 172 * not exist. Will not return <code>null</code>. 173 * 174 * <p> 175 * <b>Definition:</b> 176 * A computer processable form of the units in some unit representation system 177 * </p> 178 */ 179 public abstract CodeDt getCodeElement(); 180 181 /** 182 * Sets the value for <b>code</b> (Coded form of the unit) 183 * 184 * <p> 185 * <b>Definition:</b> 186 * A computer processable form of the units in some unit representation system 187 * </p> 188 */ 189 public abstract BaseQuantityDt setCode( String theCode); 190 /** 191 * Gets the value(s) for <b>units</b> (Unit representation). 192 * creating it if it does 193 * not exist. Will not return <code>null</code>. 194 * 195 * <p> 196 * <b>Definition:</b> 197 * A human-readable form of the units 198 * </p> 199 */ 200 public abstract StringDt getUnitsElement() ; 201 /** 202 * Gets the value(s) for <b>value</b> (Numerical value (with implicit precision)). 203 * creating it if it does 204 * not exist. Will not return <code>null</code>. 205 * 206 * <p> 207 * <b>Definition:</b> 208 * The value of the measured amount. The value includes an implicit precision in the presentation of the value 209 * </p> 210 */ 211 public abstract DecimalDt getValueElement(); 212 213 /** 214 * <b>Not supported!</b> 215 * 216 * @deprecated get/setMissing is not supported in StringDt. Use {@link QuantityParam} instead if you 217 * need this functionality 218 */ 219 @Deprecated 220 @Override 221 public Boolean getMissing() { 222 return null; 223 } 224 225 /** 226 * <b>Not supported!</b> 227 * 228 * @deprecated get/setMissing is not supported in StringDt. Use {@link QuantityParam} instead if you 229 * need this functionality 230 */ 231 @Deprecated 232 @Override 233 public void setMissing(Boolean theMissing) { 234 throw new UnsupportedOperationException("get/setMissing is not supported in StringDt. Use {@link StringParam} instead if you need this functionality"); 235 } 236 237 238}