- java.lang.Object
-
- java.lang.Number
-
- life.expert.value.numeric.utils.NumberValue
-
- life.expert.value.numeric.utils.DefaultNumberValue
-
- All Implemented Interfaces:
java.io.Serializable,java.lang.Comparable<NumberValue>
public final class DefaultNumberValue extends NumberValue
Default implementation ofNumberValuebased onBigDecimal.- See Also:
- Serialized Form
-
-
Field Summary
Fields Modifier and Type Field Description static NumberValueONEThe value 1, with a scale of 0.
Backed byBigDecimal.ONE
-
Constructor Summary
Constructors Constructor Description DefaultNumberValue(java.lang.Number number)Instantiates a new Default number value.
-
Method Summary
Modifier and Type Method Description doubledoubleValue()doubledoubleValueExact()Access the numeric value asdouble.floatfloatValue()longgetAmountFractionDenominator()This method allows to extract the denominator part of the current fraction, hereby givenlonggetAmountFractionNumerator()This method allows to extract the numerator part of the current fraction, hereby givenprotected static java.math.BigDecimalgetBigDecimal(java.lang.Number num)Deprecated.will be removed.java.lang.Class<?>getNumberType()Get the numeric implementation type, that is the base of this number.intgetPrecision()Returns the precision of thisQuantity.intgetScale()Returns the scale of thisQuantity.intintValue()intintValueExact()Access the numeric value asint.longlongValue()longlongValueExact()Access the numeric value aslong.<T extends java.lang.Number>
TnumberValue(java.lang.Class<T> numberType)Access the numeric value asNumber.<T extends java.lang.Number>
TnumberValueExact(java.lang.Class<T> numberType)Access the numeric value asNumber.static NumberValueof(java.lang.Number number)Creates a new instance ofNumberValue, using the given number.NumberValueround(java.math.MathContext mathContext)Access the current NumberValue rounded using the givenMathContext.java.lang.StringtoString()-
Methods inherited from class life.expert.value.numeric.utils.NumberValue
compareTo
-
-
-
-
Field Detail
-
ONE
public static final NumberValue ONE
The value 1, with a scale of 0.
Backed byBigDecimal.ONE- Since:
- 0.8
-
-
Method Detail
-
of
public static NumberValue of(java.lang.Number number)
Creates a new instance ofNumberValue, using the given number.- Parameters:
number- The numeric part, not null.- Returns:
- A new instance of
NumberValue.
-
getNumberType
public java.lang.Class<?> getNumberType()
Description copied from class:NumberValueGet the numeric implementation type, that is the base of this number.- Specified by:
getNumberTypein classNumberValue- Returns:
- the numeric implementation type, not
null.
-
getPrecision
public int getPrecision()
Description copied from class:NumberValueReturns the precision of thisQuantity. (The precision is the number of digits in the unscaled value.) The precision of a zero value is 1.- Specified by:
getPrecisionin classNumberValue- Returns:
- the precision of this
Quantity.
-
getScale
public int getScale()
Description copied from class:NumberValueReturns the scale of thisQuantity. If zero or positive, the scale is the number of digits to the right of the decimal point. If negative, the unscaled value of the number is multiplied by ten to the power of the negation of the scale. For example, a scale of-3means the unscaled value is multiplied by 1000.- Specified by:
getScalein classNumberValue- Returns:
- the scale of this
Quantity.
-
intValue
public int intValue()
- Specified by:
intValuein classjava.lang.Number
-
intValueExact
public int intValueExact()
Description copied from class:NumberValueAccess the numeric value asint. Hereby no truncation will be performed to fit the value into the target data type.- Specified by:
intValueExactin classNumberValue- Returns:
- the (possibly) truncated value of the
Quantity.
-
longValue
public long longValue()
- Specified by:
longValuein classjava.lang.Number
-
longValueExact
public long longValueExact()
Description copied from class:NumberValueAccess the numeric value aslong. Hereby no truncation will be performed to fit the value into the target data type.- Specified by:
longValueExactin classNumberValue- Returns:
- the (possibly) truncated value of the
Quantity.
-
floatValue
public float floatValue()
- Specified by:
floatValuein classjava.lang.Number
-
doubleValue
public double doubleValue()
- Specified by:
doubleValuein classjava.lang.Number
-
doubleValueExact
public double doubleValueExact()
Description copied from class:NumberValueAccess the numeric value asdouble. Hereby no truncation will be performed to fit the value into the target data type.- Specified by:
doubleValueExactin classNumberValue- Returns:
- the (possibly) truncated value of the
Quantity.
-
getAmountFractionNumerator
public long getAmountFractionNumerator()
Description copied from class:NumberValueThis method allows to extract the numerator part of the current fraction, hereby given
the following must be always true:* w = longValue() n = getFractionNominator() d = getFractionDenominator()** !(w<0 && n>0) and !(w>0 && n<0) and d>0 and |n| < d // || = absolute value- Specified by:
getAmountFractionNumeratorin classNumberValue- Returns:
- the amount's fraction numerator..
-
getAmountFractionDenominator
public long getAmountFractionDenominator()
Description copied from class:NumberValueThis method allows to extract the denominator part of the current fraction, hereby given
the following must be always true:w = longValue() n = getFractionNominator() d = getFractionDenominator()*
.!(w<0 && n>0) and !(w>0 && n<0) and d>0 and |n| < d // || = absolute value*- Specified by:
getAmountFractionDenominatorin classNumberValue- Returns:
- the amount's fraction denominator.
-
numberValue
public <T extends java.lang.Number> T numberValue(java.lang.Class<T> numberType)
Description copied from class:NumberValueAccess the numeric value asNumber. Hereby truncation may be performed as needed to fit the value into the target data type.- Specified by:
numberValuein classNumberValue- Type Parameters:
T- the type parameter- Parameters:
numberType- The concrete number class to be returned. Basically the following Number types, must be supported if available on the corresponding runtime platform:java.lang.Longjava.lang.Doublejava.lang.Numberjava.math.BigInteger, currently not available on all platforms.java.math.BigDecimal, currently not available on all platforms.
- Returns:
- the (possibly) truncated value of the
Quantity.
-
round
public NumberValue round(java.math.MathContext mathContext)
Description copied from class:NumberValueAccess the current NumberValue rounded using the givenMathContext.- Specified by:
roundin classNumberValue- Parameters:
mathContext- theMathContextto be applied.- Returns:
- the new NumberValue, never null.
- See Also:
java.math.BigDecimal#round(java.math.MathContext)java.math.BigDecimal#round(java.math.MathContext)
-
numberValueExact
public <T extends java.lang.Number> T numberValueExact(java.lang.Class<T> numberType)
Description copied from class:NumberValueAccess the numeric value asNumber. Hereby no truncation will be performed to fit the value into the target data type.- Specified by:
numberValueExactin classNumberValue- Type Parameters:
T- the type parameter- Parameters:
numberType- The concrete number class to be returned. Basically the following Number types, must be supported if available on the corresponding runtime platform:java.lang.Longjava.lang.Doublejava.lang.Numberjava.math.BigInteger, currently not available on all platforms.java.math.BigDecimal, currently not available on all platforms.
- Returns:
- the (possibly) truncated value of the
Quantity.
-
getBigDecimal
@Deprecated protected static java.math.BigDecimal getBigDecimal(java.lang.Number num)
Deprecated.will be removed.Creates aBigDecimalfrom the givenNumberdoing the valid conversion depending the type given.- Parameters:
num- the number type- Returns:
- the corresponding
BigDecimal
-
toString
public java.lang.String toString()
- Overrides:
toStringin classjava.lang.Object
-
-