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.r4.model;
033
034import java.math.BigDecimal;
035import java.math.MathContext;
036import java.math.RoundingMode;
037
038import org.hl7.fhir.instance.model.api.IBaseDecimalDatatype;
039
040import ca.uhn.fhir.model.api.annotation.DatatypeDef;
041
042/**
043 * Primitive type "decimal" in FHIR: A rational number
044 */
045@DatatypeDef(name = "decimal")
046public class DecimalType extends PrimitiveType<BigDecimal> implements Comparable<DecimalType>, IBaseDecimalDatatype {
047
048        private static final long serialVersionUID = 3L;
049
050        /**
051         * Constructor
052         */
053        public DecimalType() {
054                super();
055        }
056
057        /**
058         * Constructor
059         */
060        public DecimalType(BigDecimal theValue) {
061                setValue(theValue);
062        }
063
064        /**
065         * Constructor
066         */
067        public DecimalType(double theValue) {
068                // Use the valueOf here because the constructor gives wacky precision
069                // changes due to the floating point conversion
070                setValue(BigDecimal.valueOf(theValue));
071        }
072
073        /**
074         * Constructor
075         */
076        public DecimalType(long theValue) {
077                setValue(theValue);
078        }
079
080        /**
081         * Constructor
082         */
083        public DecimalType(String theValue) {
084                setValue(new BigDecimal(theValue));
085                setRepresentation(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().toString();
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        /**
156         * Sets a new value using a long
157         */
158        public void setValue(long theValue) {
159                setValue(BigDecimal.valueOf(theValue));
160        }
161
162        /**
163         * Sets a new value using a double
164         */
165        public void setValue(double theValue) {
166                setValue(BigDecimal.valueOf(theValue));
167        }
168
169        @Override
170        public DecimalType copy() {
171                return new DecimalType(getValue());
172        }
173
174        public String fhirType() {
175                return "decimal";               
176        }
177
178        /**
179         * A parser can provide a literal representation for the decimal value that preserves
180         * the presented form.
181         * 
182         * All sorts of bad things can happen if this method is used to set the string representation
183         * to anything other than what was parsed into the actual value. Don't do that
184         * 
185         * @param value
186         * @return
187         */
188        public DecimalType setRepresentation(String value) {
189          forceStringValue(value);
190          return this;
191        }
192}