Top Description Inners Constructors Methods
tech.units.indriya.function

public Class DefaultNumberSystem

extends Object
implements NumberSystem
Class Inheritance
All Implemented Interfaces
tech.units.indriya.spi.NumberSystem
Imports
java.math.BigDecimal, .BigInteger, .MathContext, .RoundingMode, java.util.concurrent.atomic.AtomicInteger, .AtomicLong, java.util.function.UnaryOperator, tech.units.indriya.spi.NumberSystem

NumberSystem implementation to support Java's built-in Numbers and the RationalNumber type.
Authors
Andi Huber, Werner Keil
Since
2.0

Nested and Inner Type Summary

Modifier and TypeClass and Description
private static enum
DefaultNumberSystem.NumberType

In order of increasing number type 'widening'.

Constructor Summary

AccessConstructor and Description
public

Method Summary

Modifier and TypeMethod and Description
public Number
abs(final Number number)

Implements tech.units.indriya.spi.NumberSystem.abs.

Returns the absolute of given number as a Number that best represents the arithmetic result within the set of number types this NumberSystem supports.

public Number
add(final Number x, final Number y)

Implements tech.units.indriya.spi.NumberSystem.add.

Returns the sum of given x and y as a Number that best represents the arithmetic result within the set of number types this NumberSystem supports.

private Number
addWideAndNarrow(final DefaultNumberSystem.NumberType wideType, final Number wide, final DefaultNumberSystem.NumberType narrowType, final Number narrow)

private static Number[]
applyToArray(final Number[] array, final UnaryOperator<Number> operator)

private int
bitLengthOfInteger(final Number number)

public int
compare(final Number x, final Number y)

Implements tech.units.indriya.spi.NumberSystem.compare.

Compares two Number values numerically.

private int
compareWideVsNarrow(final DefaultNumberSystem.NumberType wideType, final Number wide, final DefaultNumberSystem.NumberType
- currently unused (but future refactoring might use it)
unusedNarrowType
,
final Number narrow)

private static BigInteger
copySignTo(final int sign, final BigInteger absNumber)

private static BigDecimal
copySignTo(final int sign, final BigDecimal absNumber)

public Number
divide(final Number x, final Number y)

Implements tech.units.indriya.spi.NumberSystem.divide.

Returns the division of given x and y as a Number that best represents the arithmetic result within the set of number types this NumberSystem supports.

public Number[]
divideAndRemainder(final Number x, final Number y, final boolean
- whether the division remainder should be rounded towards zero
roundRemainderTowardsZero
)

Implements tech.units.indriya.spi.NumberSystem.divideAndRemainder.

Returns a two-element Number array containing {x / y, x % y}

public Number
exp(final Number number)

Implements tech.units.indriya.spi.NumberSystem.exp.

Returns Euler's Constant to the power of of given number as a Number that best represents the arithmetic result within the set of number types this NumberSystem supports.

private BigInteger
pack-priv static boolean
isFractional(final BigDecimal decimal)

Whether given BigDecimal has (non-zero) fractional parts.

public boolean
isInteger(final Number number)

Implements tech.units.indriya.spi.NumberSystem.isInteger.

Checks whether given number has fractional parts or not.

private boolean
isInteger(final DefaultNumberSystem.NumberType numberType, final Number number)

Whether given Number represents an integer.

private boolean
isIntegerOnly(final Number number)

Whether the Number's type can only represent integers.

public boolean
public boolean
public boolean
public Number
log(final Number number)

Implements tech.units.indriya.spi.NumberSystem.log.

Returns the natural logarithm of given number as a Number that best represents the arithmetic result within the set of number types this NumberSystem supports.

public Number
multiply(final Number x, final Number y)

Implements tech.units.indriya.spi.NumberSystem.multiply.

Returns the product of given x and y as a Number that best represents the arithmetic result within the set of number types this NumberSystem supports.

private Number
multiplyWideAndNarrow(final DefaultNumberSystem.NumberType wideType, final Number wide, final DefaultNumberSystem.NumberType narrowType, final Number narrow)

public Number
narrow(final Number number)

Implements tech.units.indriya.spi.NumberSystem.narrow.

'Narrows' given number as a Number that best represents the numeric value within the set of number types this NumberSystem supports.

public Number
negate(final Number number)

Implements tech.units.indriya.spi.NumberSystem.negate.

Returns the negation of given number as a Number that best represents the arithmetic result within the set of number types this NumberSystem supports.

