Class DecimalQuantity_AbstractBCD

java.lang.Object
com.ibm.icu.impl.number.DecimalQuantity_AbstractBCD
All Implemented Interfaces:
DecimalQuantity, PluralRules.IFixedDecimal
Direct Known Subclasses:
DecimalQuantity_DualStorageBCD

public abstract class DecimalQuantity_AbstractBCD extends Object implements DecimalQuantity
Represents numbers and digit display properties using Binary Coded Decimal (BCD).
  • Field Details

    • 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().

    • flags

      protected byte flags
      A bitmask of properties relating to the number represented by this object.
      See Also:
    • NEGATIVE_FLAG

      protected static final int NEGATIVE_FLAG
      See Also:
    • INFINITY_FLAG

      protected static final int INFINITY_FLAG
      See Also:
    • NAN_FLAG

      protected static final int NAN_FLAG
      See Also:
    • 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
    • SECTION_LOWER_EDGE

      private static final int SECTION_LOWER_EDGE
      See Also:
    • SECTION_UPPER_EDGE

      private static final int SECTION_UPPER_EDGE
      See Also:
  • Constructor Details

    • DecimalQuantity_AbstractBCD

      public DecimalQuantity_AbstractBCD()
  • Method Details

    • 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.
    • clear

    • 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.
    • getPositionFingerprint

      public long getPositionFingerprint()
      Description copied from interface: DecimalQuantity
      This method is for internal testing only.
      Specified by:
      getPositionFingerprint in interface DecimalQuantity
    • roundToIncrement

      public void roundToIncrement(BigDecimal roundingIncrement, 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(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 ArithmeticException
      Specified by:
      getMagnitude in interface DecimalQuantity
      Returns:
      The power of ten corresponding to the most significant nonzero digit.
      Throws:
      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
    • isHasIntegerValue

      public boolean isHasIntegerValue()
      Description copied from interface: PluralRules.IFixedDecimal
      Whether the number has no nonzero fraction digits.
      Specified by:
      isHasIntegerValue in interface PluralRules.IFixedDecimal
    • getStandardPlural

      public StandardPlural getStandardPlural(PluralRules rules)
      Description copied from interface: DecimalQuantity
      Computes the plural form for this number based on the specified set of rules.
      Specified by:
      getStandardPlural in interface DecimalQuantity
      Parameters:
      rules - A PluralRules object representing the set of rules.
      Returns:
      The StandardPlural according to the PluralRules. If the plural form is not in the set of standard plurals, StandardPlural.OTHER is returned instead.
    • getPluralOperand

      public double getPluralOperand(PluralRules.Operand operand)
      Description copied from interface: PluralRules.IFixedDecimal
      Returns the value corresponding to the specified operand (n, i, f, t, v, or w). If the operand is 'n', returns a double; otherwise, returns an integer.
      Specified by:
      getPluralOperand in interface PluralRules.IFixedDecimal
    • populateUFieldPosition

      public void populateUFieldPosition(FieldPosition fp)
      Description copied from interface: DecimalQuantity
      If the given FieldPosition is a UFieldPosition, populates it with the fraction length and fraction long value. If the argument is not a UFieldPosition, nothing happens.
      Specified by:
      populateUFieldPosition in interface DecimalQuantity
      Parameters:
      fp - The UFieldPosition to populate.
    • 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()
    • isNegative

      public boolean isNegative()
      Specified by:
      isNegative in interface DecimalQuantity
      Returns:
      Whether the value represented by this DecimalQuantity is less than zero.
    • signum

      public Modifier.Signum signum()
      Specified by:
      signum in interface DecimalQuantity
      Returns:
      The appropriate value from the Signum enum.
    • isInfinite

      public boolean isInfinite()
      Specified by:
      isInfinite in interface DecimalQuantity
      Specified by:
      isInfinite in interface PluralRules.IFixedDecimal
      Returns:
      Whether the value represented by this DecimalQuantity is infinite.
    • isNaN

      public boolean isNaN()
      Specified by:
      isNaN in interface DecimalQuantity
      Specified by:
      isNaN in interface PluralRules.IFixedDecimal
      Returns:
      Whether the value represented by this DecimalQuantity is not a number.
    • isZeroish

      public boolean isZeroish()
      Specified by:
      isZeroish in interface DecimalQuantity
      Returns:
      Whether the value represented by this DecimalQuantity is zero, infinity, or NaN.
    • 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(BigInteger n)
    • _setToBigInteger

      private void _setToBigInteger(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(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(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.
    • toBigDecimal

      public BigDecimal toBigDecimal()
      Specified by:
      toBigDecimal in interface DecimalQuantity
    • safeSubtract

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

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

      public void roundToNickel(int magnitude, 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, 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, MathContext mathContext, boolean nickel)
    • roundToInfinity

      public void roundToInfinity()
      Description copied from interface: DecimalQuantity
      Rounds the number to an infinite number of decimal points. This has no effect except for forcing the double in DecimalQuantity_AbstractBCD to adopt its exact representation.
      Specified by:
      roundToInfinity in interface DecimalQuantity
    • 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 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(StringBuilder result)
    • toScientificString

      public String toScientificString()
    • toScientificString

      public void toScientificString(StringBuilder result)
    • toExponentString

      public 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(StringBuilder result)
    • equals

      public boolean equals(Object other)
      Overrides:
      equals in class 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(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 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().