001package org.hl7.fhir.dstu2.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 ca.uhn.fhir.model.api.annotation.DatatypeDef;
037import org.hl7.fhir.instance.model.api.IBaseDecimalDatatype;
038
039/**
040 * Primitive type "decimal" in FHIR: A rational number
041 */
042@DatatypeDef(name = "decimal")
043public class DecimalType extends PrimitiveType<BigDecimal> implements Comparable<DecimalType>, IBaseDecimalDatatype {
044
045  private static final long serialVersionUID = 3L;
046
047  /**
048   * Constructor
049   */
050  public DecimalType() {
051    super();
052  }
053
054  /**
055   * Constructor
056   */
057  public DecimalType(BigDecimal theValue) {
058    setValue(theValue);
059  }
060
061  /**
062   * Constructor
063   */
064  public DecimalType(double theValue) {
065    // Use the valueOf here because the constructor gives wacky precision
066    // changes due to the floating point conversion
067    setValue(BigDecimal.valueOf(theValue));
068  }
069
070  /**
071   * Constructor
072   */
073  public DecimalType(long theValue) {
074    setValue(new BigDecimal(theValue));
075  }
076
077  /**
078   * Constructor
079   */
080  public DecimalType(String theValue) {
081    setValue(new BigDecimal(theValue));
082  }
083
084  @Override
085  public int compareTo(DecimalType theObj) {
086    if (getValue() == null && theObj.getValue() == null) {
087      return 0;
088    }
089    if (getValue() != null && theObj.getValue() == null) {
090      return 1;
091    }
092    if (getValue() == null && theObj.getValue() != null) {
093      return -1;
094    }
095    return getValue().compareTo(theObj.getValue());
096  }
097
098  @Override
099  protected String encode(BigDecimal theValue) {
100    return getValue().toPlainString();
101  }
102
103  /**
104   * Gets the value as an integer, using {@link BigDecimal#intValue()}
105   */
106  public int getValueAsInteger() {
107    return getValue().intValue();
108  }
109
110  public Number getValueAsNumber() {
111    return getValue();
112  }
113
114  @Override
115  protected BigDecimal parse(String theValue) {
116    return new BigDecimal(theValue);
117  }
118
119  /**
120   * Rounds the value to the given prevision
121   * 
122   * @see MathContext#getPrecision()
123   */
124  public void round(int thePrecision) {
125    if (getValue() != null) {
126      BigDecimal newValue = getValue().round(new MathContext(thePrecision));
127      setValue(newValue);
128    }
129  }
130
131  /**
132   * Rounds the value to the given prevision
133   * 
134   * @see MathContext#getPrecision()
135   * @see MathContext#getRoundingMode()
136   */
137  public void round(int thePrecision, RoundingMode theRoundingMode) {
138    if (getValue() != null) {
139      BigDecimal newValue = getValue().round(new MathContext(thePrecision, theRoundingMode));
140      setValue(newValue);
141    }
142  }
143
144  /**
145   * Sets a new value using an integer
146   */
147  public void setValueAsInteger(int theValue) {
148    setValue(new BigDecimal(theValue));
149  }
150
151  @Override
152  public DecimalType copy() {
153    return new DecimalType(getValue());
154  }
155
156  public String fhirType() {
157    return "decimal";
158  }
159}