public Number
power(final Number number, final int
- an integer
exponent
)

Implements tech.units.indriya.spi.NumberSystem.power.

Returns given number to the power of exponent as a Number that best represents the arithmetic result within the set of number types this NumberSystem supports.

public Number
reciprocal(final Number number)

Implements tech.units.indriya.spi.NumberSystem.reciprocal.

Returns the reciprocal of given number as a Number that best represents the arithmetic result within the set of number types this NumberSystem supports.

public int
signum(final Number number)

Implements tech.units.indriya.spi.NumberSystem.signum.

Returns the signum function of given number.

public Number
subtract(final Number x, final Number y)

Implements tech.units.indriya.spi.NumberSystem.subtract.

Returns the difference of given x and y as a Number that best represents the arithmetic result within the set of number types this NumberSystem supports.

private BigDecimal
toBigDecimal(final Number number)

private IllegalStateException
private IllegalArgumentException
private IllegalArgumentException
Inherited from java.lang.Object:
cloneequalsfinalizegetClasshashCodenotifynotifyAlltoStringwaitwaitwait

Constructor Detail

DefaultNumberSystemback to summary
public DefaultNumberSystem()

Method Detail

absback to summary
public Number abs(final Number number)

Implements tech.units.indriya.spi.NumberSystem.abs.

Doc from tech.units.indriya.spi.NumberSystem.abs.

Returns the absolute of given number as a Number that best represents the arithmetic result within the set of number types this NumberSystem supports.

Returns:Number

abs(number)

Annotations
@Override
addback to summary
public Number add(final Number x, final Number y)

Implements tech.units.indriya.spi.NumberSystem.add.

Doc from tech.units.indriya.spi.NumberSystem.add.

Returns the sum of given x and y as a Number that best represents the arithmetic result within the set of number types this NumberSystem supports.

Returns:Number

x + y

Annotations
@Override
addWideAndNarrowback to summary
private Number addWideAndNarrow(final DefaultNumberSystem.NumberType wideType, final Number wide, final DefaultNumberSystem.NumberType narrowType, final Number narrow)
applyToArrayback to summary
private static Number[] applyToArray(final Number[] array, final UnaryOperator<Number> operator)
bitLengthOfIntegerback to summary
private int bitLengthOfInteger(final Number number)
compareback to summary
public int compare(final Number x, final Number y)

Implements tech.units.indriya.spi.NumberSystem.compare.

Doc from tech.units.indriya.spi.NumberSystem.compare.

Compares two Number values numerically.

Returns:int

the value 0 if x == y; a value less than 0 if x < y; and a value greater than 0 if x > y

Annotations
@Override
compareWideVsNarrowback to summary
private int compareWideVsNarrow(final DefaultNumberSystem.NumberType wideType, final Number wide, final DefaultNumberSystem.NumberType unusedNarrowType, final Number narrow)
Parameters
unusedNarrowType:DefaultNumberSystem.NumberType

- currently unused (but future refactoring might use it)

copySignToback to summary
private static BigInteger copySignTo(final int sign, final BigInteger absNumber)
copySignToback to summary
private static BigDecimal copySignTo(final int sign, final BigDecimal absNumber)
divideback to summary
public Number divide(final Number x, final Number y)

Implements tech.units.indriya.spi.NumberSystem.divide.

Doc from tech.units.indriya.spi.NumberSystem.divide.

Returns the division of given x and y as a Number that best represents the arithmetic result within the set of number types this NumberSystem supports.

Returns:Number

x / y

Annotations
@Override
divideAndRemainderback to summary
public Number[] divideAndRemainder(final Number x, final Number y, final boolean roundRemainderTowardsZero)

Implements tech.units.indriya.spi.NumberSystem.divideAndRemainder.

Doc from tech.units.indriya.spi.NumberSystem.divideAndRemainder.

Returns a two-element Number array containing {x / y, x % y}

Parameters
roundRemainderTowardsZero:boolean

- whether the division remainder should be rounded towards zero

Annotations
@Override
expback to summary
public Number exp(final Number number)

Implements tech.units.indriya.spi.NumberSystem.exp.

Doc from tech.units.indriya.spi.NumberSystem.exp.

Returns Euler's Constant to the power of of given number as a Number that best represents the arithmetic result within the set of number types this NumberSystem supports.

Returns:Number

e^number, with e Euler's Constant)

Annotations
@Override
integerToBigIntegerback to summary
private BigInteger integerToBigInteger(final Number number)
isFractionalback to summary
pack-priv static boolean isFractional(final BigDecimal decimal)

