Class DecimalQuantity_AbstractBCD

    • Field Summary

      Fields 
      Modifier and Type Field Description
      private static double[] DOUBLE_MULTIPLIERS  
      boolean explicitExactDouble
      Deprecated.
      This API is ICU internal only.
      protected int exponent
      The value of the (suppressed) exponent after the number has been put into a notation with exponents (ex: compact, scientific).
      protected byte flags
      A bitmask of properties relating to the number represented by this object.
      protected static int INFINITY_FLAG  
      (package private) static byte[] INT64_BCD  
      protected boolean isApproximate
      Whether the value in the BCD comes from the double fast path without having been rounded to ensure correctness
      protected int lReqPos  
      protected static int NAN_FLAG  
      protected static int NEGATIVE_FLAG  
      protected int origDelta
      The change in magnitude relative to the original double.
      protected double origDouble
      The original number provided by the user and which is represented in BCD.
      protected int precision
      The number of digits in the BCD.
      protected int rReqPos  
      protected int scale
      The power of ten corresponding to the least significant digit in the BCD.
      private static int SECTION_LOWER_EDGE  
      private static int SECTION_UPPER_EDGE  
    • Method Summary

      All Methods Static Methods Instance Methods Abstract Methods Concrete Methods Deprecated Methods 
      Modifier and Type Method Description
      private void _setToBigDecimal​(java.math.BigDecimal n)  
      private void _setToBigInteger​(java.math.BigInteger n)  
      private void _setToDoubleFast​(double n)
      Uses double multiplication and division to get the number into integer space before converting to digits.
      private void _setToInt​(int n)  
      private void _setToLong​(long n)  
      void adjustExponent​(int delta)
      Adjusts the value for the (suppressed) exponent stored when using notation with exponents (ex: compact, scientific).
      void adjustMagnitude​(int delta)
      Scales the number by a power of ten.
      void appendDigit​(byte value, int leadingZeros, boolean appendAsInteger)
      Deprecated.
      This API is ICU internal only.
      void applyMaxInteger​(int maxInt)
      Truncates digits from the upper magnitude of the number in order to satisfy the specified maximum number of integer digits.
      protected abstract java.math.BigDecimal bcdToBigDecimal()
      Returns a BigDecimal encoding the internal BCD value.
      DecimalQuantity_AbstractBCD clear()  
      protected abstract void compact()
      Removes trailing zeros from the BCD (adjusting the scale as required) and then computes the precision.
      private void convertToAccurateDouble()
      Uses Double.toString() to obtain an exact accurate representation of the double, overwriting it into the BCD.
      protected abstract void copyBcdFrom​(DecimalQuantity _other)  
      void copyFrom​(DecimalQuantity _other)
      Sets this instance to be equal to another instance.
      boolean equals​(java.lang.Object other)  
      boolean fitsInLong()
      Returns whether or not a Long can fully represent the value stored in this DecimalQuantity.
      private int fractionCount()  
      private int fractionCountWithoutTrailingZeros()  
      byte getDigit​(int magnitude)
      Gets the digit at the specified magnitude.
      protected abstract byte getDigitPos​(int position)
      Returns a single digit from the BCD list.
      int getExponent()  
      int getLowerDisplayMagnitude()
      Gets the smallest power of ten that needs to be displayed.
      int getMagnitude()  
      double getPluralOperand​(PluralRules.Operand operand)
      Returns the value corresponding to the specified operand (n, i, f, t, v, or w).
      long getPositionFingerprint()
      This method is for internal testing only.
      StandardPlural getStandardPlural​(PluralRules rules)
      Computes the plural form for this number based on the specified set of rules.
      int getUpperDisplayMagnitude()
      Gets the largest power of ten that needs to be displayed.
      boolean isHasIntegerValue()
      Whether the number has no nonzero fraction digits.
      boolean isInfinite()  
      boolean isNaN()  
      boolean isNegative()  
      boolean isZeroish()  
      void multiplyBy​(java.math.BigDecimal multiplicand)
      Multiply the internal value.
      void negate()
      Flips the sign from positive to negative and back.
      protected abstract void popFromLeft​(int numDigits)
      Directly removes digits from the front of the BCD list.
      void populateUFieldPosition​(java.text.FieldPosition fp)
      If the given FieldPosition is a UFieldPosition, populates it with the fraction length and fraction long value.
      protected abstract void readBigIntegerToBcd​(java.math.BigInteger input)
      Sets the internal BCD state to represent the value in the given BigInteger.
      protected abstract void readIntToBcd​(int input)
      Sets the internal BCD state to represent the value in the given int.
      protected abstract void readLongToBcd​(long input)
      Sets the internal BCD state to represent the value in the given long.
      void resetExponent()
      Resets the DecimalQuantity to the value before adjustMagnitude and adjustExponent.
      void roundToIncrement​(java.math.BigDecimal roundingIncrement, java.math.MathContext mathContext)
      Rounds the number to a specified interval, such as 0.05.
      void roundToInfinity()
      Rounds the number to an infinite number of decimal points.
      void roundToMagnitude​(int magnitude, java.math.MathContext mathContext)
      Rounds the number to a specified magnitude (power of ten).
      private void roundToMagnitude​(int magnitude, java.math.MathContext mathContext, boolean nickel)  
      void roundToNickel​(int magnitude, java.math.MathContext mathContext)
      Rounds the number to the nearest multiple of 5 at the specified magnitude.
      private static int safeSubtract​(int a, int b)  
      protected abstract void setBcdToZero()
      Sets the internal representation to zero.
      protected abstract void setDigitPos​(int position, byte value)
      Sets the digit in the BCD list.
      void setMinFraction​(int minFrac)
      Sets the minimum fraction digits that this DecimalQuantity should generate.
      void setMinInteger​(int minInt)
      Sets the minimum integer digits that this DecimalQuantity should generate.
      void setToBigDecimal​(java.math.BigDecimal n)
      Sets the internal BCD state to represent the value in the given BigDecimal.
      void setToBigInteger​(java.math.BigInteger n)  
      void setToDouble​(double n)
      Sets the internal BCD state to represent the value in the given double.
      void setToInt​(int n)  
      void setToLong​(long n)  
      protected abstract void shiftLeft​(int numDigits)
      Adds zeros to the end of the BCD list.
      protected abstract void shiftRight​(int numDigits)
      Removes digits from the end of the BCD list.
      Modifier.Signum signum()  
      java.math.BigDecimal toBigDecimal()  
      double toDouble()
      Returns a double approximating the internal BCD.
      java.lang.String toExponentString()
      Returns the string using ASCII digits and using exponential notation for non-zero exponents, following the UTS 35 specification for plural rule samples.
      private void toExponentString​(java.lang.StringBuilder result)  
      long toFractionLong​(boolean includeTrailingZeros)
      This returns a long representing the fraction digits of the number, as required by PluralRules.
      long toLong​(boolean truncateIfOverflow)
      Returns a long approximating the decimal quantity.
      java.lang.String toPlainString()
      Returns the string in "plain" format (no exponential notation) using ASCII digits.
      void toPlainString​(java.lang.StringBuilder result)  
      java.lang.String toScientificString()  
      void toScientificString​(java.lang.StringBuilder result)  
      void truncate()
      Removes all fraction digits.
      • Methods inherited from class java.lang.Object

        clone, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
    • Field Detail

      • scale

        protected int scale
        The power of ten corresponding to the least significant digit in the BCD. For example, if this object represents the number "3.14", the BCD will be "0x314" and the scale will be -2.

        Note that in BigDecimal, the scale is defined differently: the number of digits after the decimal place, which is the negative of our definition of scale.

      • precision

        protected int precision
        The number of digits in the BCD. For example, "1007" has BCD "0x1007" and precision 4. A long cannot represent precisions greater than 16.

        This value must be re-calculated whenever the value in bcd changes by using #computePrecisionAndCompact().

      • origDouble

        protected double origDouble
        The original number provided by the user and which is represented in BCD. Used when we need to re-compute the BCD for an exact double representation.
      • origDelta

        protected int origDelta
        The change in magnitude relative to the original double. Used when we need to re-compute the BCD for an exact double representation.
      • isApproximate

        protected boolean isApproximate
        Whether the value in the BCD comes from the double fast path without having been rounded to ensure correctness
      • lReqPos

        protected int lReqPos
      • rReqPos

        protected int rReqPos
      • exponent

        protected int exponent
        The value of the (suppressed) exponent after the number has been put into a notation with exponents (ex: compact, scientific).
      • DOUBLE_MULTIPLIERS

        private static final double[] DOUBLE_MULTIPLIERS
      • explicitExactDouble

        @Deprecated
        public boolean explicitExactDouble
        Deprecated.
        This API is ICU internal only.
        Whether this DecimalQuantity_DualStorageBCD has been explicitly converted to an exact double. true if backed by a double that was explicitly converted via convertToAccurateDouble; false otherwise. Used for testing.
      • INT64_BCD

        static final byte[] INT64_BCD
    • Constructor Detail

      • DecimalQuantity_AbstractBCD

        public DecimalQuantity_AbstractBCD()
    • Method Detail

      • copyFrom

        public void copyFrom​(DecimalQuantity _other)
        Description copied from interface: DecimalQuantity
        Sets this instance to be equal to another instance.
        Specified by:
        copyFrom in interface DecimalQuantity
        Parameters:
        _other - The instance to copy from.
      • setMinInteger

        public void setMinInteger​(int minInt)
        Description copied from interface: DecimalQuantity
        Sets the minimum integer digits that this DecimalQuantity should generate. This method does not perform rounding.
        Specified by:
        setMinInteger in interface DecimalQuantity
        Parameters:
        minInt - The minimum number of integer digits.
      • setMinFraction

        public void setMinFraction​(int minFrac)
        Description copied from interface: DecimalQuantity
        Sets the minimum fraction digits that this DecimalQuantity should generate. This method does not perform rounding.
        Specified by:
        setMinFraction in interface DecimalQuantity
        Parameters:
        minFrac - The minimum number of fraction digits.
      • applyMaxInteger

        public void applyMaxInteger​(int maxInt)
        Description copied from interface: DecimalQuantity
        Truncates digits from the upper magnitude of the number in order to satisfy the specified maximum number of integer digits.
        Specified by:
        applyMaxInteger in interface DecimalQuantity
        Parameters:
        maxInt - The maximum number of integer digits.
      • roundToIncrement

        public void roundToIncrement​(java.math.BigDecimal roundingIncrement,
                                     java.math.MathContext mathContext)
        Description copied from interface: DecimalQuantity
        Rounds the number to a specified interval, such as 0.05.

        If rounding to a power of ten, use the more efficient DecimalQuantity.roundToMagnitude(int, java.math.MathContext) instead.

        Specified by:
        roundToIncrement in interface DecimalQuantity
        Parameters:
        roundingIncrement - The increment to which to round.
        mathContext - The MathContext to use if rounding is necessary. Undefined behavior if null.
      • multiplyBy

        public void multiplyBy​(java.math.BigDecimal multiplicand)
        Description copied from interface: DecimalQuantity
        Multiply the internal value.
        Specified by:
        multiplyBy in interface DecimalQuantity
        Parameters:
        multiplicand - The value by which to multiply.
      • negate

        public void negate()
        Description copied from interface: DecimalQuantity
        Flips the sign from positive to negative and back.
        Specified by:
        negate in interface DecimalQuantity
      • getMagnitude

        public int getMagnitude()
                         throws java.lang.ArithmeticException
        Specified by:
        getMagnitude in interface DecimalQuantity
        Returns:
        The power of ten corresponding to the most significant nonzero digit.
        Throws:
        java.lang.ArithmeticException - If the value represented is zero.
      • adjustMagnitude

        public void adjustMagnitude​(int delta)
        Description copied from interface: DecimalQuantity
        Scales the number by a power of ten. For example, if the value is currently "1234.56", calling this method with delta=-3 will change the value to "1.23456".
        Specified by:
        adjustMagnitude in interface DecimalQuantity
        Parameters:
        delta - The number of magnitudes of ten to change by.
      • getExponent

        public int getExponent()
        Specified by:
        getExponent in interface DecimalQuantity
        Returns:
        The value of the (suppressed) exponent after the number has been put into a notation with exponents (ex: compact, scientific). Ex: given the number 1000 as "1K" / "1E3", the return value will be 3 (positive).
      • adjustExponent

        public void adjustExponent​(int delta)
        Description copied from interface: DecimalQuantity
        Adjusts the value for the (suppressed) exponent stored when using notation with exponents (ex: compact, scientific).

        Adjusting the exponent is decoupled from DecimalQuantity.adjustMagnitude(int) in order to allow flexibility for StandardPlural to be selected in formatting (ex: for compact notation) either with or without the exponent applied in the value of the number.

        Specified by:
        adjustExponent in interface DecimalQuantity
        Parameters:
        delta - The value to adjust the exponent by.
      • resetExponent

        public void resetExponent()
        Description copied from interface: DecimalQuantity
        Resets the DecimalQuantity to the value before adjustMagnitude and adjustExponent.
        Specified by:
        resetExponent in interface DecimalQuantity
      • getUpperDisplayMagnitude

        public int getUpperDisplayMagnitude()
        Description copied from interface: DecimalQuantity
        Gets the largest power of ten that needs to be displayed. The value returned by this function will be bounded between minInt and maxInt.
        Specified by:
        getUpperDisplayMagnitude in interface DecimalQuantity
        Returns:
        The highest-magnitude digit to be displayed.
      • getLowerDisplayMagnitude

        public int getLowerDisplayMagnitude()
        Description copied from interface: DecimalQuantity
        Gets the smallest power of ten that needs to be displayed. The value returned by this function will be bounded between -minFrac and -maxFrac.
        Specified by:
        getLowerDisplayMagnitude in interface DecimalQuantity
        Returns:
        The lowest-magnitude digit to be displayed.
      • getDigit

        public byte getDigit​(int magnitude)
        Description copied from interface: DecimalQuantity
        Gets the digit at the specified magnitude. For example, if the represented number is 12.3, getDigit(-1) returns 3, since 3 is the digit corresponding to 10^-1.
        Specified by:
        getDigit in interface DecimalQuantity
        Parameters:
        magnitude - The magnitude of the digit.
        Returns:
        The digit at the specified magnitude.
      • fractionCount

        private int fractionCount()
      • fractionCountWithoutTrailingZeros

        private int fractionCountWithoutTrailingZeros()
      • setToInt

        public void setToInt​(int n)
      • _setToInt

        private void _setToInt​(int n)
      • setToLong

        public void setToLong​(long n)
      • _setToLong

        private void _setToLong​(long n)
      • setToBigInteger

        public void setToBigInteger​(java.math.BigInteger n)
      • _setToBigInteger

        private void _setToBigInteger​(java.math.BigInteger n)
      • setToDouble

        public void setToDouble​(double n)
        Sets the internal BCD state to represent the value in the given double.
        Parameters:
        n - The value to consume.
      • _setToDoubleFast

        private void _setToDoubleFast​(double n)
        Uses double multiplication and division to get the number into integer space before converting to digits. Since double arithmetic is inexact, the resulting digits may not be accurate.
      • convertToAccurateDouble

        private void convertToAccurateDouble()
        Uses Double.toString() to obtain an exact accurate representation of the double, overwriting it into the BCD. This method can be called at any point after _setToDoubleFast(double) while isApproximate is still true.
      • setToBigDecimal

        public void setToBigDecimal​(java.math.BigDecimal n)
        Sets the internal BCD state to represent the value in the given BigDecimal.
        Specified by:
        setToBigDecimal in interface DecimalQuantity
        Parameters:
        n - The value to consume.
      • _setToBigDecimal

        private void _setToBigDecimal​(java.math.BigDecimal n)
      • toLong

        public long toLong​(boolean truncateIfOverflow)
        Description copied from interface: DecimalQuantity
        Returns a long approximating the decimal quantity. A long can only represent the integral part of the number. Note: this method incorporates the value of getExponent (for cases such as compact notation) to return the proper long value represented by the result.
        Specified by:
        toLong in interface DecimalQuantity
        Parameters:
        truncateIfOverflow - if false and the number does NOT fit, fails with an error. See comment about call site guards in DecimalQuantity_AbstractBCD.java
        Returns:
        A 64-bit integer representation of the internal number.
      • toFractionLong

        public long toFractionLong​(boolean includeTrailingZeros)
        This returns a long representing the fraction digits of the number, as required by PluralRules. For example, if we represent the number "1.20" (including optional and required digits), then this function returns "20" if includeTrailingZeros is true or "2" if false. Note: this method incorporates the value of exponent (for cases such as compact notation) to return the proper long value represented by the result.
      • fitsInLong

        public boolean fitsInLong()
        Returns whether or not a Long can fully represent the value stored in this DecimalQuantity.
      • toDouble

        public double toDouble()
        Returns a double approximating the internal BCD. The double may not retain all of the information encoded in the BCD if the BCD represents a number out of range of a double.
        Specified by:
        toDouble in interface DecimalQuantity
        Returns:
        A double representation of the internal BCD.
      • safeSubtract

        private static int safeSubtract​(int a,
                                        int b)
      • truncate

        public void truncate()
        Removes all fraction digits.
      • roundToNickel

        public void roundToNickel​(int magnitude,
                                  java.math.MathContext mathContext)
        Description copied from interface: DecimalQuantity
        Rounds the number to the nearest multiple of 5 at the specified magnitude. For example, when magnitude == -2, this performs rounding to the nearest 0.05.
        Specified by:
        roundToNickel in interface DecimalQuantity
        Parameters:
        magnitude - The magnitude at which the digit should become either 0 or 5.
        mathContext - Rounding strategy.
      • roundToMagnitude

        public void roundToMagnitude​(int magnitude,
                                     java.math.MathContext mathContext)
        Description copied from interface: DecimalQuantity
        Rounds the number to a specified magnitude (power of ten).
        Specified by:
        roundToMagnitude in interface DecimalQuantity
        Parameters:
        magnitude - The power of ten to which to round. For example, a value of -2 will round to 2 decimal places.
        mathContext - The MathContext to use if rounding is necessary. Undefined behavior if null.
      • roundToMagnitude

        private void roundToMagnitude​(int magnitude,
                                      java.math.MathContext mathContext,
                                      boolean nickel)
      • appendDigit

        @Deprecated
        public void appendDigit​(byte value,
                                int leadingZeros,
                                boolean appendAsInteger)
        Deprecated.
        This API is ICU internal only.
        Appends a digit, optionally with one or more leading zeros, to the end of the value represented by this DecimalQuantity.

        The primary use of this method is to construct numbers during a parsing loop. It allows parsing to take advantage of the digit list infrastructure primarily designed for formatting.

        Parameters:
        value - The digit to append.
        leadingZeros - The number of zeros to append before the digit. For example, if the value in this instance starts as 12.3, and you append a 4 with 1 leading zero, the value becomes 12.304.
        appendAsInteger - If true, increase the magnitude of existing digits to make room for the new digit. If false, append to the end like a fraction digit. If true, there must not be any fraction digits already in the number.
      • toPlainString

        public java.lang.String toPlainString()
        Description copied from interface: DecimalQuantity
        Returns the string in "plain" format (no exponential notation) using ASCII digits.
        Specified by:
        toPlainString in interface DecimalQuantity
      • toPlainString

        public void toPlainString​(java.lang.StringBuilder result)
      • toScientificString

        public java.lang.String toScientificString()
      • toScientificString

        public void toScientificString​(java.lang.StringBuilder result)
      • toExponentString

        public java.lang.String toExponentString()
        Description copied from interface: DecimalQuantity
        Returns the string using ASCII digits and using exponential notation for non-zero exponents, following the UTS 35 specification for plural rule samples.
        Specified by:
        toExponentString in interface DecimalQuantity
      • toExponentString

        private void toExponentString​(java.lang.StringBuilder result)
      • equals

        public boolean equals​(java.lang.Object other)
        Overrides:
        equals in class java.lang.Object
      • getDigitPos

        protected abstract byte getDigitPos​(int position)
        Returns a single digit from the BCD list. No internal state is changed by calling this method.
        Parameters:
        position - The position of the digit to pop, counted in BCD units from the least significant digit. If outside the range supported by the implementation, zero is returned.
        Returns:
        The digit at the specified location.
      • setDigitPos

        protected abstract void setDigitPos​(int position,
                                            byte value)
        Sets the digit in the BCD list. This method only sets the digit; it is the caller's responsibility to call compact() after setting the digit, and to ensure that the precision field is updated to reflect the correct number of digits if a nonzero digit is added to the decimal.
        Parameters:
        position - The position of the digit to pop, counted in BCD units from the least significant digit. If outside the range supported by the implementation, an AssertionError is thrown.
        value - The digit to set at the specified location.
      • shiftLeft

        protected abstract void shiftLeft​(int numDigits)
        Adds zeros to the end of the BCD list. This will result in an invalid BCD representation; it is the caller's responsibility to do further manipulation and then call compact().
        Parameters:
        numDigits - The number of zeros to add.
      • shiftRight

        protected abstract void shiftRight​(int numDigits)
        Removes digits from the end of the BCD list. This may result in an invalid BCD representation; it is the caller's responsibility to follow-up with a call to compact().
        Parameters:
        numDigits - The number of digits to remove.
      • popFromLeft

        protected abstract void popFromLeft​(int numDigits)
        Directly removes digits from the front of the BCD list. Updates precision. CAUTION: it is the caller's responsibility to call compact() after this method.
      • setBcdToZero

        protected abstract void setBcdToZero()
        Sets the internal representation to zero. Clears any values stored in scale, precision, hasDouble, origDouble, origDelta, exponent, and BCD data.
      • readIntToBcd

        protected abstract void readIntToBcd​(int input)
        Sets the internal BCD state to represent the value in the given int. The int is guaranteed to be either positive. The internal state is guaranteed to be empty when this method is called.
        Parameters:
        n - The value to consume.
      • readLongToBcd

        protected abstract void readLongToBcd​(long input)
        Sets the internal BCD state to represent the value in the given long. The long is guaranteed to be either positive. The internal state is guaranteed to be empty when this method is called.
        Parameters:
        n - The value to consume.
      • readBigIntegerToBcd

        protected abstract void readBigIntegerToBcd​(java.math.BigInteger input)
        Sets the internal BCD state to represent the value in the given BigInteger. The BigInteger is guaranteed to be positive, and it is guaranteed to be larger than Long.MAX_VALUE. The internal state is guaranteed to be empty when this method is called.
        Parameters:
        n - The value to consume.
      • bcdToBigDecimal

        protected abstract java.math.BigDecimal bcdToBigDecimal()
        Returns a BigDecimal encoding the internal BCD value.
        Returns:
        A BigDecimal representation of the internal BCD.
      • copyBcdFrom

        protected abstract void copyBcdFrom​(DecimalQuantity _other)
      • compact

        protected abstract void compact()
        Removes trailing zeros from the BCD (adjusting the scale as required) and then computes the precision. The precision is the number of digits in the number up through the greatest nonzero digit.

        This method must always be called when bcd changes in order for assumptions to be correct in methods like fractionCount().