/** * Applies an operation to the amounts. * <p> * 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. * <pre> * multiplied = base.mapAmount(value -> value * 3); * </pre> * * @param mapper the operator to be applied to the amounts * @return a copy of this amount with the mapping applied to the original amounts */ public MultiCurrencyAmount mapAmounts(DoubleUnaryOperator mapper) { ArgChecker.notNull(mapper, "mapper"); return amounts.stream() .map(ca -> ca.mapAmount(mapper)) .collect(MultiCurrencyAmount.collectorInternal()); }
/** * Converts an FX exchange to a string. * * @param base the base currency amount * @param counter the counter currency amount * @return the string form */ public static String fx(CurrencyAmount base, CurrencyAmount counter) { BigDecimal rateDec = BigDecimal.valueOf( counter.getAmount() / base.getAmount()).setScale(base.getCurrency().getMinorUnitDigits() + 2, RoundingMode.HALF_UP).abs(); FxRate rate = FxRate.of(base.getCurrency(), counter.getCurrency(), rateDec.doubleValue()); BigDecimal baseDec = BigDecimal.valueOf(base.getAmount()).stripTrailingZeros(); BigDecimal counterDec = BigDecimal.valueOf(counter.getAmount()).stripTrailingZeros(); boolean roundBase = baseDec.scale() < counterDec.scale(); CurrencyAmount round = roundBase ? base : counter; return (round.getAmount() < 0 ? "Pay " : "Rec ") + SummarizerUtils.amount(round.mapAmount(a -> Math.abs(a))) + " " + "@ " + rate; }
public void test_mapAmount() { CurrencyAmount test = CCY_AMOUNT.mapAmount(v -> v * 2 + 1); assertEquals(test, CurrencyAmount.of(CCY1, AMT1 * 2 + 1)); }
@Override public PortfolioItemSummary summarize() { // Long Pay USD 1mm @ GBP/USD 1.32 Premium USD 100k : 21Jan18 StringBuilder buf = new StringBuilder(96); CurrencyAmount base = product.getUnderlying().getBaseCurrencyAmount(); CurrencyAmount counter = product.getUnderlying().getCounterCurrencyAmount(); buf.append(product.getLongShort()); buf.append(' '); buf.append(SummarizerUtils.fx(base, counter)); buf.append(" Premium "); buf.append(SummarizerUtils.amount(premium.getValue().mapAmount(v -> Math.abs(v)))); buf.append(" : "); buf.append(SummarizerUtils.date(product.getExpiryDate())); CurrencyPair currencyPair = product.getCurrencyPair(); return SummarizerUtils.summary( this, ProductType.FX_VANILLA_OPTION, buf.toString(), currencyPair.getBase(), currencyPair.getCounter()); }
@Override public PortfolioItemSummary summarize() { // Long Barrier Pay USD 1mm Premium USD 100k @ GBP/USD 1.32 : 21Jan18 StringBuilder buf = new StringBuilder(96); CurrencyAmount base = product.getUnderlyingOption().getUnderlying().getBaseCurrencyAmount(); CurrencyAmount counter = product.getUnderlyingOption().getUnderlying().getCounterCurrencyAmount(); buf.append(product.getUnderlyingOption().getLongShort()); buf.append(" Barrier "); buf.append(SummarizerUtils.fx(base, counter)); buf.append(" Premium "); buf.append(SummarizerUtils.amount(premium.getValue().mapAmount(v -> Math.abs(v)))); buf.append(" : "); buf.append(SummarizerUtils.date(product.getUnderlyingOption().getExpiryDate())); CurrencyPair currencyPair = product.getCurrencyPair(); return SummarizerUtils.summary( this, ProductType.FX_SINGLE_BARRIER_OPTION, buf.toString(), currencyPair.getBase(), currencyPair.getCounter()); }