Class ScaledDecimal

  • All Implemented Interfaces:
    Serializable, Comparable<ScaledDecimal>

    public class ScaledDecimal
    extends Object
    implements Comparable<ScaledDecimal>, Serializable
    Represents a scaled decimal number, characterized by a mantissa and an exponent. This class provides methods to manipulate and convert the representation of scaled decimals. It supports initialization from various sources such as strings, BigDecimal, and byte arrays. Additionally, it offers normalization and conversion utilities.

    The scalable range of the exponent is constrained to [-63, 63], and violations of these bounds will throw exceptions where applicable.

    See Also:
    Serialized Form
    • Field Summary

      Fields 
      Modifier and Type Field Description
      static int MAX_LEN
      The maximum allowable length for a scaled decimal representation.
    • Constructor Summary

      Constructors 
      Constructor Description
      ScaledDecimal()
      Default constructor for the ScaledDecimal class.
      ScaledDecimal​(byte[] buffer, int offset, int length)
      Constructs a ScaledDecimal instance using a source byte array representation.
      ScaledDecimal​(long mantissa, int exponent)
      Constructs a ScaledDecimal with the specified mantissa and exponent values.
      ScaledDecimal​(String number)
      Constructs a ScaledDecimal instance from a string representation.
      ScaledDecimal​(BigDecimal bigDecimal)
      Constructs a ScaledDecimal instance from the specified BigDecimal object.
    • Method Summary

      All Methods Instance Methods Concrete Methods 
      Modifier and Type Method Description
      int compareTo​(ScaledDecimal o)
      Compares this ScaledDecimal object with the specified ScaledDecimal for order.
      boolean equals​(Object obj)  
      int getExponent()
      Retrieves the exponent part of the scaled decimal.
      long getMantissa()
      Retrieves the mantissa part of the scaled decimal.
      int hashCode()  
      void normalize()
      Normalizes the mantissa and adjusts the exponent accordingly.
      void normalize​(int newExponent)
      Normalizes the scaled decimal's mantissa to adjust the representation to a specified target exponent.
      void set​(byte[] buffer, int offset, int length)
      Sets the value of this ScaledDecimal using a byte array representation.
      void set​(String number)
      Sets the value of this ScaledDecimal using a string representation.
      void set​(BigDecimal bigDecimal)
      Sets the value of this ScaledDecimal from the specified BigDecimal.
      void setExponent​(int exponent)
      Sets the exponent value for the ScaledDecimal.
      void setMantissa​(long mantissa)
      Sets the mantissa part of the scaled decimal.
      BigDecimal toBigDecimal()
      Converts the current ScaledDecimal instance to a BigDecimal.
      int toByteArray​(byte[] buffer, int offset)
      Encodes the current ScaledDecimal instance into a byte array representation and stores it in the provided buffer.
      String toString()  
    • Field Detail

      • MAX_LEN

        public static final int MAX_LEN
        The maximum allowable length for a scaled decimal representation. This value is calculated as the sum of the following: - The minimum representable value (1). - The maximum allowable exponent (20). - The bit length of the mantissa (63).
        See Also:
        Constant Field Values
    • Constructor Detail

      • ScaledDecimal

        public ScaledDecimal​(long mantissa,
                             int exponent)
        Constructs a ScaledDecimal with the specified mantissa and exponent values.
        Parameters:
        mantissa - the mantissa part of the scaled decimal
        exponent - the exponent part of the scaled decimal
        Throws:
        IllegalArgumentException - if (exponent < -63) or (exponent > 63)
      • ScaledDecimal

        public ScaledDecimal​(String number)
        Constructs a ScaledDecimal instance from a string representation.
        Parameters:
        number - the source string representing the scaled decimal
        Throws:
        NullPointerException - if number is null
        NumberFormatException - if the string format of number is invalid
      • ScaledDecimal

        public ScaledDecimal​(BigDecimal bigDecimal)
        Constructs a ScaledDecimal instance from the specified BigDecimal object.
        Parameters:
        bigDecimal - the source BigDecimal value to initialize the ScaledDecimal
        Throws:
        NullPointerException - if (bigDecimal == null)
        IllegalArgumentException - if the mantissa of bigDecimal is not within the valid range between MIN_MANTISSA and MAX_MANTISSA
      • ScaledDecimal

        public ScaledDecimal​(byte[] buffer,
                             int offset,
                             int length)
        Constructs a ScaledDecimal instance using a source byte array representation.
        Parameters:
        buffer - the source byte array containing the representation of the scaled decimal
        offset - the starting offset in the byte array
        length - the length of the value in the byte array
        Throws:
        NullPointerException - if buffer is null
        IllegalArgumentException - if offset < 0, length < 1, or if offset + length > buffer.length
        NumberFormatException - if there are invalid characters in the byte array or if parsing the value fails
      • ScaledDecimal

        public ScaledDecimal()
        Default constructor for the ScaledDecimal class.

        Initializes a ScaledDecimal instance with default values.

    • Method Detail

      • set

        public final void set​(BigDecimal bigDecimal)
        Sets the value of this ScaledDecimal from the specified BigDecimal.

        This method validates the BigDecimal value to ensure that its mantissa is within the range defined by MIN_MANTISSA and MAX_MANTISSA. If the BigDecimal is invalid or null, appropriate exceptions are thrown.

        Parameters:
        bigDecimal - the BigDecimal value to be set in this ScaledDecimal
        Throws:
        NullPointerException - if bigDecimal is null
        IllegalArgumentException - if the mantissa of bigDecimal is not within the valid range between MIN_MANTISSA and MAX_MANTISSA
      • set

        public final void set​(String number)
        Sets the value of this ScaledDecimal using a string representation.
        Parameters:
        number - the string representation of the scaled decimal
        Throws:
        NullPointerException - if number is null
      • set

        public final void set​(byte[] buffer,
                              int offset,
                              int length)
        Sets the value of this ScaledDecimal using a byte array representation.

        The method takes a byte array, a starting offset, and a length and parses the byte array to extract the mantissa and exponent for the ScaledDecimal. The byte array must represent a valid numeric value in string form, and it should not exceed the range of allowed values for mantissa.

        Parameters:
        buffer - the byte array containing the numeric representation
        offset - the offset within the byte array to start parsing
        length - the number of bytes to read from the byte array
        Throws:
        NullPointerException - if buffer is null
        IllegalArgumentException - if offset < 0, length < 1, or offset + length > buffer.length
        NumberFormatException - if there are invalid characters in the byte array or if the numeric value is not properly formatted
        IllegalArgumentException - if the parsed value exceeds the range of allowed values
      • getExponent

        public final int getExponent()
        Retrieves the exponent part of the scaled decimal.
        Returns:
        the exponent of the scaled decimal
      • getMantissa

        public final long getMantissa()
        Retrieves the mantissa part of the scaled decimal.
        Returns:
        the mantissa of the scaled decimal
      • setMantissa

        public final void setMantissa​(long mantissa)
        Sets the mantissa part of the scaled decimal.
        Parameters:
        mantissa - the mantissa value to set
      • setExponent

        public final void setExponent​(int exponent)
        Sets the exponent value for the ScaledDecimal.

        The exponent determines the scale of the decimal value and must be within the range of -63 to 63, inclusive. If the provided value is outside this range, an IllegalArgumentException is thrown.

        Parameters:
        exponent - the exponent value to set
        Throws:
        IllegalArgumentException - if exponent < -63 or exponent > 63
      • compareTo

        public int compareTo​(ScaledDecimal o)
        Compares this ScaledDecimal object with the specified ScaledDecimal for order. Returns a negative integer, zero, or a positive integer if this ScaledDecimal is less than, equal to, or greater than the specified ScaledDecimal, respectively.
        Specified by:
        compareTo in interface Comparable<ScaledDecimal>
        Parameters:
        o - the ScaledDecimal to be compared with this ScaledDecimal
        Returns:
        a negative integer, zero, or a positive integer depending on whether this ScaledDecimal is less than, equal to, or greater than the specified ScaledDecimal
        Throws:
        NullPointerException - if the specified ScaledDecimal is null
      • hashCode

        public int hashCode()
        Overrides:
        hashCode in class Object
      • toBigDecimal

        public final BigDecimal toBigDecimal()
        Converts the current ScaledDecimal instance to a BigDecimal.
        Returns:
        a BigDecimal representation of the current ScaledDecimal, constructed using the mantissa and exponent values.
      • toByteArray

        public final int toByteArray​(byte[] buffer,
                                     int offset)
        Encodes the current ScaledDecimal instance into a byte array representation and stores it in the provided buffer. The method writes the numeric value (including sign, mantissa, and decimal point, if applicable) starting at the specified offset in the buffer.
        Parameters:
        buffer - the target byte array where the scaled decimal representation will be written
        offset - the starting offset in the byte array for writing the scaled decimal
        Returns:
        the number of bytes written into the buffer representing the scaled decimal
        Throws:
        NullPointerException - if buffer is null
        IllegalArgumentException - if offset < 0 or offset >= buffer.length
        IndexOutOfBoundsException - if writing, the scaled decimal exceeds the bounds of the buffer
      • normalize

        public final void normalize()
        Normalizes the mantissa and adjusts the exponent accordingly.

        This method reduces the mantissa by removing trailing zeroes while incrementing the exponent by the same amount. The normalization process is only carried out if the mantissa is nonzero.

      • normalize

        public final void normalize​(int newExponent)
        Normalizes the scaled decimal's mantissa to adjust the representation to a specified target exponent. This method modifies the mantissa and/or exponent by appropriately scaling the mantissa value to match the new exponent without losing precision.

        If the adjustment to the new exponent cannot be performed without data loss (i.e., mantissa would need rounding), an IllegalArgumentException is thrown.

        Parameters:
        newExponent - the target exponent to which the scaled decimal should be normalized
        Throws:
        IllegalArgumentException - if the required adjustment to the mantissa for conversion to the specified exponent leads to data loss