Module life.expert

Class NumberUtils


  • public class NumberUtils
    extends java.lang.Object
    This utility class simplifies implementing Amount, by providing the common functionality. The different explicitly typed methods are all reduced to methods using BigDecimal as input, hereby performing any conversion to BigDecimal as needed. Obviously this takes some time, so implementors that want to avoid this overhead should implement Amount directly.
    • Constructor Summary

      Constructors 
      Constructor Description
      NumberUtils()  
    • Method Summary

      Modifier and Type Method Description
      static void checkAmountParameter​(Quantity amount, Unit unit)
      Method to check if a unit is compatible with this amount instance.
      static void checkNoInfinityOrNaN​(java.lang.Number number)
      Check no infinity or na n.
      static void checkNumberParameter​(java.lang.Number number)
      Internal method to check for correct number parameter.
      static java.math.BigDecimal getBigDecimal​(double num)
      Creates a BigDecimal from the given Number doing the valid conversion depending the type given.
      static java.math.BigDecimal getBigDecimal​(long num)
      Creates a BigDecimal from the given Number doing the valid conversion depending the type given.
      static java.math.BigDecimal getBigDecimal​(java.lang.Number num)
      Creates a BigDecimal from the given Number doing the valid conversion depending the type given.
      static java.math.BigDecimal getBigDecimal​(java.lang.Number num, AmountContext amountContext)
      Creates a BigDecimal from the given Number doing the valid conversion depending the type given, if a AmountContext is given, it is applied to the number returned.
      static java.math.MathContext getMathContext​(AmountContext amountContext, java.math.RoundingMode defaultMode)
      Evaluates the MathContext from the given AmountContext.
      static boolean isInfinityAndNotNaN​(java.lang.Number number)
      Is infinity and not na n boolean.
      • Methods inherited from class java.lang.Object

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

      • NumberUtils

        public NumberUtils()
    • Method Detail

      • getBigDecimal

        public static java.math.BigDecimal getBigDecimal​(long num)
        Creates a BigDecimal from the given Number doing the valid conversion depending the type given.
        Parameters:
        num - the number type
        Returns:
        the corresponding BigDecimal
      • getBigDecimal

        public static java.math.BigDecimal getBigDecimal​(double num)
        Creates a BigDecimal from the given Number doing the valid conversion depending the type given.
        Parameters:
        num - the number type
        Returns:
        the corresponding BigDecimal
      • getBigDecimal

        public static java.math.BigDecimal getBigDecimal​(java.lang.Number num)
        Creates a BigDecimal from the given Number doing the valid conversion depending the type given.
        Parameters:
        num - the number type
        Returns:
        the corresponding BigDecimal
      • getBigDecimal

        public static java.math.BigDecimal getBigDecimal​(java.lang.Number num,
                                                         AmountContext amountContext)
        Creates a BigDecimal from the given Number doing the valid conversion depending the type given, if a AmountContext is given, it is applied to the number returned.
        Parameters:
        num - the number type
        amountContext - the money context
        Returns:
        the corresponding BigDecimal
      • getMathContext

        public static java.math.MathContext getMathContext​(AmountContext amountContext,
                                                           java.math.RoundingMode defaultMode)
        Evaluates the MathContext from the given AmountContext.
        Parameters:
        amountContext - the AmountContext
        defaultMode - the default RoundingMode, to be used if no one is set in AmountContext.
        Returns:
        the corresponding MathContext
      • checkAmountParameter

        public static void checkAmountParameter​(Quantity amount,
                                                Unit unit)
        Method to check if a unit is compatible with this amount instance.
        Parameters:
        amount - The amount to be compared to, never null.
        unit - the amount's unit to compare, never null.
        Throws:
        ValueException - If the amount is null, or the amount's Unit is not compatible, meaning has a different value of Unit.getCode() ()}).
      • checkNumberParameter

        public static void checkNumberParameter​(java.lang.Number number)
        Internal method to check for correct number parameter.
        Parameters:
        number - the number to be checked.
        Throws:
        java.lang.IllegalArgumentException - If the number is null
      • checkNoInfinityOrNaN

        public static void checkNoInfinityOrNaN​(java.lang.Number number)
        Check no infinity or na n.
        Parameters:
        number - the number
      • isInfinityAndNotNaN

        public static boolean isInfinityAndNotNaN​(java.lang.Number number)
        Is infinity and not na n boolean.
        Parameters:
        number - the number
        Returns:
        the boolean