001package org.hl7.fhir.r4.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
032import java.math.BigDecimal;
033import java.math.MathContext;
034import java.math.RoundingMode;
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(theValue);
076  }
077
078  /**
079   * Constructor
080   */
081  public DecimalType(String theValue) {
082    setValueAsString(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().toString();
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(BigDecimal.valueOf(theValue));
150  }
151
152  public void setValueAsString(String theString) {
153    setValue(new BigDecimal(theString));
154    setRepresentation(theString);
155  }
156
157  /**
158   * Sets a new value using a long
159   */
160  public void setValue(long theValue) {
161    setValue(BigDecimal.valueOf(theValue));
162  }
163
164  /**
165   * Sets a new value using a double
166   */
167  public void setValue(double theValue) {
168    setValue(BigDecimal.valueOf(theValue));
169  }
170
171  @Override
172  public DecimalType copy() {
173    DecimalType ret = new DecimalType(getValue());
174    copyValues(ret);
175    return ret;
176  }
177
178  public String fhirType() {
179    return "decimal";
180  }
181
182  /**
183   * A parser can provide a literal representation for the decimal value that
184   * preserves the presented form.
185   * 
186   * All sorts of bad things can happen if this method is used to set the string
187   * representation to anything other than what was parsed into the actual value.
188   * Don't do that
189   * 
190   * @param value
191   * @return
192   */
193  public DecimalType setRepresentation(String value) {
194    forceStringValue(value);
195    return this;
196  }
197}