Class NumberContext

  • All Implemented Interfaces:
    TypeContext<java.lang.Comparable<?>>

    public final class NumberContext
    extends FormatContext<java.lang.Comparable<?>>

    Think of this as a MathContext that specifies both precision and scale. Numeric data types (non-integers) in databases are specified using precision and scale. While doing maths the precision is all that matters, but before sending a number to a database, or printing/displaying it, rounding to a specified scale is desirable.

    The enforce methods first enforce the precision and then set the scale. It is possible that this will create a number with trailing zeros and more digits than the precision allows. It is also possible to define a context with a scale that is larger than the precision. This is NOT how precision and scale is used with numeric types in databases.

    • Field Detail

      • DEFAULT_MATH

        private static final java.math.MathContext DEFAULT_MATH
      • DEFAULT_STYLE

        private static final NumberStyle DEFAULT_STYLE
      • myEpsilon

        private final double myEpsilon
      • myMathContext

        private final java.math.MathContext myMathContext
      • myRoundingFactor

        private final double myRoundingFactor
      • myScale

        private final int myScale
      • myZeroError

        private final double myZeroError
    • Constructor Detail

      • NumberContext

        private NumberContext​(java.text.NumberFormat format,
                              int precision,
                              int scale,
                              java.math.RoundingMode mode)
      • NumberContext

        NumberContext​(java.text.NumberFormat format,
                      java.math.MathContext math,
                      int scale)
    • Method Detail

      • compare

        public static int compare​(double arg1,
                                  double arg2)
        Variation of Double.compare(double, double) that returns 0 if arg1 == arg2.
      • compare

        public static int compare​(float arg1,
                                  float arg2)
        Variation of Float.compare(float, float) that returns 0 if arg1 == arg2.
      • getCurrency

        public static NumberContext getCurrency​(java.util.Locale locale)
      • getInteger

        public static NumberContext getInteger​(java.util.Locale locale)
      • getPercent

        public static NumberContext getPercent​(int scale,
                                               java.util.Locale locale)
      • getPercent

        public static NumberContext getPercent​(java.util.Locale locale)
      • of

        public static NumberContext of​(int precision,
                                       int scale)
      • ofMath

        public static NumberContext ofMath​(java.math.MathContext math)
      • ofPrecision

        public static NumberContext ofPrecision​(int precision)
      • toFormat

        public static java.text.Format toFormat​(NumberStyle style,
                                                java.util.Locale locale)
      • isZero

        private static boolean isZero​(double value,
                                      double tolerance)
      • enforce

        public java.math.BigDecimal enforce​(java.math.BigDecimal number)
        Will first enforce the precision, and then the scale. Both operations will comply with the rounding mode.
      • enforce

        public java.lang.Comparable<?> enforce​(java.lang.Comparable<?> object)
        Description copied from interface: TypeContext
        Will force the object to conform to the context's specification. The default implementation formats a String and then parses that back to an object (of the original type).
      • enforce

        public double enforce​(double number)
        If precision is specified then this method instantiates a BigDecimal, enforces that, and then extracts a double again.

        If only a scale is specified then this enforces without creating any objects. In this case the precision is given by the type double and the rounding mode is always "half even" as given by Math.rint(double) (regardless of what rounding mode is specified).

      • epsilon

        public double epsilon()
        epsilon is defined as the difference between 1 and the next larger decimal number with the given number of digits (precision).
      • equals

        public boolean equals​(java.lang.Object obj)
        Overrides:
        equals in class java.lang.Object
        See Also:
        Object.equals(java.lang.Object)
      • format

        public java.lang.String format​(double number)
      • format

        public java.lang.String format​(long number)
      • getFormat

        public java.text.NumberFormat getFormat()
        Overrides:
        getFormat in class FormatContext<java.lang.Comparable<?>>
      • getMathContext

        public java.math.MathContext getMathContext()
      • getPrecision

        public int getPrecision()
      • getRoundingMode

        public java.math.RoundingMode getRoundingMode()
      • getScale

        public int getScale()
      • hashCode

        public int hashCode()
        Overrides:
        hashCode in class java.lang.Object
        See Also:
        Object.hashCode()
      • isDifferent

        public boolean isDifferent​(double expected,
                                   double actual)
      • isInteger

        public boolean isInteger​(double value)
      • isLessThan

        public boolean isLessThan​(java.math.BigDecimal reference,
                                  java.math.BigDecimal value)
      • isMinimal

        public boolean isMinimal​(java.math.BigDecimal value)
        The absolute smallest number allowed by this context, but not zero. A single ±1 at the very last decimal place.

        Say you rounded a number to scale 4 and ended up with a number that is ±1E-4.

      • isMoreThan

        public boolean isMoreThan​(java.math.BigDecimal reference,
                                  java.math.BigDecimal value)
      • isSmall

        public boolean isSmall​(java.math.BigDecimal comparedTo,
                               java.math.BigDecimal value)
      • isSmall

        public boolean isSmall​(double comparedTo,
                               double value)
      • isZero

        public boolean isZero​(java.math.BigDecimal value)
      • isZero

        public boolean isZero​(double value)
      • toBigDecimal

        public java.math.BigDecimal toBigDecimal​(double number)
        Will create an "enforced" BigDecimal instance.
      • toLocalizedPattern

        public java.lang.String toLocalizedPattern()
        Works with DecimalFormat and NumberContext.FormatPattern implementations. In other cases it returns null.
      • toPattern

        public java.lang.String toPattern()
        Works with DecimalFormat and NumberContext.FormatPattern implementations. In other cases it returns null.
      • toString

        public java.lang.String toString()
        Overrides:
        toString in class java.lang.Object
      • withDecrementedPrecision

        public NumberContext withDecrementedPrecision()
      • withDecrementedPrecision

        public NumberContext withDecrementedPrecision​(int subtrahend)
      • withDecrementedScale

        public NumberContext withDecrementedScale()
      • withDecrementedScale

        public NumberContext withDecrementedScale​(int subtrahend)
      • withDoubledPrecision

        public NumberContext withDoubledPrecision()
      • withHalvedPrecision

        public NumberContext withHalvedPrecision()
      • withIncrementedPrecision

        public NumberContext withIncrementedPrecision()
      • withIncrementedPrecision

        public NumberContext withIncrementedPrecision​(int addend)
      • withIncrementedScale

        public NumberContext withIncrementedScale()
      • withIncrementedScale

        public NumberContext withIncrementedScale​(int addend)
      • withMath

        public NumberContext withMath​(java.math.MathContext math)
      • withMode

        public NumberContext withMode​(java.math.RoundingMode mode)
      • withPrecision

        public NumberContext withPrecision​(int precision)
      • scale

        private java.math.BigDecimal scale​(java.math.BigDecimal number)
      • configureFormat

        protected void configureFormat​(java.text.Format format,
                                       java.lang.Object object)
        Specified by:
        configureFormat in class FormatContext<java.lang.Comparable<?>>
      • handleFormatException

        protected java.lang.String handleFormatException​(java.text.Format format,
                                                         java.lang.Object object)
        Specified by:
        handleFormatException in class FormatContext<java.lang.Comparable<?>>
      • handleParseException

        protected java.lang.Comparable<?> handleParseException​(java.text.Format format,
                                                               java.lang.String string)
        Specified by:
        handleParseException in class FormatContext<java.lang.Comparable<?>>