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.apache.commons.lang3.StringUtils;
037import org.hl7.fhir.instance.model.api.IBaseDecimalDatatype;
038
039import ca.uhn.fhir.model.api.annotation.DatatypeDef;
040
041/**
042 * Primitive type "decimal" in FHIR: A rational number
043 */
044@DatatypeDef(name = "decimal")
045public class DecimalType extends PrimitiveType<BigDecimal> implements Comparable<DecimalType>, IBaseDecimalDatatype {
046
047  private static final long serialVersionUID = 3L;
048
049  /**
050   * Constructor
051   */
052  public DecimalType() {
053    super();
054  }
055
056  /**
057   * Constructor
058   */
059  public DecimalType(BigDecimal theValue) {
060    setValue(theValue);
061  }
062
063  /**
064   * Constructor
065   */
066  public DecimalType(double theValue) {
067    // Use the valueOf here because the constructor gives wacky precision
068    // changes due to the floating point conversion
069    setValue(BigDecimal.valueOf(theValue));
070  }
071
072  /**
073   * Constructor
074   */
075  public DecimalType(long theValue) {
076    setValue(theValue);
077  }
078
079  /**
080   * Constructor
081   */
082  public DecimalType(String theValue) {
083    setValueAsString(theValue);
084  }
085
086  @Override
087  public int compareTo(DecimalType theObj) {
088    if (getValue() == null && theObj.getValue() == null) {
089      return 0;
090    }
091    if (getValue() != null && theObj.getValue() == null) {
092      return 1;
093    }
094    if (getValue() == null && theObj.getValue() != null) {
095      return -1;
096    }
097    return getValue().compareTo(theObj.getValue());
098  }
099
100  @Override
101  protected String encode(BigDecimal theValue) {
102    return getValue().toString();
103  }
104
105  /**
106   * Gets the value as an integer, using {@link BigDecimal#intValue()}
107   */
108  public int getValueAsInteger() {
109    return getValue().intValue();
110  }
111
112  public Number getValueAsNumber() {
113    return getValue();
114  }
115
116  @Override
117  protected BigDecimal parse(String theValue) {
118    return new BigDecimal(theValue);
119  }
120
121  /**
122   * Rounds the value to the given prevision
123   * 
124   * @see MathContext#getPrecision()
125   */
126  public void round(int thePrecision) {
127    if (getValue() != null) {
128      BigDecimal newValue = getValue().round(new MathContext(thePrecision));
129      setValue(newValue);
130    }
131  }
132
133  /**
134   * Rounds the value to the given prevision
135   * 
136   * @see MathContext#getPrecision()
137   * @see MathContext#getRoundingMode()
138   */
139  public void round(int thePrecision, RoundingMode theRoundingMode) {
140    if (getValue() != null) {
141      BigDecimal newValue = getValue().round(new MathContext(thePrecision, theRoundingMode));
142      setValue(newValue);
143    }
144  }
145
146  /**
147   * Sets a new value using an integer
148   */
149  public void setValueAsInteger(int theValue) {
150    setValue(BigDecimal.valueOf(theValue));
151  }
152
153  public void setValueAsString(String theString) {
154    if (StringUtils.isBlank(theString)) {
155      setValue((BigDecimal) null);
156      setRepresentation(null);
157    } else {
158      setValue(new BigDecimal(theString));
159      setRepresentation(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    DecimalType ret = new DecimalType(getValue());
180    copyValues(ret);
181    return ret;
182  }
183
184  public String fhirType() {
185    return "decimal";
186  }
187
188  /**
189   * A parser can provide a literal representation for the decimal value that
190   * preserves the presented form.
191   * 
192   * All sorts of bad things can happen if this method is used to set the string
193   * representation to anything other than what was parsed into the actual value.
194   * Don't do that
195   * 
196   * @param value
197   * @return
198   */
199  public DecimalType setRepresentation(String value) {
200    forceStringValue(value);
201    return this;
202  }
203}