Class PackedDecimal


  • public final class PackedDecimal
    extends Object
    Arithmetic, copying and shifting operations for Packed Decimal data.
    • Method Summary

      All Methods Static Methods Concrete Methods 
      Modifier and Type Method Description
      static void addPackedDecimal​(byte[] result, int resultOffset, int resultPrecision, byte[] op1Decimal, int op1Offset, int op1Precision, byte[] op2Decimal, int op2Offset, int op2Precision, boolean checkOverflow)
      Add two Packed Decimals in byte arrays.
      static int checkPackedDecimal​(byte[] byteArray, int offset, int precision)
      Checks the validity of a Packed Decimal, return code indicating the status of the Packed Decimal.
      static int checkPackedDecimal​(byte[] byteArray, int offset, int precision, boolean ignoreHighNibbleForEvenPrecision)
      Checks the validity of a Packed Decimal, return code indicating the status of the Packed Decimal.
      static int checkPackedDecimal​(byte[] byteArray, int offset, int precision, boolean ignoreHighNibbleForEvenPrecision, boolean canOverwriteHighNibbleForEvenPrecision)
      Checks the validity of a Packed Decimal, return code indicating the status of the Packed Decimal.
      static void dividePackedDecimal​(byte[] result, int resultOffset, int resultPrecision, byte[] op1Decimal, int op1Offset, int op1Precision, byte[] op2Decimal, int op2Offset, int op2Precision, boolean checkOverflow)
      Divides two Packed Decimals is byte arrays.
      static boolean equalsPackedDecimal​(byte[] op1Decimal, int op1Offset, int op1Precision, byte[] op2Decimal, int op2Offset, int op2Precision)
      Checks if the two Packed Decimal operands are equal.
      static boolean greaterThanOrEqualsPackedDecimal​(byte[] op1Decimal, int op1Offset, int op1Precision, byte[] op2Decimal, int op2Offset, int op2Precision)
      Checks if the first Packed Decimal operand is greater than or equal to the second one.
      static boolean greaterThanPackedDecimal​(byte[] op1Decimal, int op1Offset, int op1Precision, byte[] op2Decimal, int op2Offset, int op2Precision)
      Checks if the first Packed Decimal operand is greater than the second one.
      static boolean lessThanOrEqualsPackedDecimal​(byte[] op1Decimal, int op1Offset, int op1Precision, byte[] op2Decimal, int op2Offset, int op2Precision)
      Checks if the first Packed Decimal operand is less than or equal to the second one.
      static boolean lessThanPackedDecimal​(byte[] op1Decimal, int op1Offset, int op1Precision, byte[] op2Decimal, int op2Offset, int op2Precision)
      Checks if the first Packed Decimal operand is lesser than the second one.
      static void movePackedDecimal​(byte[] destination, int destinationOffset, int destinationPrecision, byte[] source, int sourceOffset, int sourcePrecision, boolean checkOverflow)
      Creates a copy of a Packed Decimal.
      static void multiplyPackedDecimal​(byte[] result, int resultOffset, int resultPrecision, byte[] op1Decimal, int op1Offset, int op1Precision, byte[] op2Decimal, int op2Offset, int op2Precision, boolean checkOverflow)
      Multiplies two Packed Decimals in byte arrays.
      static boolean notEqualsPackedDecimal​(byte[] op1Decimal, int op1Offset, int op1Precision, byte[] op2Decimal, int op2Offset, int op2Precision)
      Checks if the two Packed Decimal operands are unequal.
      static void remainderPackedDecimal​(byte[] result, int resultOffset, int resultPrecision, byte[] op1Decimal, int op1Offset, int op1Precision, byte[] op2Decimal, int op2Offset, int op2Precision, boolean checkOverflow)
      Calculates the remainder resulting from the division of two Packed Decimals in byte arrays.
      static void setPackedZero​(byte[] byteArray, int offset, int len)
      Create a positive Packed Decimal representation of zero.
      static void shiftLeftPackedDecimal​(byte[] destination, int destinationOffset, int destinationPrecision, byte[] source, int sourceOffset, int sourcePrecision, int shiftAmount, boolean checkOverflow)
      Left shift a Packed Decimal appending zeros to the right.
      static void shiftRightPackedDecimal​(byte[] destination, int destinationOffset, int destinationPrecision, byte[] source, int sourceOffset, int sourcePrecision, int shiftAmount, boolean round, boolean checkOverflow)
      Right shift, and optionally round, a Packed Decimal.
      static void subtractPackedDecimal​(byte[] result, int resultOffset, int resultPrecision, byte[] op1Decimal, int op1Offset, int op1Precision, byte[] op2Decimal, int op2Offset, int op2Precision, boolean checkOverflow)
      Subtracts two Packed Decimals in byte arrays.
    • Method Detail

      • checkPackedDecimal

        public static int checkPackedDecimal​(byte[] byteArray,
                                             int offset,
                                             int precision,
                                             boolean ignoreHighNibbleForEvenPrecision,
                                             boolean canOverwriteHighNibbleForEvenPrecision)
        Checks the validity of a Packed Decimal, return code indicating the status of the Packed Decimal.
        Parameters:
        byteArray - the source container array
        offset - starting offset of the Packed Decimal
        precision - precision of the Packed Decimal. Maximum valid precision is 253
        ignoreHighNibbleForEvenPrecision - if true, ignore to check if the top nibble (first 4 bits) of the input is an invalid sign value in the case of even precision
        canOverwriteHighNibbleForEvenPrecision - if true, change the high nibble to a zero in case of even precision
        Returns:
        the condition code: 0 All digit codes and the sign valid 1 Sign invalid 2 At least one digit code invalid 3 Sign invalid and at least one digit code invalid
        Throws:
        NullPointerException - if byteArray is null
        ArrayIndexOutOfBoundsException - if an invalid array access occurs
      • checkPackedDecimal

        public static int checkPackedDecimal​(byte[] byteArray,
                                             int offset,
                                             int precision,
                                             boolean ignoreHighNibbleForEvenPrecision)
        Checks the validity of a Packed Decimal, return code indicating the status of the Packed Decimal. The most significant nibble cannot be overwritten.
        Parameters:
        byteArray - the source container array
        offset - starting offset of the Packed Decimal
        precision - precision of the Packed Decimal
        ignoreHighNibbleForEvenPrecision - if true, ignore the high nibble in the case of even precision
        Returns:
        the condition code: 0 All digit codes and the sign valid 1 Sign invalid 2 At least one digit code invalid 3 Sign invalid and at least one digit code invalid
        Throws:
        NullPointerException - if byteArray is null
        ArrayIndexOutOfBoundsException - if an invalid array access occurs
      • checkPackedDecimal

        public static int checkPackedDecimal​(byte[] byteArray,
                                             int offset,
                                             int precision)
        Checks the validity of a Packed Decimal, return code indicating the status of the Packed Decimal. Don't ignore the most significant nibble. The most significant nibble cannot be overwritten.
        Parameters:
        byteArray - the source container array
        offset - starting offset of the Packed Decimal
        precision - precision of the Packed Decimal
        Returns:
        the condition code: 0 All digit codes and the sign valid 1 Sign invalid 2 At least one digit code invalid 3 Sign invalid and at least one digit code invalid
        Throws:
        NullPointerException - if byteArray is null
        ArrayIndexOutOfBoundsException - if an invalid array access occurs
      • addPackedDecimal

        public static void addPackedDecimal​(byte[] result,
                                            int resultOffset,
                                            int resultPrecision,
                                            byte[] op1Decimal,
                                            int op1Offset,
                                            int op1Precision,
                                            byte[] op2Decimal,
                                            int op2Offset,
                                            int op2Precision,
                                            boolean checkOverflow)
                                     throws ArithmeticException
        Add two Packed Decimals in byte arrays. The sign of an input Packed Decimal is assumed to be positive unless the sign nibble contains one of the negative sign codes, in which case the sign of the respective input Packed Decimal is interpreted as negative.
        Parameters:
        result - byte array that will hold the sum of the two operand Packed Decimals
        resultOffset - offset into result where the sum Packed Decimal begins
        resultPrecision - number of Packed Decimal digits for the sum. Maximum valid precision is 253
        op1Decimal - byte array that holds the first operand Packed Decimal.
        op1Offset - offset into op1Decimal where the Packed Decimal. is located
        op1Precision - number of Packed Decimal digits for the first operand. Maximum valid precision is 253
        op2Decimal - byte array that holds the second operand Packed Decimal
        op2Offset - offset into op2Decimal where the Packed Decimal is located
        op2Precision - number of Packed Decimal digits for the second operand. Maximum valid precision is 253
        checkOverflow - check for overflow
        Throws:
        NullPointerException - if any of the byte arrays are null
        ArrayIndexOutOfBoundsException - if an invalid array access occurs
        ArithmeticException - if an overflow occurs during the computation of the sum
      • subtractPackedDecimal

        public static void subtractPackedDecimal​(byte[] result,
                                                 int resultOffset,
                                                 int resultPrecision,
                                                 byte[] op1Decimal,
                                                 int op1Offset,
                                                 int op1Precision,
                                                 byte[] op2Decimal,
                                                 int op2Offset,
                                                 int op2Precision,
                                                 boolean checkOverflow)
                                          throws ArithmeticException
        Subtracts two Packed Decimals in byte arrays. The sign of an input Packed Decimal is assumed to be positive unless the sign nibble contains one of the negative sign codes, in which case the sign of the respective input Packed Decimal is interpreted as negative.
        Parameters:
        result - byte array that will hold the difference of the two operand Packed Decimals
        resultOffset - offset into result where the result Packed Decimal is located
        resultPrecision - number of Packed Decimal digits for the result. Maximum valid precision is 253
        op1Decimal - byte array that holds the first Packed Decimal operand
        op1Offset - offset into op1Decimal where the first operand is located
        op1Precision - number of Packed Decimal digits for the first operand. Maximum valid precision is 253
        op2Decimal - byte array that holds the second Packed Decimal operand
        op2Offset - offset into op2Decimal where the second operand is located
        op2Precision - number of Packed Decimal digits for the second operand. Maximum valid precision is 253
        checkOverflow - check for overflow
        Throws:
        NullPointerException - if any of the byte arrays are null
        ArrayIndexOutOfBoundsException - if an invalid array access occurs
        ArithmeticException - if an overflow occurs during the computation of the difference
      • setPackedZero

        public static void setPackedZero​(byte[] byteArray,
                                         int offset,
                                         int len)
        Create a positive Packed Decimal representation of zero.
        Parameters:
        byteArray - byte array which will hold the packed zero
        offset - offset into toBytes where the packed zero begins
        len - length of the packed zero. Maximum valid length is 253
        Throws:
        NullPointerException - if toBytes is null
        ArrayIndexOutOfBoundsException - if an invalid array access occurs
      • multiplyPackedDecimal

        public static void multiplyPackedDecimal​(byte[] result,
                                                 int resultOffset,
                                                 int resultPrecision,
                                                 byte[] op1Decimal,
                                                 int op1Offset,
                                                 int op1Precision,
                                                 byte[] op2Decimal,
                                                 int op2Offset,
                                                 int op2Precision,
                                                 boolean checkOverflow)
        Multiplies two Packed Decimals in byte arrays. The sign of an input Packed Decimal is assumed to be positive unless the sign nibble contains one of the negative sign codes, in which case the sign of the respective input Packed Decimal is interpreted as negative.
        Parameters:
        result - byte array that will hold the product Packed Decimal
        resultOffset - offset into result where the product Packed Decimal is located
        resultPrecision - the length (number of digits) of the product Packed Decimal. Maximum valid precision is 253
        op1Decimal - byte array that will hold the multiplicand Packed Decimal
        op1Offset - offset into op1Decimal where the multiplicand is located
        op1Precision - the length (number of digits) of the multiplicand Packed Decimal. Maximum valid precision is 253
        op2Decimal - byte array that will hold the multiplier
        op2Offset - offset into op2Decimal where the multiplier is located
        op2Precision - the length (number of digits) of the multiplier Packed Decimal. Maximum valid precision is 253
        checkOverflow - if set to true, Packed Decimals are validated before multiplication and an IllegalArgumentException or ArithmeticException may be thrown. If not, the result can be invalid
        Throws:
        NullPointerException - if any of the byte arrays are null
        ArrayIndexOutOfBoundsException - if an invalid array access occurs
        IllegalArgumentException - if an overflow occurs during multiplication
        ArithmeticException - if any of the Packed Decimal operands are invalid
      • dividePackedDecimal

        public static void dividePackedDecimal​(byte[] result,
                                               int resultOffset,
                                               int resultPrecision,
                                               byte[] op1Decimal,
                                               int op1Offset,
                                               int op1Precision,
                                               byte[] op2Decimal,
                                               int op2Offset,
                                               int op2Precision,
                                               boolean checkOverflow)
        Divides two Packed Decimals is byte arrays. The sign of an input Packed Decimal is assumed to be positive unless the sign nibble contains one of the negative sign codes, in which case the sign of the respective input Packed Decimal is interpreted as negative.
        Parameters:
        result - byte array that will hold the quotient Packed Decimal
        resultOffset - offset into result where the quotient Packed Decimal is located
        resultPrecision - the length (number of digits) of the quotient Packed Decimal. Maximum valid precision is 253
        op1Decimal - byte array that will hold the dividend Packed Decimal
        op1Offset - offset into op1Decimal where the dividend is located
        op1Precision - the length (number of digits) of the dividend Packed Decimal. Maximum valid precision is 253
        op2Decimal - byte array that will hold the divisor
        op2Offset - offset into op2Decimal where the divisor is located
        op2Precision - the length (number of digits) of the divisor Packed Decimal. Maximum valid precision is 253
        checkOverflow - if set to true, Packed Decimals are validated before division and an IllegalArgumentException or ArithmeticException may be thrown. If not, the result can be invalid
        Throws:
        NullPointerException - if any of the byte arrays are null
        ArrayIndexOutOfBoundsException - if an invalid array access occurs
        IllegalArgumentException - if an overflow occurs during division
        ArithmeticException - if any of the Packed Decimal operands are invalid
      • remainderPackedDecimal

        public static void remainderPackedDecimal​(byte[] result,
                                                  int resultOffset,
                                                  int resultPrecision,
                                                  byte[] op1Decimal,
                                                  int op1Offset,
                                                  int op1Precision,
                                                  byte[] op2Decimal,
                                                  int op2Offset,
                                                  int op2Precision,
                                                  boolean checkOverflow)
        Calculates the remainder resulting from the division of two Packed Decimals in byte arrays. The sign of an input Packed Decimal is assumed to be positive unless the sign nibble contains one of the negative sign codes, in which case the sign of the respective input Packed Decimal is interpreted as negative.
        Parameters:
        result - byte array that will hold the remainder Packed Decimal
        resultOffset - offset into result where the remainder Packed Decimal is located
        resultPrecision - number of Packed Decimal digits for the remainder. Maximum valid precision is 253
        op1Decimal - byte array that will hold the dividend Packed Decimal
        op1Offset - offset into op1Decimal where the dividend is located
        op1Precision - number of Packed Decimal digits for the dividend. Maximum valid precision is 253
        op2Decimal - byte array that will hold the divisor
        op2Offset - offset into op2Decimal where the divisor is located
        op2Precision - number of Packed Decimal digits for the divisor. Maximum valid precision is 253
        checkOverflow - if set to true, Packed Decimals are validated before division and an IllegalArgumentException or ArithmeticException may be thrown. If not, the result can be invalid
        Throws:
        NullPointerException - if any of the byte arrays are null
        ArrayIndexOutOfBoundsException - if an invalid array access occurs
        IllegalArgumentException - if an overflow occurs during division
        ArithmeticException - if any of the Packed Decimal operands are invalid
      • lessThanPackedDecimal

        public static boolean lessThanPackedDecimal​(byte[] op1Decimal,
                                                    int op1Offset,
                                                    int op1Precision,
                                                    byte[] op2Decimal,
                                                    int op2Offset,
                                                    int op2Precision)
        Checks if the first Packed Decimal operand is lesser than the second one.
        Parameters:
        op1Decimal - byte array that holds the first Packed Decimal operand
        op1Offset - offset into op1Decimal where the first operand is located
        op1Precision - number of Packed Decimal digits for the first operand
        op2Decimal - byte array that holds the second Packed Decimal operand
        op2Offset - offset into op2Decimal where the second operand is located
        op2Precision - number of Packed Decimal digits for the second operand
        Returns:
        true if op1Decimal < op2Decimal, false otherwise
        Throws:
        NullPointerException - if any of the byte arrays are null
        ArrayIndexOutOfBoundsException - if an invalid array access occurs
      • lessThanOrEqualsPackedDecimal

        public static boolean lessThanOrEqualsPackedDecimal​(byte[] op1Decimal,
                                                            int op1Offset,
                                                            int op1Precision,
                                                            byte[] op2Decimal,
                                                            int op2Offset,
                                                            int op2Precision)
        Checks if the first Packed Decimal operand is less than or equal to the second one.
        Parameters:
        op1Decimal - byte array that holds the first Packed Decimal operand
        op1Offset - offset into op1Decimal where the first operand is located
        op1Precision - number of Packed Decimal digits for the first operand
        op2Decimal - byte array that holds the second Packed Decimal operand
        op2Offset - offset into op2Decimal where the second operand is located
        op2Precision - number of Packed Decimal digits for the second operand
        Returns:
        true if op1Decimal <= op2Decimal, false otherwise
        Throws:
        NullPointerException - if any of the byte arrays are null
        ArrayIndexOutOfBoundsException - if an invalid array access occurs
      • greaterThanPackedDecimal

        public static boolean greaterThanPackedDecimal​(byte[] op1Decimal,
                                                       int op1Offset,
                                                       int op1Precision,
                                                       byte[] op2Decimal,
                                                       int op2Offset,
                                                       int op2Precision)
        Checks if the first Packed Decimal operand is greater than the second one.
        Parameters:
        op1Decimal - byte array that holds the first Packed Decimal operand
        op1Offset - offset into op1Decimal where the first operand is located
        op1Precision - number of Packed Decimal digits for the first operand
        op2Decimal - byte array that holds the second Packed Decimal operand
        op2Offset - offset into op2Decimal where the second operand is located
        op2Precision - number of Packed Decimal digits for the second operand
        Returns:
        true if op1Decimal > op2Decimal, false otherwise
        Throws:
        NullPointerException - if any of the byte arrays are null
        ArrayIndexOutOfBoundsException - if an invalid array access occurs
      • greaterThanOrEqualsPackedDecimal

        public static boolean greaterThanOrEqualsPackedDecimal​(byte[] op1Decimal,
                                                               int op1Offset,
                                                               int op1Precision,
                                                               byte[] op2Decimal,
                                                               int op2Offset,
                                                               int op2Precision)
        Checks if the first Packed Decimal operand is greater than or equal to the second one.
        Parameters:
        op1Decimal - byte array that holds the first Packed Decimal operand
        op1Offset - offset into op1Decimal where the first operand is located
        op1Precision - number of Packed Decimal digits for the first operand
        op2Decimal - byte array that holds the second Packed Decimal operand
        op2Offset - offset into op2Decimal where the second operand is located
        op2Precision - number of Packed Decimal digits for the second operand
        Returns:
        true if op1Decimal >= op2Decimal, false otherwise
        Throws:
        NullPointerException - if any of the byte arrays are null
        ArrayIndexOutOfBoundsException - if an invalid array access occurs
      • equalsPackedDecimal

        public static boolean equalsPackedDecimal​(byte[] op1Decimal,
                                                  int op1Offset,
                                                  int op1Precision,
                                                  byte[] op2Decimal,
                                                  int op2Offset,
                                                  int op2Precision)
        Checks if the two Packed Decimal operands are equal.
        Parameters:
        op1Decimal - byte array that holds the first Packed Decimal operand
        op1Offset - offset into op1Decimal where the first operand is located
        op1Precision - number of Packed Decimal digits for the first operand
        op2Decimal - byte array that holds the second Packed Decimal operand
        op2Offset - offset into op2Decimal where the second operand is located
        op2Precision - number of Packed Decimal digits for the second operand
        Returns:
        true if op1Decimal == op2Decimal, false otherwise
        Throws:
        NullPointerException - if any of the byte arrays are null
        ArrayIndexOutOfBoundsException - if an invalid array access occurs
      • notEqualsPackedDecimal

        public static boolean notEqualsPackedDecimal​(byte[] op1Decimal,
                                                     int op1Offset,
                                                     int op1Precision,
                                                     byte[] op2Decimal,
                                                     int op2Offset,
                                                     int op2Precision)
        Checks if the two Packed Decimal operands are unequal.
        Parameters:
        op1Decimal - byte array that holds the first Packed Decimal operand
        op1Offset - offset into op1Decimal where the first operand is located
        op1Precision - number of Packed Decimal digits for the first operand
        op2Decimal - byte array that holds the second Packed Decimal operand
        op2Offset - offset into op2Decimal where the second operand is located
        op2Precision - number of Packed Decimal digits for the second operand
        Returns:
        true if op1Decimal != op2Decimal, false otherwise
        Throws:
        NullPointerException - if any of the byte arrays are null
        ArrayIndexOutOfBoundsException - if an invalid array access occurs
      • shiftRightPackedDecimal

        public static void shiftRightPackedDecimal​(byte[] destination,
                                                   int destinationOffset,
                                                   int destinationPrecision,
                                                   byte[] source,
                                                   int sourceOffset,
                                                   int sourcePrecision,
                                                   int shiftAmount,
                                                   boolean round,
                                                   boolean checkOverflow)
        Right shift, and optionally round, a Packed Decimal. If the resultant is zero, it can either be a positive zero 0x0C or a negative zero 0x0D.
        Parameters:
        destination - byte array that holds the result of the right shift (and rounding)
        destinationOffset - offset into destination where the result Packed Decimal is located
        destinationPrecision - number of Packed Decimal digits in the destination
        source - byte array that holds the Packed Decimal operand to be right shifted
        sourceOffset - offset into source where the operand is located
        sourcePrecision - number of Packed Decimal digits in the operand
        shiftAmount - amount by which the source will be right shifted
        round - if set to true, causes rounding to occur
        checkOverflow - if set to true, check for overflow
        Throws:
        NullPointerException - if any of the byte arrays are null
        ArrayIndexOutOfBoundsException - if an invalid array access occurs
        ArithmeticException - if a decimal overflow occurs
        IllegalArgumentException - if the shiftAmount or sourcePrecision parameter is invalid or the source packed decimal contains invalid sign or digit code
      • shiftLeftPackedDecimal

        public static void shiftLeftPackedDecimal​(byte[] destination,
                                                  int destinationOffset,
                                                  int destinationPrecision,
                                                  byte[] source,
                                                  int sourceOffset,
                                                  int sourcePrecision,
                                                  int shiftAmount,
                                                  boolean checkOverflow)
        Left shift a Packed Decimal appending zeros to the right. In the absence of overflow, the sign of a zero can either be positive 0x0C or negative 0x0D.
        Parameters:
        destination - byte array that holds the result of the left shift
        destinationOffset - offset into destination where the result Packed Decimal is located
        destinationPrecision - number of Packed Decimal digits in the destination
        source - byte array that holds the Packed Decimal operand to be left shifted
        sourceOffset - offset into source where the operand is located
        sourcePrecision - number of Packed Decimal digits in the operand
        shiftAmount - amount by which the source will be left shifted
        checkOverflow - if set to true, check for overflow
        Throws:
        NullPointerException - if any of the byte arrays are null
        ArrayIndexOutOfBoundsException - if an invalid array access occurs
        ArithmeticException - if a decimal overflow occurs
        IllegalArgumentException - if the shiftAmount or sourcePrecision parameter is invalid or the source packed decimal contains invalid sign or digit code
      • movePackedDecimal

        public static void movePackedDecimal​(byte[] destination,
                                             int destinationOffset,
                                             int destinationPrecision,
                                             byte[] source,
                                             int sourceOffset,
                                             int sourcePrecision,
                                             boolean checkOverflow)
        Creates a copy of a Packed Decimal.
        Parameters:
        destination - byte array representing the destination
        destinationOffset - offset into destination destination where the Packed Decimal is located
        destinationPrecision - number of Packed Decimal digits for the destination
        source - byte array which holds the source Packed Decimal
        sourceOffset - offset into source where the Packed Decimal is located
        sourcePrecision - number of Packed Decimal digits for the source. Maximum valid precision is 253
        checkOverflow - if set to true, moved Packed Decimals are validated, and an IllegalArgumentException or ArithmeticException is thrown if non-zero nibbles are truncated during the move operation. If set to false, no validating is conducted.
        Throws:
        NullPointerException - if any of the byte arrays are null
        ArrayIndexOutOfBoundsException - if an invalid array access occurs
        IllegalArgumentException - if the source Packed Decimal is invalid
        ArithmeticException - if a decimal overflow occurs