public class ScaledDecimal extends Object implements Comparable<ScaledDecimal>, Serializable
The scalable range of the exponent is constrained to [-63, 63], and violations of these bounds will throw exceptions where applicable.
Modifier and Type | Field and Description |
---|---|
static int |
MAX_LEN
The maximum allowable length for a scaled decimal representation.
|
Constructor and Description |
---|
ScaledDecimal()
Default constructor for the ScaledDecimal class.
|
ScaledDecimal(BigDecimal bigDecimal)
Constructs a ScaledDecimal instance from the specified BigDecimal object.
|
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.
|
Modifier and Type | Method and 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(BigDecimal bigDecimal)
Sets the value of this ScaledDecimal from the specified
BigDecimal . |
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 |
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() |
public static final int MAX_LEN
public ScaledDecimal(long mantissa, int exponent)
mantissa
- the mantissa part of the scaled decimalexponent
- the exponent part of the scaled decimalIllegalArgumentException
- if (exponent < -63)
or (exponent > 63)
public ScaledDecimal(String number)
number
- the source string representing the scaled decimalNullPointerException
- if number
is null
NumberFormatException
- if the string format of number
is invalidpublic ScaledDecimal(BigDecimal bigDecimal)
bigDecimal
- the source BigDecimal value to initialize the ScaledDecimalNullPointerException
- if (bigDecimal == null)
IllegalArgumentException
- if the mantissa of bigDecimal
is not within
the valid range between MIN_MANTISSA
and
MAX_MANTISSA
public ScaledDecimal(byte[] buffer, int offset, int length)
buffer
- the source byte array containing the representation of the scaled decimaloffset
- the starting offset in the byte arraylength
- the length of the value in the byte arrayNullPointerException
- 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 failspublic ScaledDecimal()
Initializes a ScaledDecimal instance with default values.
public final void set(BigDecimal bigDecimal)
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.
bigDecimal
- the BigDecimal
value to be set in this ScaledDecimalNullPointerException
- if bigDecimal
is null
IllegalArgumentException
- if the mantissa of bigDecimal
is not within
the valid range between MIN_MANTISSA
and MAX_MANTISSA
public final void set(String number)
number
- the string representation of the scaled decimalNullPointerException
- if number
is null
public final void set(byte[] buffer, int offset, int length)
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.
buffer
- the byte array containing the numeric representationoffset
- the offset within the byte array to start parsinglength
- the number of bytes to read from the byte arrayNullPointerException
- 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 formattedIllegalArgumentException
- if the parsed value exceeds the range of allowed valuespublic final int getExponent()
public final long getMantissa()
public final void setMantissa(long mantissa)
mantissa
- the mantissa value to setpublic final void setExponent(int exponent)
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.
exponent
- the exponent value to setIllegalArgumentException
- if exponent < -63
or exponent > 63
public int compareTo(ScaledDecimal o)
compareTo
in interface Comparable<ScaledDecimal>
o
- the ScaledDecimal to be compared with this ScaledDecimalNullPointerException
- if the specified ScaledDecimal is nullpublic final BigDecimal toBigDecimal()
BigDecimal
.BigDecimal
representation of the current ScaledDecimal,
constructed using the mantissa and exponent values.public final int toByteArray(byte[] buffer, int offset)
buffer
- the target byte array where the scaled decimal representation will be writtenoffset
- the starting offset in the byte array for writing the scaled decimalNullPointerException
- if buffer
is null
IllegalArgumentException
- if offset < 0
or offset >= buffer.length
IndexOutOfBoundsException
- if writing, the scaled decimal exceeds the bounds of the bufferpublic final void normalize()
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.
public final void normalize(int newExponent)
If the adjustment to the new exponent cannot be performed without data loss
(i.e., mantissa would need rounding), an IllegalArgumentException
is thrown.
newExponent
- the target exponent to which the scaled decimal should be normalizedIllegalArgumentException
- if the required adjustment to the mantissa for conversion
to the specified exponent leads to data lossCopyright © 2005–2025 Onix Solutions. All rights reserved.