Module java.money
Package javax.money

Class 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 as double.
      abstract long getAmountFractionDenominator()
      This method allows to extract the denominator part of the current fraction, hereby given
      abstract long getAmountFractionNumerator()
      This method allows to extract the numerator part of the current fraction, hereby given
      abstract java.lang.Class<?> getNumberType()
      Get the numeric implementation type, that is the base of this number.
      abstract int getPrecision()
      Returns the precision of this MonetaryAmount.
      abstract int getScale()
      Returns the scale of this MonetaryAmount.
      abstract int intValueExact()
      Access the numeric value as int.
      abstract long longValueExact()
      Access the numeric value as long.
      abstract <T extends java.lang.Number>
      T
      numberValue​(java.lang.Class<T> numberType)
      Access the numeric value as Number.
      abstract <T extends java.lang.Number>
      T
      numberValueExact​(java.lang.Class<T> numberType)
      Access the numeric value as Number.
      abstract NumberValue round​(java.math.MathContext mathContext)
      Access the current NumberValue rounded using the given MathContext.
      • Methods inherited from class java.lang.Number

        byteValue, doubleValue, floatValue, intValue, longValue, shortValue
      • Methods inherited from class java.lang.Object

        clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
    • Constructor Detail

      • NumberValue

        public NumberValue()
    • Method Detail

      • 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 this MonetaryAmount. (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 this MonetaryAmount. 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 as int. 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 as long. 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 as double. 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 as Number. 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

        public abstract NumberValue round​(java.math.MathContext mathContext)
        Access the current NumberValue rounded using the given MathContext.
        Parameters:
        mathContext - the MathContext 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 as Number. 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
        
         w = longValue()
         n = getFractionNominator()
         d = getFractionDenominator()
         
        the following must be always true:
        
         !(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
        
         w = longValue()
         n = getFractionNominator()
         d = getFractionDenominator()
         
        the following must be always true:
        
         !(w<0 && n>0)  and
         !(w>0 && n<0)  and
         d>0            and
         |n| < d        // || = absolute value
         
        .
        Returns:
        the amount's fraction denominator.
      • compareTo

        public int compareTo​(NumberValue other)
        Specified by:
        compareTo in interface java.lang.Comparable<NumberValue>