java.lang.Object
java.lang.Number
javax.money.NumberValue
- All Implemented Interfaces:
java.io.Serializable
,java.lang.Comparable<NumberValue>
public abstract class NumberValue extends java.lang.Number implements java.lang.Comparable<NumberValue>
Instances of this class allow to externalize the numeric value of a
MonetaryAmount
. The class extends
Number
for maximal compatibility with the JDK but also adds additional functionality to
overcome some of the pitfalls of the JDK's Number class.
Implementation specification
Implementations of this interface must be :
- Comparable, hereby comparing the numeric value.
- immutable
- serializable
- thread-safe
- Author:
- Anatole Tresch
- See Also:
- Serialized Form
-
Constructor Summary
Constructors Constructor Description NumberValue()
-
Method Summary
Modifier and Type Method Description int
compareTo(NumberValue other)
abstract double
doubleValueExact()
Access the numeric value asdouble
.abstract long
getAmountFractionDenominator()
This method allows to extract the denominator part of the current fraction, hereby givenabstract long
getAmountFractionNumerator()
This method allows to extract the numerator part of the current fraction, hereby givenabstract java.lang.Class<?>
getNumberType()
Get the numeric implementation type, that is the base of this number.abstract int
getPrecision()
Returns the precision of thisMonetaryAmount
.abstract int
getScale()
Returns the scale of thisMonetaryAmount
.abstract int
intValueExact()
Access the numeric value asint
.abstract long
longValueExact()
Access the numeric value aslong
.abstract <T extends java.lang.Number>
TnumberValue(java.lang.Class<T> numberType)
Access the numeric value asNumber
.abstract <T extends java.lang.Number>
TnumberValueExact(java.lang.Class<T> numberType)
Access the numeric value asNumber
.abstract NumberValue
round(java.math.MathContext mathContext)
Access the current NumberValue rounded using the givenMathContext
.
-
Constructor Details
-
NumberValue
public NumberValue()
-
-
Method Details
-
getNumberType
public abstract java.lang.Class<?> getNumberType()Get the numeric implementation type, that is the base of this number.- Returns:
- the numeric implementation type, not
null
.
-
getPrecision
public abstract int getPrecision()Returns the precision of thisMonetaryAmount
. (The precision is the number of digits in the unscaled value.)The precision of a zero value is 1.
- Returns:
- the precision of this
MonetaryAmount
.
-
getScale
public abstract int getScale()Returns the scale of thisMonetaryAmount
. If zero or positive, the scale is the number of digits to the right of the decimal point. If negative, the unscaled value of the number is multiplied by ten to the power of the negation of the scale. For example, a scale of-3
means the unscaled value is multiplied by 1000.- Returns:
- the scale of this
MonetaryAmount
.
-
intValueExact
public abstract int intValueExact()Access the numeric value asint
. Hereby no truncation will be performed to fit the value into the target data type.- Returns:
- the (possibly) truncated value of the
MonetaryAmount
. - Throws:
java.lang.ArithmeticException
- If the value must be truncated to fit the target datatype.
-
longValueExact
public abstract long longValueExact()Access the numeric value aslong
. Hereby no truncation will be performed to fit the value into the target data type.- Returns:
- the (possibly) truncated value of the
MonetaryAmount
. - Throws:
java.lang.ArithmeticException
- If the value must be truncated to fit the target datatype.
-
doubleValueExact
public abstract double doubleValueExact()Access the numeric value asdouble
. Hereby no truncation will be performed to fit the value into the target data type.- Returns:
- the (possibly) truncated value of the
MonetaryAmount
. - Throws:
java.lang.ArithmeticException
- If the value must be truncated to fit the target datatype.
-
numberValue
public abstract <T extends java.lang.Number> T numberValue(java.lang.Class<T> numberType)Access the numeric value asNumber
. Hereby truncation may be performed as needed to fit the value into the target data type.- Parameters:
numberType
- The concrete number class to be returned. Basically the following Number types, must be supported if available on the corresponding runtime platform:java.lang.Long
java.lang.Double
java.lang.Number
java.math.BigInteger
, currently not available on all platforms.java.math.BigDecimal
, currently not available on all platforms.
- Returns:
- the (possibly) truncated value of the
MonetaryAmount
.
-
round
Access the current NumberValue rounded using the givenMathContext
.- Parameters:
mathContext
- theMathContext
to be applied.- Returns:
- the new NumberValue, never null.
- See Also:
BigDecimal.round(java.math.MathContext)
-
numberValueExact
public abstract <T extends java.lang.Number> T numberValueExact(java.lang.Class<T> numberType)Access the numeric value asNumber
. Hereby no truncation will be performed to fit the value into the target data type.- Parameters:
numberType
- The concrete number class to be returned. Basically the following Number types, must be supported if available on the corresponding runtime platform:java.lang.Long
java.lang.Double
java.lang.Number
java.math.BigInteger
, currently not available on all platforms.java.math.BigDecimal
, currently not available on all platforms.
- Returns:
- the (possibly) truncated value of the
MonetaryAmount
. - Throws:
java.lang.ArithmeticException
- If the value must be truncated to fit the target datatype.
-
getAmountFractionNumerator
public abstract long getAmountFractionNumerator()This method allows to extract the numerator part of the current fraction, hereby given
the following must be always true:w = longValue() n = getFractionNominator() d = getFractionDenominator()
.!(w<0 && n>0) and !(w>0 && n<0) and d>0 and |n| < d // || = absolute value
- Returns:
- the amount's fraction numerator..
-
getAmountFractionDenominator
public abstract long getAmountFractionDenominator()This method allows to extract the denominator part of the current fraction, hereby given
the following must be always true:w = longValue() n = getFractionNominator() d = getFractionDenominator()
.!(w<0 && n>0) and !(w>0 && n<0) and d>0 and |n| < d // || = absolute value
- Returns:
- the amount's fraction denominator.
-
compareTo
- Specified by:
compareTo
in interfacejava.lang.Comparable<NumberValue>
-