Class MultiCurrencyAmount

    • Method Detail

      • empty

        public static MultiCurrencyAmount empty()
        Obtains an empty MultiCurrencyAmount.
        Returns:
        the empty instance
      • of

        public static MultiCurrencyAmount of​(Currency currency,
                                             double amount)
        Obtains an instance from a currency and amount.
        Parameters:
        currency - the currency
        amount - the amount
        Returns:
        the amount
      • of

        public static MultiCurrencyAmount of​(CurrencyAmount... amounts)
        Obtains an instance from an array of CurrencyAmount objects.

        It is an error for the input to contain the same currency twice.

        Parameters:
        amounts - the amounts
        Returns:
        the amount
      • of

        public static MultiCurrencyAmount of​(Iterable<CurrencyAmount> amounts)
        Obtains an instance from a list of CurrencyAmount objects.

        It is an error for the input to contain the same currency twice.

        Parameters:
        amounts - the amounts
        Returns:
        the amount
      • of

        public static MultiCurrencyAmount of​(Map<Currency,​Double> map)
        Obtains an instance from a map of currency to amount.
        Parameters:
        map - the map of currency to amount
        Returns:
        the amount
      • total

        public static MultiCurrencyAmount total​(Iterable<CurrencyAmount> amounts)
        Obtains an instance from the total of a list of CurrencyAmount objects.

        If the input contains the same currency more than once, the amounts are added together. For example, an input of (EUR 100, EUR 200, CAD 100) would result in (EUR 300, CAD 100).

        Parameters:
        amounts - the amounts
        Returns:
        the amount
      • toMultiCurrencyAmount

        public static Collector<CurrencyAmount,​?,​MultiCurrencyAmount> toMultiCurrencyAmount()
        Returns a collector that can be used to create a multi-currency amount from a stream of amounts.

        If the input contains the same currency more than once, the amounts are added together. For example, an input of (EUR 100, EUR 200, CAD 100) would result in (EUR 300, CAD 100).

        Returns:
        the collector
      • getCurrencies

        public ImmutableSet<Currency> getCurrencies()
        Gets the set of stored currencies.
        Returns:
        the set of currencies in this amount
      • size

        public int size()
        Gets the number of stored amounts.
        Returns:
        the number of amounts
      • contains

        public boolean contains​(Currency currency)
        Checks if this multi-amount contains an amount for the specified currency.
        Parameters:
        currency - the currency to find
        Returns:
        true if this amount contains a value for the currency
      • getAmount

        public CurrencyAmount getAmount​(Currency currency)
        Gets the CurrencyAmount for the specified currency, throwing an exception if not found.
        Parameters:
        currency - the currency to find an amount for
        Returns:
        the amount
        Throws:
        IllegalArgumentException - if the currency is not found
      • getAmountOrZero

        public CurrencyAmount getAmountOrZero​(Currency currency)
        Gets the CurrencyAmount for the specified currency, returning zero if not found.
        Parameters:
        currency - the currency to find an amount for
        Returns:
        the amount
      • plus

        public MultiCurrencyAmount plus​(Currency currency,
                                        double amountToAdd)
        Returns a copy of this MultiCurrencyAmount with the specified amount added.

        This adds the specified amount to this monetary amount, returning a new object. If the currency is already present, the amount is added to the existing amount. If the currency is not yet present, the currency-amount is added to the map. The addition uses standard double arithmetic.

        This instance is immutable and unaffected by this method.

        Parameters:
        currency - the currency to add to
        amountToAdd - the amount to add
        Returns:
        an amount based on this with the specified amount added
      • plus

        public MultiCurrencyAmount plus​(CurrencyAmount amountToAdd)
        Returns a copy of this MultiCurrencyAmount with the specified amount added.

        This adds the specified amount to this monetary amount, returning a new object. If the currency is already present, the amount is added to the existing amount. If the currency is not yet present, the currency-amount is added to the map. The addition uses standard double arithmetic.

        This instance is immutable and unaffected by this method.

        Parameters:
        amountToAdd - the amount to add
        Returns:
        an amount based on this with the specified amount added
      • plus

        public MultiCurrencyAmount plus​(MultiCurrencyAmount amountToAdd)
        Returns a copy of this MultiCurrencyAmount with the specified amount added.

        This adds the specified amount to this monetary amount, returning a new object. If the currency is already present, the amount is added to the existing amount. If the currency is not yet present, the currency-amount is added to the map. The addition uses standard double arithmetic.

        This instance is immutable and unaffected by this method.

        Parameters:
        amountToAdd - the amount to add
        Returns:
        an amount based on this with the specified amount added
      • minus

        public MultiCurrencyAmount minus​(Currency currency,
                                         double amountToAdd)
        Returns a copy of this MultiCurrencyAmount with the specified amount subtracted.

        This subtracts the specified amount from this monetary amount, returning a new object. If the currency is already present, the amount is subtracted from the existing amount. If the currency is not yet present, the negated amount is included. The subtraction uses standard double arithmetic.

        This instance is immutable and unaffected by this method.

        Parameters:
        currency - the currency to subtract from
        amountToAdd - the amount to subtract
        Returns:
        an amount based on this with the specified amount subtracted
      • minus

        public MultiCurrencyAmount minus​(CurrencyAmount amountToSubtract)
        Returns a copy of this MultiCurrencyAmount with the specified amount subtracted.

        This subtracts the specified amount from this monetary amount, returning a new object. If the currency is already present, the amount is subtracted from the existing amount. If the currency is not yet present, the negated amount is included. The subtraction uses standard double arithmetic.

        This instance is immutable and unaffected by this method.

        Parameters:
        amountToSubtract - the amount to subtract
        Returns:
        an amount based on this with the specified amount subtracted
      • minus

        public MultiCurrencyAmount minus​(MultiCurrencyAmount amountToSubtract)
        Returns a copy of this MultiCurrencyAmount with the specified amount subtracted.

        This subtracts the specified amount from this monetary amount, returning a new object. If the currency is already present, the amount is subtracted from the existing amount. If the currency is not yet present, the negated amount is included. The subtraction uses standard double arithmetic.

        This instance is immutable and unaffected by this method.

        Parameters:
        amountToSubtract - the amount to subtract
        Returns:
        an amount based on this with the specified amount subtracted
      • multipliedBy

        public MultiCurrencyAmount multipliedBy​(double factor)
        Returns a copy of this MultiCurrencyAmount with all the amounts multiplied by the factor.

        This instance is immutable and unaffected by this method.

        Parameters:
        factor - the multiplicative factor
        Returns:
        an amount based on this with all the amounts multiplied by the factor
      • negated

        public MultiCurrencyAmount negated()
        Returns a copy of this CurrencyAmount with the amount negated.

        This takes this amount and negates it. If any amount is 0.0 or -0.0 the negated amount is 0.0.

        This instance is immutable and unaffected by this method.

        Returns:
        an amount based on this with the amount negated
      • stream

        public Stream<CurrencyAmount> stream()
        Returns a stream over the currency amounts.

        This provides access to the entire set of amounts.

        Returns:
        a stream over the individual amounts
      • mapAmounts

        public MultiCurrencyAmount mapAmounts​(DoubleUnaryOperator mapper)
        Applies an operation to the amounts.

        This is generally used to apply a mathematical operation to the amounts. For example, the operator could multiply the amounts by a constant, or take the inverse.

           multiplied = base.mapAmount(value -> value * 3);
         
        Parameters:
        mapper - the operator to be applied to the amounts
        Returns:
        a copy of this amount with the mapping applied to the original amounts
      • mapCurrencyAmounts

        public MultiCurrencyAmount mapCurrencyAmounts​(UnaryOperator<CurrencyAmount> operator)
        Applies an operation to the currency amounts.

        The operator is called once for each currency in this amount. The operator may return an amount with a different currency. The result will be the total of the altered amounts.

        Parameters:
        operator - the operator to be applied to the amounts
        Returns:
        a copy of this amount with the mapping applied to the original amounts
      • convertedTo

        public CurrencyAmount convertedTo​(Currency resultCurrency,
                                          FxRateProvider rateProvider)
        Converts this amount to an equivalent amount the specified currency.

        The result will be expressed in terms of the given currency. If conversion is needed, the provider will be used to supply the FX rate.

        Specified by:
        convertedTo in interface FxConvertible<CurrencyAmount>
        Parameters:
        resultCurrency - the currency of the result
        rateProvider - the provider of FX rates
        Returns:
        the converted instance, which should be expressed in the specified currency
        Throws:
        RuntimeException - if no FX rate could be found
      • toMap

        public ImmutableSortedMap<Currency,​Double> toMap()
        Converts this MultiCurrencyAmount to a map keyed by currency.
        Returns:
        the amounts in a map keyed by currency
      • toString

        public String toString()
        Gets the amount as a string.

        The format includes each currency-amount.

        Overrides:
        toString in class Object
        Returns:
        the currency amount
      • meta

        public static MultiCurrencyAmount.Meta meta()
        The meta-bean for MultiCurrencyAmount.
        Returns:
        the meta-bean, not null
      • getAmounts

        public ImmutableSortedSet<CurrencyAmount> getAmounts()
        Gets the set of currency amounts. Each currency will occur only once, as per a map keyed by currency.
        Returns:
        the value of the property, not null
      • hashCode

        public int hashCode()
        Overrides:
        hashCode in class Object