View Javadoc
1   package ca.uhn.fhir.model.primitive;
2   
3   /*
4    * #%L
5    * HAPI FHIR - Core Library
6    * %%
7    * Copyright (C) 2014 - 2018 University Health Network
8    * %%
9    * Licensed under the Apache License, Version 2.0 (the "License");
10   * you may not use this file except in compliance with the License.
11   * You may obtain a copy of the License at
12   * 
13   *      http://www.apache.org/licenses/LICENSE-2.0
14   * 
15   * Unless required by applicable law or agreed to in writing, software
16   * distributed under the License is distributed on an "AS IS" BASIS,
17   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
18   * See the License for the specific language governing permissions and
19   * limitations under the License.
20   * #L%
21   */
22  
23  import java.math.BigDecimal;
24  import java.math.MathContext;
25  import java.math.RoundingMode;
26  
27  import org.hl7.fhir.instance.model.api.IBaseDecimalDatatype;
28  
29  import ca.uhn.fhir.model.api.BasePrimitive;
30  import ca.uhn.fhir.model.api.annotation.DatatypeDef;
31  import ca.uhn.fhir.model.api.annotation.SimpleSetter;
32  
33  @DatatypeDef(name = "decimal")
34  public class DecimalDt extends BasePrimitive<BigDecimal> implements Comparable<DecimalDt>, IBaseDecimalDatatype {
35  
36  	/**
37  	 * Constructor
38  	 */
39  	public DecimalDt() {
40  		super();
41  	}
42  
43  	/**
44  	 * Constructor
45  	 */
46  	@SimpleSetter
47  	public DecimalDt(@SimpleSetter.Parameter(name = "theValue") BigDecimal theValue) {
48  		setValue(theValue);
49  	}
50  
51  	/**
52  	 * Constructor
53  	 */
54  	@SimpleSetter
55  	public DecimalDt(@SimpleSetter.Parameter(name = "theValue") double theValue) {
56  		// Use the valueOf here because the constructor gives crazy precision
57  		// changes due to the floating point conversion
58  		setValue(BigDecimal.valueOf(theValue));
59  	}
60  
61  	/**
62  	 * Constructor
63  	 */
64  	@SimpleSetter
65  	public DecimalDt(@SimpleSetter.Parameter(name = "theValue") long theValue) {
66  		setValue(new BigDecimal(theValue));
67  	}
68  
69  	/**
70  	 * Constructor
71  	 */
72  	public DecimalDt(String theValue) {
73  		setValue(new BigDecimal(theValue));
74  	}
75  
76  	@Override
77  	public int compareTo(DecimalDt theObj) {
78  		if (getValue() == null && theObj.getValue() == null) {
79  			return 0;
80  		}
81  		if (getValue() != null && (theObj == null || theObj.getValue() == null)) {
82  			return 1;
83  		}
84  		if (getValue() == null && theObj.getValue() != null) {
85  			return -1;
86  		}
87  		return getValue().compareTo(theObj.getValue());
88  	}
89  
90  	@Override
91  	protected String encode(BigDecimal theValue) {
92  		return getValue().toPlainString();
93  	}
94  
95  	/**
96  	 * Gets the value as an integer, using {@link BigDecimal#intValue()}
97  	 */
98  	public int getValueAsInteger() {
99  		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 }