001package ca.uhn.fhir.model.primitive;
002
003/*
004 * #%L
005 * HAPI FHIR - Core Library
006 * %%
007 * Copyright (C) 2014 - 2021 Smile CDR, Inc.
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;
024import java.math.MathContext;
025import java.math.RoundingMode;
026
027import org.hl7.fhir.instance.model.api.IBaseDecimalDatatype;
028
029import ca.uhn.fhir.model.api.BasePrimitive;
030import ca.uhn.fhir.model.api.annotation.DatatypeDef;
031import ca.uhn.fhir.model.api.annotation.SimpleSetter;
032
033@DatatypeDef(name = "decimal")
034public class DecimalDt extends BasePrimitive<BigDecimal> implements Comparable<DecimalDt>, IBaseDecimalDatatype {
035
036        /**
037         * Constructor
038         */
039        public DecimalDt() {
040                super();
041        }
042
043        /**
044         * Constructor
045         */
046        @SimpleSetter
047        public DecimalDt(@SimpleSetter.Parameter(name = "theValue") BigDecimal theValue) {
048                setValue(theValue);
049        }
050
051        /**
052         * Constructor
053         */
054        @SimpleSetter
055        public DecimalDt(@SimpleSetter.Parameter(name = "theValue") double theValue) {
056                // Use the valueOf here because the constructor gives crazy precision
057                // changes due to the floating point conversion
058                setValue(BigDecimal.valueOf(theValue));
059        }
060
061        /**
062         * Constructor
063         */
064        @SimpleSetter
065        public DecimalDt(@SimpleSetter.Parameter(name = "theValue") long theValue) {
066                setValue(new BigDecimal(theValue));
067        }
068
069        /**
070         * Constructor
071         */
072        public DecimalDt(String theValue) {
073                setValue(new BigDecimal(theValue));
074        }
075
076        @Override
077        public int compareTo(DecimalDt theObj) {
078                if (getValue() == null && theObj.getValue() == null) {
079                        return 0;
080                }
081                if (getValue() != null && (theObj == null || theObj.getValue() == null)) {
082                        return 1;
083                }
084                if (getValue() == null && theObj.getValue() != null) {
085                        return -1;
086                }
087                return getValue().compareTo(theObj.getValue());
088        }
089
090        @Override
091        protected String encode(BigDecimal theValue) {
092                return getValue().toPlainString();
093        }
094
095        /**
096         * Gets the value as an integer, using {@link BigDecimal#intValue()}
097         */
098        public int getValueAsInteger() {
099                return getValue().intValue();
100        }
101
102        public Number getValueAsNumber() {
103                return getValue();
104        }
105
106        @Override
107        protected BigDecimal parse(String theValue) {
108                return new BigDecimal(theValue);
109        }
110
111        /**
112         * Rounds the value to the given prevision
113         * 
114         * @see MathContext#getPrecision()
115         */
116        public void round(int thePrecision) {
117                if (getValue() != null) {
118                        BigDecimal newValue = getValue().round(new MathContext(thePrecision));
119                        setValue(newValue);
120                }
121        }
122
123        /**
124         * Rounds the value to the given prevision
125         * 
126         * @see MathContext#getPrecision()
127         * @see MathContext#getRoundingMode()
128         */
129        public void round(int thePrecision, RoundingMode theRoundingMode) {
130                if (getValue() != null) {
131                        BigDecimal newValue = getValue().round(new MathContext(thePrecision, theRoundingMode));
132                        setValue(newValue);
133                }
134        }
135
136        /**
137         * Sets a new value using an integer
138         */
139        public void setValueAsInteger(int theValue) {
140                setValue(new BigDecimal(theValue));
141        }
142
143}