Whether given BigDecimal has (non-zero) fractional parts. When false, given BigDecimal can be converted to a BigInteger.

Implementation Note

BigDecimal#stripTrailingZeros() creates a new BigDecimal just to do the check.

See Also
https://stackoverflow.com/questions/1078953/check-if-bigdecimal-is-integer-value
isIntegerback to summary
public boolean isInteger(final Number number)

Implements tech.units.indriya.spi.NumberSystem.isInteger.

Doc from tech.units.indriya.spi.NumberSystem.isInteger.

Checks whether given number has fractional parts or not.

Returns:boolean

whether number represents a whole number

Annotations
@Override
isIntegerback to summary
private boolean isInteger(final DefaultNumberSystem.NumberType numberType, final Number number)

Whether given Number represents an integer. Optimized for when we know the NumberType in advance.

isIntegerOnlyback to summary
private boolean isIntegerOnly(final Number number)

Whether the Number's type can only represent integers.

If false it can also represent fractional numbers.

Note

this does not check whether given number represents an integer.

isLessThanOneback to summary
public boolean isLessThanOne(final Number number)

Implements tech.units.indriya.spi.NumberSystem.isLessThanOne.

Annotations
@Override
isOneback to summary
public boolean isOne(final Number number)

Implements tech.units.indriya.spi.NumberSystem.isOne.

Annotations
@Override
isZeroback to summary
public boolean isZero(final Number number)

Implements tech.units.indriya.spi.NumberSystem.isZero.

Annotations
@Override
logback to summary
public Number log(final Number number)

Implements tech.units.indriya.spi.NumberSystem.log.

Doc from tech.units.indriya.spi.NumberSystem.log.

Returns the natural logarithm of given number as a Number that best represents the arithmetic result within the set of number types this NumberSystem supports.

Returns:Number

natural logarithm of number

Annotations
@Override
multiplyback to summary
public Number multiply(final Number x, final Number y)

Implements tech.units.indriya.spi.NumberSystem.multiply.

Doc from tech.units.indriya.spi.NumberSystem.multiply.

Returns the product of given x and y as a Number that best represents the arithmetic result within the set of number types this NumberSystem supports.

Returns:Number

x * y

Annotations
@Override
multiplyWideAndNarrowback to summary
private Number multiplyWideAndNarrow(final DefaultNumberSystem.NumberType wideType, final Number wide, final DefaultNumberSystem.NumberType narrowType, final Number narrow)
narrowback to summary
public Number narrow(final Number number)

Implements tech.units.indriya.spi.NumberSystem.narrow.

Doc from tech.units.indriya.spi.NumberSystem.narrow.

'Narrows' given number as a Number that best represents the numeric value within the set of number types this NumberSystem supports.

eg. A BigInteger that is within range of Java's Long type can be narrowed to Long w/o loss of precision.

Returns:Number

'best' representation of number w/o loss of precision

Annotations
@Override
negateback to summary
public Number negate(final Number number)

Implements tech.units.indriya.spi.NumberSystem.negate.

Doc from tech.units.indriya.spi.NumberSystem.negate.

Returns the negation of given number as a Number that best represents the arithmetic result within the set of number types this NumberSystem supports.

Returns:Number

-number

Annotations
@Override
powerback to summary
public Number power(final Number number, final int exponent)

Implements tech.units.indriya.spi.NumberSystem.power.

Doc from tech.units.indriya.spi.NumberSystem.power.

Returns given number to the power of exponent as a Number that best represents the arithmetic result within the set of number types this NumberSystem supports.

Parameters
exponent:int

- an integer

Returns:Number

number^exponent

Annotations
@Override
reciprocalback to summary
public Number reciprocal(final Number number)

Implements tech.units.indriya.spi.NumberSystem.reciprocal.

Doc from tech.units.indriya.spi.NumberSystem.reciprocal.

Returns the reciprocal of given number as a Number that best represents the arithmetic result within the set of number types this NumberSystem supports.

Returns:Number

number^-1

Annotations
@Override
signumback to summary
public int signum(final Number number)

Implements tech.units.indriya.spi.NumberSystem.signum.

Doc from tech.units.indriya.spi.NumberSystem.signum.

Returns the signum function of given number.

Returns:int

signum(number)

Annotations
@Override
subtractback to summary
public Number subtract(final Number x, final Number y)

Implements tech.units.indriya.spi.NumberSystem.subtract.

