Amount

data class Amount<T : Any>(val quantity: Long, val displayTokenSize: BigDecimal, val token: T) : Comparable<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.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.

Parameters

T

the type of the token, for example Currency. T should implement TokenizableAssetInfo if automatic conversion to/from a display format is required.

Constructors

Link copied to clipboard
constructor(tokenQuantity: Long, token: T)

Automatic conversion constructor from number of tokens to an Amount using getDisplayTokenSize to determine the displayTokenSize.

constructor(quantity: Long, displayTokenSize: BigDecimal, token: T)

Types

Link copied to clipboard
object Companion

Properties

Link copied to clipboard

An extension property that lets you write 100.DOLLARS.CASH

Link copied to clipboard

the nominal display unit size of a single token, potentially with trailing decimal display places if the scale parameter is non-zero.

Link copied to clipboard

the number of tokens as a long value.

Link copied to clipboard

An extension property that lets you get a cash state from an issued token, under the NULL_PARTY

Link copied to clipboard
val token: T

the type of token this is an amount of. This is usually a singleton.

Functions

Link copied to clipboard
Link copied to clipboard
Link copied to clipboard
operator fun minus(other: Amount<T>): Amount<T>

A checked subtraction operator is supported to simplify netting of Amounts.

Link copied to clipboard
operator fun plus(other: Amount<T>): Amount<T>

A checked addition operator is supported to simplify aggregation of Amounts. Mixing non-identical token types will throw IllegalArgumentException.

Link copied to clipboard
fun splitEvenly(partitions: Int): List<Amount<T>>

This method provides a token conserving divide mechanism.

Link copied to clipboard
operator fun times(other: Int): Amount<T>
operator fun times(other: Long): Amount<T>

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.

Link copied to clipboard

Convert a currency 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 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.

Link copied to clipboard
open override fun toString(): String

Convert a currency Amount to a display string representation.

Link copied to clipboard

Strips the issuer and returns an Amount of the raw token directly. This is useful when you are mixing code that cares about specific issuers with code that will accept any, or which is imposing issuer constraints via some other mechanism and the additional type safety is not wanted.