Class ScaledDecimal
- java.lang.Object
-
- biz.onixs.util.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 intMAX_LENThe 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 intcompareTo(ScaledDecimal o)Compares this ScaledDecimal object with the specified ScaledDecimal for order.booleanequals(Object obj)intgetExponent()Retrieves the exponent part of the scaled decimal.longgetMantissa()Retrieves the mantissa part of the scaled decimal.inthashCode()voidnormalize()Normalizes the mantissa and adjusts the exponent accordingly.voidnormalize(int newExponent)Normalizes the scaled decimal's mantissa to adjust the representation to a specified target exponent.voidset(byte[] buffer, int offset, int length)Sets the value of this ScaledDecimal using a byte array representation.voidset(String number)Sets the value of this ScaledDecimal using a string representation.voidset(BigDecimal bigDecimal)Sets the value of this ScaledDecimal from the specifiedBigDecimal.voidsetExponent(int exponent)Sets the exponent value for the ScaledDecimal.voidsetMantissa(long mantissa)Sets the mantissa part of the scaled decimal.BigDecimaltoBigDecimal()Converts the current ScaledDecimal instance to aBigDecimal.inttoByteArray(byte[] buffer, int offset)Encodes the current ScaledDecimal instance into a byte array representation and stores it in the provided buffer.StringtoString()
-
-
-
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 decimalexponent- 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- ifnumberisnullNumberFormatException- if the string format ofnumberis 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 ofbigDecimalis not within the valid range betweenMIN_MANTISSAandMAX_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 decimaloffset- the starting offset in the byte arraylength- the length of the value in the byte array- Throws:
NullPointerException- ifbufferisnullIllegalArgumentException- ifoffset < 0,length < 1, or ifoffset + length > buffer.lengthNumberFormatException- 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 specifiedBigDecimal.This method validates the
BigDecimalvalue to ensure that its mantissa is within the range defined byMIN_MANTISSAandMAX_MANTISSA. If theBigDecimalis invalid or null, appropriate exceptions are thrown.- Parameters:
bigDecimal- theBigDecimalvalue to be set in this ScaledDecimal- Throws:
NullPointerException- ifbigDecimalisnullIllegalArgumentException- if the mantissa ofbigDecimalis not within the valid range betweenMIN_MANTISSAandMAX_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- ifnumberisnull
-
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 representationoffset- the offset within the byte array to start parsinglength- the number of bytes to read from the byte array- Throws:
NullPointerException- ifbufferisnullIllegalArgumentException- ifoffset < 0,length < 1, oroffset + length > buffer.lengthNumberFormatException- 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 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
IllegalArgumentExceptionis thrown.- Parameters:
exponent- the exponent value to set- Throws:
IllegalArgumentException- ifexponent < -63orexponent > 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:
compareToin interfaceComparable<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
-
toBigDecimal
public final BigDecimal toBigDecimal()
Converts the current ScaledDecimal instance to aBigDecimal.- Returns:
- a
BigDecimalrepresentation 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 writtenoffset- 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- ifbufferisnullIllegalArgumentException- ifoffset < 0oroffset >= buffer.lengthIndexOutOfBoundsException- 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
IllegalArgumentExceptionis 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
-
-