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