
001package org.hl7.fhir.dstu3.model; 002 003/* 004 Copyright (c) 2011+, HL7, Inc. 005 All rights reserved. 006 007 Redistribution and use in source and binary forms, with or without modification, 008 are permitted provided that the following conditions are met: 009 010 * Redistributions of source code must retain the above copyright notice, this 011 list of conditions and the following disclaimer. 012 * Redistributions in binary form must reproduce the above copyright notice, 013 this list of conditions and the following disclaimer in the documentation 014 and/or other materials provided with the distribution. 015 * Neither the name of HL7 nor the names of its contributors may be used to 016 endorse or promote products derived from this software without specific 017 prior written permission. 018 019 THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND 020 ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED 021 WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. 022 IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, 023 INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT 024 NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR 025 PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, 026 WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) 027 ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE 028 POSSIBILITY OF SUCH DAMAGE. 029 030 */ 031 032 033 034import java.math.BigDecimal; 035import java.math.MathContext; 036import java.math.RoundingMode; 037 038import org.apache.commons.lang3.StringUtils; 039import org.hl7.fhir.instance.model.api.IBaseDecimalDatatype; 040 041import ca.uhn.fhir.model.api.annotation.DatatypeDef; 042 043/** 044 * Primitive type "decimal" in FHIR: A rational number 045 */ 046@DatatypeDef(name = "decimal") 047public class DecimalType extends PrimitiveType<BigDecimal> implements Comparable<DecimalType>, IBaseDecimalDatatype { 048 049 private static final long serialVersionUID = 3L; 050 051 /** 052 * Constructor 053 */ 054 public DecimalType() { 055 super(); 056 } 057 058 /** 059 * Constructor 060 */ 061 public DecimalType(BigDecimal theValue) { 062 setValue(theValue); 063 } 064 065 /** 066 * Constructor 067 */ 068 public DecimalType(double theValue) { 069 // Use the valueOf here because the constructor gives wacky precision 070 // changes due to the floating point conversion 071 setValue(BigDecimal.valueOf(theValue)); 072 } 073 074 /** 075 * Constructor 076 */ 077 public DecimalType(long theValue) { 078 setValue(theValue); 079 } 080 081 /** 082 * Constructor 083 */ 084 public DecimalType(String theValue) { 085 setValueAsString(theValue); 086 } 087 088 @Override 089 public int compareTo(DecimalType theObj) { 090 if (getValue() == null && theObj.getValue() == null) { 091 return 0; 092 } 093 if (getValue() != null && theObj.getValue() == null) { 094 return 1; 095 } 096 if (getValue() == null && theObj.getValue() != null) { 097 return -1; 098 } 099 return getValue().compareTo(theObj.getValue()); 100 } 101 102 @Override 103 protected String encode(BigDecimal theValue) { 104 return getValue().toPlainString(); 105 } 106 107 /** 108 * Gets the value as an integer, using {@link BigDecimal#intValue()} 109 */ 110 public int getValueAsInteger() { 111 return getValue().intValue(); 112 } 113 114 public Number getValueAsNumber() { 115 return getValue(); 116 } 117 118 @Override 119 protected BigDecimal parse(String theValue) { 120 return new BigDecimal(theValue); 121 } 122 123 /** 124 * Rounds the value to the given prevision 125 * 126 * @see MathContext#getPrecision() 127 */ 128 public void round(int thePrecision) { 129 if (getValue() != null) { 130 BigDecimal newValue = getValue().round(new MathContext(thePrecision)); 131 setValue(newValue); 132 } 133 } 134 135 /** 136 * Rounds the value to the given prevision 137 * 138 * @see MathContext#getPrecision() 139 * @see MathContext#getRoundingMode() 140 */ 141 public void round(int thePrecision, RoundingMode theRoundingMode) { 142 if (getValue() != null) { 143 BigDecimal newValue = getValue().round(new MathContext(thePrecision, theRoundingMode)); 144 setValue(newValue); 145 } 146 } 147 148 /** 149 * Sets a new value using an integer 150 */ 151 public void setValueAsInteger(int theValue) { 152 setValue(BigDecimal.valueOf(theValue)); 153 } 154 155 public void setValueAsString(String theString) { 156 if (StringUtils.isBlank(theString)) { 157 setValue((BigDecimal) null); 158 } else { 159 setValue(new BigDecimal(theString)); 160 } 161 } 162 163 /** 164 * Sets a new value using a long 165 */ 166 public void setValue(long theValue) { 167 setValue(BigDecimal.valueOf(theValue)); 168 } 169 170 /** 171 * Sets a new value using a double 172 */ 173 public void setValue(double theValue) { 174 setValue(BigDecimal.valueOf(theValue)); 175 } 176 177 @Override 178 public DecimalType copy() { 179 return new DecimalType(getValue()); 180 } 181 182 public String fhirType() { 183 return "decimal"; 184 } 185 186}