Package me.datafox.dfxengine.math.api
Interface Numeral
- All Superinterfaces:
Comparable<Numeral>
- All Known Implementing Classes:
BigDecNumeral,BigIntNumeral,DoubleNumeral,FloatNumeral,IntNumeral,LongNumeral,MappingOperationChain.SpecialNumeral
A numeric value that can be backed by various
Number types. Specifically, the allowed types are
Integer, Long, BigInteger, Float, Double BigDecimal. An
implementation of numeral should be immutable. It is called numeral to prevent name collisions with Number.-
Method Summary
Modifier and TypeMethodDescriptionbooleancanConvert(NumeralType type) convert(NumeralType type) convertIfAllowed(NumeralType type) doublefloatgetType()intintValue()longMethods inherited from interface java.lang.Comparable
compareTo
-
Method Details
-
getNumber
Number getNumber()- Returns:
- the
Numberbacking this numeral
-
getType
NumeralType getType()- Returns:
- the backing
Number's type
-
canConvert
- Parameters:
type- type to be checked for- Returns:
trueif this numeral can be converted to the specified type- Throws:
NullPointerException- if the specified type isnullIllegalArgumentException- if the specified type is notnull, but it is not recognised as any of the elements ofNumeralType. This should never happen
-
convert
- Parameters:
type- type for this numeral to be converted to- Returns:
- a numeral backed with the specified type
- Throws:
ExtendedArithmeticException- if the value of this numeral is outside the specified type's boundsNullPointerException- if the specified type isnullIllegalArgumentException- if the specified type is notnull, but it is not recognised as any of the elements ofNumeralType. This should never happen
-
convertIfAllowed
- Parameters:
type- type for this numeral to be converted to- Returns:
- a numeral backed with the specified type, unless the value of this numeral is outside the specified type's bounds, in which case this numeral is returned
- Throws:
NullPointerException- if the specified type isnullIllegalArgumentException- if the specified type is notnull, but it is not recognised as any of the elements ofNumeralType. This should never happen
-
toInteger
Numeral toInteger()- Returns:
- a numeral backed with the smallest integer type that can hold this numeral's value, unless this numeral is already an integer, in which case this numeral is returned
-
toDecimal
Numeral toDecimal()- Returns:
- a numeral backed with the smallest decimal type that can hold this numeral's value, unless this numeral is already a decimal, in which case this numeral is returned
-
toSmallestType
Numeral toSmallestType()- Returns:
- a numeral backed with the smallest type that can hold this numeral's value. This method does not convert between integer and decimal types.
-
intValue
int intValue()- Returns:
- the value of this numeral as an
int - Throws:
ExtendedArithmeticException- if the value of this numeral is smaller thanInteger.MIN_VALUEor greater thanInteger.MAX_VALUE
-
longValue
long longValue()- Returns:
- the value of this numeral as a
long - Throws:
ExtendedArithmeticException- if the value of this numeral is smaller thanLong.MIN_VALUEor greater thanLong.MAX_VALUE
-
bigIntValue
BigInteger bigIntValue()- Returns:
- the value of this numeral as a
BigInteger
-
floatValue
float floatValue()- Returns:
- the value of this numeral as a
float - Throws:
ExtendedArithmeticException- if the value of this numeral is smaller than-Float.MAX_VALUEor greater thanFloat.MAX_VALUE
-
doubleValue
double doubleValue()- Returns:
- the value of this numeral as a
double - Throws:
ExtendedArithmeticException- if the value of this numeral is smaller than-Double.MAX_VALUEor greater thanDouble.MAX_VALUE
-
bigDecValue
BigDecimal bigDecValue()- Returns:
- the value of this numeral as a
BigDecimal
-