/** * Returns the total of the sensitivity values. * <p> * The result is the total of all values, in whatever currency they are defined. * * @return the total sensitivity */ public MultiCurrencyAmount total() { return sensitivities.stream() .map(CrossGammaParameterSensitivity::total) .collect(toMultiCurrencyAmount()); }
/** * Returns the total of the sensitivity values. * <p> * The result is the total of all values, in whatever currency they are defined. * * @return the total sensitivity */ public MultiCurrencyAmount total() { return sensitivities.stream() .map(CurrencyParameterSensitivity::total) .collect(toMultiCurrencyAmount()); }
/** * Obtains an instance from the total of a list of {@code CurrencyAmount} objects. * <p> * 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). * * @param amounts the amounts * @return the amount */ public static MultiCurrencyAmount total(Iterable<CurrencyAmount> amounts) { ArgChecker.notNull(amounts, "amounts"); return Guavate.stream(amounts).collect(toMultiCurrencyAmount()); }
/** * Applies an operation to the currency amounts. * <p> * 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. * * @param operator the operator to be applied to the amounts * @return a copy of this amount with the mapping applied to the original amounts */ public MultiCurrencyAmount mapCurrencyAmounts(UnaryOperator<CurrencyAmount> operator) { ArgChecker.notNull(operator, "operator"); return amounts.stream() .map(ca -> operator.apply(ca)) .collect(MultiCurrencyAmount.toMultiCurrencyAmount()); }
/** * Returns a copy of this {@code MultiCurrencyAmount} with the specified amount added. * <p> * 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 {@code double} arithmetic. * <p> * This instance is immutable and unaffected by this method. * * @param amountToAdd the amount to add * @return an amount based on this with the specified amount added */ public MultiCurrencyAmount plus(CurrencyAmount amountToAdd) { ArgChecker.notNull(amountToAdd, "amountToAdd"); return Stream.concat(amounts.stream(), Stream.of(amountToAdd)).collect(toMultiCurrencyAmount()); }
/** * Returns a copy of this {@code MultiCurrencyAmount} with the specified amount added. * <p> * 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 {@code double} arithmetic. * <p> * This instance is immutable and unaffected by this method. * * @param amountToAdd the amount to add * @return an amount based on this with the specified amount added */ public MultiCurrencyAmount plus(MultiCurrencyAmount amountToAdd) { ArgChecker.notNull(amountToAdd, "amountToAdd"); return Stream.concat(amounts.stream(), amountToAdd.stream()).collect(toMultiCurrencyAmount()); }
public void test_collector_null() { List<CurrencyAmount> amount = Arrays.asList( CurrencyAmount.of(CCY1, 100), null, CurrencyAmount.of(CCY2, 100)); assertThrowsIllegalArg(() -> amount.stream().collect(toMultiCurrencyAmount())); }
private static MultiCurrencyAmount swapValue( RatesProvider provider, ResolvedSwap swap, ToDoubleBiFunction<ResolvedSwapLeg, RatesProvider> legFn) { if (swap.isCrossCurrency()) { return swap.getLegs().stream() .map(leg -> CurrencyAmount.of(leg.getCurrency(), legFn.applyAsDouble(leg, provider))) .collect(toMultiCurrencyAmount()); } else { Currency currency = swap.getLegs().iterator().next().getCurrency(); double total = 0d; for (ResolvedSwapLeg leg : swap.getLegs()) { total += legFn.applyAsDouble(leg, provider); } return MultiCurrencyAmount.of(currency, total); } }
public void test_collector() { List<CurrencyAmount> amount = ImmutableList.of( CurrencyAmount.of(CCY1, 100), CurrencyAmount.of(CCY1, 150), CurrencyAmount.of(CCY2, 100)); MultiCurrencyAmount test = amount.stream().collect(toMultiCurrencyAmount()); MultiCurrencyAmount expected = MultiCurrencyAmount.of(CurrencyAmount.of(CCY1, 250), CurrencyAmount.of(CCY2, 100)); assertEquals(test, expected); }
public void test_collector_parallel() { List<CurrencyAmount> amount = ImmutableList.of( CurrencyAmount.of(CCY1, 100), CurrencyAmount.of(CCY1, 150), CurrencyAmount.of(CCY2, 100)); MultiCurrencyAmount test = amount.parallelStream().collect(toMultiCurrencyAmount()); MultiCurrencyAmount expected = MultiCurrencyAmount.of(CurrencyAmount.of(CCY1, 250), CurrencyAmount.of(CCY2, 100)); assertEquals(test, expected); }
public void test_stream() { MultiCurrencyAmount base = MultiCurrencyAmount.of(CA1, CA2); MultiCurrencyAmount test = base.stream() .map(ca -> ca.mapAmount(a -> a * 3)) .collect(toMultiCurrencyAmount()); assertMCA(test, CA1.mapAmount(a -> a * 3), CA2.mapAmount(a -> a * 3)); }