Doc from tech.units.indriya.spi.NumberSystem.subtract.

Returns the difference of given x and y as a Number that best represents the arithmetic result within the set of number types this NumberSystem supports.

Returns:Number

x - y

Annotations
@Override
toBigDecimalback to summary
private BigDecimal toBigDecimal(final Number number)
unexpectedCodeReachback to summary
private IllegalStateException unexpectedCodeReach()
unsupportedNumberTypeback to summary
private IllegalArgumentException unsupportedNumberType(final Number number)
unsupportedNumberValueback to summary
private IllegalArgumentException unsupportedNumberValue(final Number number)
tech.units.indriya.function back to summary

private sealed Enum DefaultNumberSystem.NumberType

extends Enum<DefaultNumberSystem.NumberType>
Class Inheritance

In order of increasing number type 'widening'.

Field Summary

Modifier and TypeField and Description
public static final DefaultNumberSystem.NumberType
public static final DefaultNumberSystem.NumberType
public static final DefaultNumberSystem.NumberType
public static final DefaultNumberSystem.NumberType
public static final DefaultNumberSystem.NumberType
public static final DefaultNumberSystem.NumberType
public static final DefaultNumberSystem.NumberType
private final boolean
public static final DefaultNumberSystem.NumberType
public static final DefaultNumberSystem.NumberType
private final Number
public static final DefaultNumberSystem.NumberType
public static final DefaultNumberSystem.NumberType
private final Class<? extends Number>
private final Number

Constructor Summary

AccessConstructor and Description
private
NumberType(final boolean integerOnly, final Class<? extends Number> type, final Number one, final Number zero)

Method Summary

Modifier and TypeMethod and Description
public Class<? extends Number>
public boolean
isIntegerOnly()

Whether the underlying number type can only represent integers.

pack-priv boolean
isZero(Number
- must be of type getType()
number
)

Whether given Number is ZERO.

public static DefaultNumberSystem.NumberType
pack-priv static DefaultNumberSystem.NumberType
valueOf(final Number number)

public static DefaultNumberSystem.NumberType[]
Inherited from java.lang.Enum:
clonecompareTodescribeConstableequalsfinalizegetDeclaringClasshashCodenameordinaltoStringvalueOf

Field Detail

BIG_DECIMALback to summary
public static final DefaultNumberSystem.NumberType BIG_DECIMAL
BIG_INTEGERback to summary
public static final DefaultNumberSystem.NumberType BIG_INTEGER
BYTE_BOXEDback to summary
public static final DefaultNumberSystem.NumberType BYTE_BOXED
DOUBLE_BOXEDback to summary
public static final DefaultNumberSystem.NumberType DOUBLE_BOXED
FLOAT_BOXEDback to summary
public static final DefaultNumberSystem.NumberType FLOAT_BOXED
INTEGER_ATOMICback to summary
public static final DefaultNumberSystem.NumberType INTEGER_ATOMIC
INTEGER_BOXEDback to summary
public static final DefaultNumberSystem.NumberType INTEGER_BOXED
integerOnlyback to summary
private final boolean integerOnly
LONG_ATOMICback to summary
public static final DefaultNumberSystem.NumberType LONG_ATOMIC
LONG_BOXEDback to summary
public static final DefaultNumberSystem.NumberType LONG_BOXED
oneback to summary
private final Number one
RATIONALback to summary
public static final DefaultNumberSystem.NumberType RATIONAL
SHORT_BOXEDback to summary
public static final DefaultNumberSystem.NumberType SHORT_BOXED
typeback to summary
private final Class<? extends Number> type
zeroback to summary
private final Number zero

Constructor Detail

NumberTypeback to summary
private NumberType(final boolean integerOnly, final Class<? extends Number> type, final Number one, final Number zero)

Method Detail

getTypeback to summary
public Class<? extends Number> getType()
Annotations
@SuppressWarnings:unused
isIntegerOnlyback to summary
public boolean isIntegerOnly()

Whether the underlying number type can only represent integers.

If false it can also represent fractional numbers.

isZeroback to summary
pack-priv boolean isZero(Number number)

Whether given Number is ZERO.

API Note

For class internal use only, such that we have control over the number's type that gets passed in.

Parameters
number:Number

- must be of type getType()

valueOfback to summary
public static DefaultNumberSystem.NumberType valueOf(String name)
valueOfback to summary
pack-priv static DefaultNumberSystem.NumberType valueOf(final Number number)
valuesback to summary
public static DefaultNumberSystem.NumberType[] values()