public class Amount<T>
Amount represents a positive quantity of some token (currency, asset, etc.), measured in quantity of the smallest
representable units. The nominal quantity represented by each individual token is equal to the displayTokenSize.
The scale property of the displayTokenSize should correctly reflect the displayed decimal places and is used
when rounding conversions from indicative/displayed amounts in BigDecimal to Amount occur via the
Amount.Companion.fromDecimal
method.
Amounts of different tokens do not mix and attempting to add or subtract two amounts of different currencies will throw IllegalArgumentException. Amounts may not be negative. Amounts are represented internally using a signed 64 bit value, therefore, the maximum expressable amount is 2^63 - 1 == Long.MAX_VALUE. Addition, subtraction and multiplication are overflow checked and will throw ArithmeticException if the operation would have caused integer overflow.
Modifier and Type | Class and Description |
---|---|
static class |
Amount.Companion |
Modifier and Type | Field and Description |
---|---|
static Amount.Companion |
Companion |
Constructor and Description |
---|
Amount(long quantity,
java.math.BigDecimal displayTokenSize,
T token)
Amount represents a positive quantity of some token (currency, asset, etc.), measured in quantity of the smallest
representable units. The nominal quantity represented by each individual token is equal to the displayTokenSize.
The scale property of the displayTokenSize should correctly reflect the displayed decimal places and is used
when rounding conversions from indicative/displayed amounts in BigDecimal to Amount occur via the
Amount.Companion.fromDecimal method. |
Amount(long tokenQuantity,
T token)
Automatic conversion constructor from number of tokens to an Amount using getDisplayTokenSize to determine
the displayTokenSize.
|
Modifier and Type | Method and Description |
---|---|
long |
component1()
the number of tokens as a long value.
|
java.math.BigDecimal |
component2()
the nominal display unit size of a single token,
|
T |
component3()
the type of token this is an amount of.
|
Amount<T> |
copy(long quantity,
java.math.BigDecimal displayTokenSize,
T token)
Amount represents a positive quantity of some token (currency, asset, etc.), measured in quantity of the smallest
representable units. The nominal quantity represented by each individual token is equal to the displayTokenSize.
The scale property of the displayTokenSize should correctly reflect the displayed decimal places and is used
when rounding conversions from indicative/displayed amounts in BigDecimal to Amount occur via the
Amount.Companion.fromDecimal method. |
boolean |
equals(java.lang.Object p) |
java.math.BigDecimal |
getDisplayTokenSize()
the nominal display unit size of a single token,
|
long |
getQuantity()
the number of tokens as a long value.
|
T |
getToken()
the type of token this is an amount of.
|
int |
hashCode() |
Amount<T> |
minus(Amount<T> other)
A checked subtraction operator is supported to simplify netting of Amounts.
|
Amount<T> |
plus(Amount<T> other)
A checked addition operator is supported to simplify aggregation of Amounts.
Mixing non-identical token types will throw IllegalArgumentException.
|
java.util.List<net.corda.core.contracts.Amount> |
splitEvenly(int partitions)
This method provides a token conserving divide mechanism.
|
Amount<T> |
times(long other)
The multiplication operator is supported to allow easy calculation for multiples of a primitive Amount.
Note this is not a conserving operation, so it may not always be correct modelling of proper token behaviour.
N.B. Division is not supported as fractional tokens are not representable by an Amount.
|
Amount<T> |
times(int other)
The multiplication operator is supported to allow easy calculation for multiples of a primitive Amount.
Note this is not a conserving operation, so it may not always be correct modelling of proper token behaviour.
N.B. Division is not supported as fractional tokens are not representable by an Amount.
|
java.math.BigDecimal |
toDecimal()
Convert a currency
class Amount to a decimal representation. For example, with an amount with a quantity
of "1234" GBP, returns "12.34". The precise representation is controlled by the display token size (
from Amount.Companion.getDisplayTokenSize ), which determines the size of a single token and controls the trailing decimal
places via its scale property. Note that currencies such as the Bahraini Dinar use 3 decimal places,
and it must not be presumed that this converts amounts to 2 decimal places. |
java.lang.String |
toString()
Convert a currency
class Amount to a display string representation. |
public static Amount.Companion Companion
public Amount(long quantity, java.math.BigDecimal displayTokenSize, T token)
Amount represents a positive quantity of some token (currency, asset, etc.), measured in quantity of the smallest
representable units. The nominal quantity represented by each individual token is equal to the displayTokenSize.
The scale property of the displayTokenSize should correctly reflect the displayed decimal places and is used
when rounding conversions from indicative/displayed amounts in BigDecimal to Amount occur via the
Amount.Companion.fromDecimal
method.
Amounts of different tokens do not mix and attempting to add or subtract two amounts of different currencies will throw IllegalArgumentException. Amounts may not be negative. Amounts are represented internally using a signed 64 bit value, therefore, the maximum expressable amount is 2^63 - 1 == Long.MAX_VALUE. Addition, subtraction and multiplication are overflow checked and will throw ArithmeticException if the operation would have caused integer overflow.
quantity
- the number of tokens as a long value.displayTokenSize
- the nominal display unit size of a single token, potentially with trailing decimal displayplaces if the scale parameter is non-zero.token
- the type of token this is an amount of. This is usually a singleton.Amount.Companion.fromDecimal
,
IllegalArgumentException,
ArithmeticExceptionpublic Amount(long tokenQuantity, T token)
Automatic conversion constructor from number of tokens to an Amount using getDisplayTokenSize to determine the displayTokenSize.
tokenQuantity
- the number of tokens represented.token
- the type of the token, for example a Currency object.public Amount<T> plus(Amount<T> other)
A checked addition operator is supported to simplify aggregation of Amounts. Mixing non-identical token types will throw IllegalArgumentException.
public Amount<T> minus(Amount<T> other)
A checked subtraction operator is supported to simplify netting of Amounts.
public Amount<T> times(long other)
The multiplication operator is supported to allow easy calculation for multiples of a primitive Amount. Note this is not a conserving operation, so it may not always be correct modelling of proper token behaviour. N.B. Division is not supported as fractional tokens are not representable by an Amount.
public Amount<T> times(int other)
The multiplication operator is supported to allow easy calculation for multiples of a primitive Amount. Note this is not a conserving operation, so it may not always be correct modelling of proper token behaviour. N.B. Division is not supported as fractional tokens are not representable by an Amount.
public java.util.List<net.corda.core.contracts.Amount> splitEvenly(int partitions)
This method provides a token conserving divide mechanism.
partitions
- the number of amounts to divide the current quantity into.public java.math.BigDecimal toDecimal()
Convert a currency class Amount
to a decimal representation. For example, with an amount with a quantity
of "1234" GBP, returns "12.34". The precise representation is controlled by the display token size (
from Amount.Companion.getDisplayTokenSize
), which determines the size of a single token and controls the trailing decimal
places via its scale property. Note that currencies such as the Bahraini Dinar use 3 decimal places,
and it must not be presumed that this converts amounts to 2 decimal places.
public java.lang.String toString()
Convert a currency class Amount
to a display string representation.
For example, with an amount with a quantity of "1234" GBP, returns "12.34 GBP". The result of fromDecimal is used to control the numerical formatting and the token specifier appended is taken from token.toString.
class Amount
,
Amount.Companion.fromDecimal
public long getQuantity()
the number of tokens as a long value.
public java.math.BigDecimal getDisplayTokenSize()
the nominal display unit size of a single token,
potentially with trailing decimal displayplaces if the scale parameter is non-zero.
public T getToken()
the type of token this is an amount of.
This is usually a singleton.
public long component1()
the number of tokens as a long value.
public java.math.BigDecimal component2()
the nominal display unit size of a single token,
potentially with trailing decimal displayplaces if the scale parameter is non-zero.
public T component3()
the type of token this is an amount of.
This is usually a singleton.
public Amount<T> copy(long quantity, java.math.BigDecimal displayTokenSize, T token)
Amount represents a positive quantity of some token (currency, asset, etc.), measured in quantity of the smallest
representable units. The nominal quantity represented by each individual token is equal to the displayTokenSize.
The scale property of the displayTokenSize should correctly reflect the displayed decimal places and is used
when rounding conversions from indicative/displayed amounts in BigDecimal to Amount occur via the
Amount.Companion.fromDecimal
method.
Amounts of different tokens do not mix and attempting to add or subtract two amounts of different currencies will throw IllegalArgumentException. Amounts may not be negative. Amounts are represented internally using a signed 64 bit value, therefore, the maximum expressable amount is 2^63 - 1 == Long.MAX_VALUE. Addition, subtraction and multiplication are overflow checked and will throw ArithmeticException if the operation would have caused integer overflow.
public int hashCode()
public boolean equals(java.lang.Object p)