public interface ScaleMetrics
Decimal
number. It is mainly
used internally from code implementing the arithmetic operations of a Decimal
.
The scale
determines the number of fraction digits of the Decimal
. The
scale factor
is the multiplier/divisor for conversions between the Decimal
value
and the unscaled long
value underlying every Decimal
.
Operations such as multiplyByScaleFactor(..)
are defined here as separate
methods to allow for compiler optimizations. Multiplications and divisions are for instance translated into shifts
and adds by the compiler instead of the more expensive multiplication and division operations with non-constant long
values.
ScaleMetrics
also provides access to DecimalArithmetic
instances for different rounding modes and
overflow policies. DecimalArithmetic
objects can be used to deal with Decimal
numbers in their
primitive form, meaning that Decimal
numbers are passed to the arithmetic class as unscaled
long
values.
Modifier and Type | Method and Description |
---|---|
long |
divideByScaleFactor(long dividend)
Returns
dividend/scaleFactor . |
long |
divideUnsignedByScaleFactor(long unsignedDividend)
Returns
unsignedDividend/scaleFactor using unsigned division. |
DecimalArithmetic |
getArithmetic(RoundingMode roundingMode)
Returns the arithmetic for this scale that performs all operations with the specified
roundingMode and
without overflow checks. |
DecimalArithmetic |
getArithmetic(TruncationPolicy truncationPolicy)
Returns the arithmetic for this scale that performs all operations with the specified
truncationPolicy . |
DecimalArithmetic |
getCheckedArithmetic(RoundingMode roundingMode)
Returns the arithmetic for this scale that performs all operations with the specified
roundingMode and
with overflow checks. |
DecimalArithmetic |
getDefaultArithmetic()
Returns the default arithmetic for this scale performing unchecked operations with rounding mode
HALF_UP . |
DecimalArithmetic |
getDefaultCheckedArithmetic()
Returns the default arithmetic for this scale performing checked operations with rounding mode
HALF_UP . |
long |
getMaxIntegerValue()
Returns the largest integer value that can be represented using this scale.
|
long |
getMinIntegerValue()
Returns the smallest integer value that can be represented using this scale.
|
DecimalArithmetic |
getRoundingDownArithmetic()
Returns the arithmetic for this scale performing unchecked operations with rounding mode
DOWN . |
DecimalArithmetic |
getRoundingFloorArithmetic()
Returns the arithmetic for this scale performing unchecked operations with rounding mode
FLOOR . |
DecimalArithmetic |
getRoundingHalfEvenArithmetic()
Returns the arithmetic for this scale performing unchecked operations with rounding mode
HALF_EVEN . |
DecimalArithmetic |
getRoundingUnnecessaryArithmetic()
Returns the arithmetic for this scale performing unchecked operations with rounding mode
UNNECESSARY . |
int |
getScale()
Returns the scale, the number of fraction digits to the right of the decimal point of a
Decimal value. |
long |
getScaleFactor()
Returns the scale factor, which is 10f where
f stands for the scale . |
BigDecimal |
getScaleFactorAsBigDecimal()
Returns the
scale factor as a BigDecimal value with scale zero. |
BigInteger |
getScaleFactorAsBigInteger()
Returns the
scale factor as a BigInteger value. |
int |
getScaleFactorNumberOfLeadingZeros()
Returns the number of leading zeros of the scale factor
|
boolean |
isValidIntegerValue(long value)
Returns true if the specified integer
value can be represented using this scale. |
long |
moduloByScaleFactor(long dividend)
Returns
dividend % scaleFactor also known as reminder. |
long |
mulhiByScaleFactor(int factor)
Returns
factor*high32(scaleFactor) where high32 refers to the high 32 bits of the factor. |
long |
mulloByScaleFactor(int factor)
Returns
factor*low32(scaleFactor) where low32 refers to the low 32 bits of the factor. |
long |
multiplyByScaleFactor(long factor)
Returns
factor*scaleFactor . |
long |
multiplyByScaleFactorExact(long factor)
Returns
factor*scaleFactor , checking for lost information. |
String |
toString(long value)
Returns the string representation of the specified
value applying this metric's scale. |
int getScale()
Decimal
value.long getScaleFactor()
f
stands for the scale
.BigInteger getScaleFactorAsBigInteger()
scale factor
as a BigInteger
value.BigDecimal getScaleFactorAsBigDecimal()
scale factor
as a BigDecimal
value with scale zero.int getScaleFactorNumberOfLeadingZeros()
Long.numberOfLeadingZeros(long)
applied to the scale factorlong getMaxIntegerValue()
Long.MAX_VALUE / scaleFactor
long getMinIntegerValue()
Long.MIN_VALUE / scaleFactor
boolean isValidIntegerValue(long value)
value
can be represented using this scale.value
- the value to test(Long.MIN_VALUE / scaleFactor) <= value <= (Long.MAX_VALUE / scaleFactor)
long multiplyByScaleFactor(long factor)
factor*scaleFactor
.factor
- the factorfactor*scaleFactor
long multiplyByScaleFactorExact(long factor)
factor*scaleFactor
, checking for lost information. If the result is out of the range of the
long
type, then an ArithmeticException
is thrown.factor
- the factorfactor*scaleFactor
ArithmeticException
- if an overflow occurslong mulloByScaleFactor(int factor)
factor*low32(scaleFactor)
where low32 refers to the low 32 bits of the factor.factor
- the factorfactor*low32(scaleFactor)
long mulhiByScaleFactor(int factor)
factor*high32(scaleFactor)
where high32 refers to the high 32 bits of the factor.factor
- the factorfactor*high32(scaleFactor)
long divideByScaleFactor(long dividend)
dividend/scaleFactor
.dividend
- the dividenddividend/scaleFactor
long divideUnsignedByScaleFactor(long unsignedDividend)
unsignedDividend/scaleFactor
using unsigned division.unsignedDividend
- the unsigned dividendunsignedDividend/scaleFactor
long moduloByScaleFactor(long dividend)
dividend % scaleFactor
also known as reminder.dividend
- the dividenddividend % scaleFactor
String toString(long value)
value
applying this metric's scale.value
- the unscaled decimal to convert to a stringDecimalArithmetic.toString(long)
DecimalArithmetic getDefaultArithmetic()
HALF_UP
.DecimalArithmetic getDefaultCheckedArithmetic()
HALF_UP
.DecimalArithmetic getRoundingDownArithmetic()
DOWN
.DecimalArithmetic getRoundingFloorArithmetic()
FLOOR
.DecimalArithmetic getRoundingHalfEvenArithmetic()
HALF_EVEN
.DecimalArithmetic getRoundingUnnecessaryArithmetic()
UNNECESSARY
.DecimalArithmetic getArithmetic(RoundingMode roundingMode)
roundingMode
and
without overflow checks.roundingMode
- the rounding mode used by the returned arithmeticDecimalArithmetic getCheckedArithmetic(RoundingMode roundingMode)
roundingMode
and
with overflow checks.roundingMode
- the rounding mode used by the returned arithmeticDecimalArithmetic getArithmetic(TruncationPolicy truncationPolicy)
truncationPolicy
.truncationPolicy
- the truncation policy used by the returned arithmetic