@Override protected Object propertyGet(Bean bean, String propertyName, boolean quiet) { switch (propertyName.hashCode()) { case -879772901: // amounts return ((MultiCurrencyAmount) bean).getAmounts(); } return super.propertyGet(bean, propertyName, quiet); }
/** * Obtains an instance using a function to create the entries. * <p> * The function is passed the index and returns the {@code MultiCurrencyAmount} for that index. * * @param size the number of elements, at least size one * @param valueFunction the function used to obtain each value * @return an instance initialized using the function * @throws IllegalArgumentException is size is zero or less */ public static MultiCurrencyAmountArray of(int size, IntFunction<MultiCurrencyAmount> valueFunction) { Map<Currency, double[]> map = new HashMap<>(); for (int i = 0; i < size; i++) { MultiCurrencyAmount mca = valueFunction.apply(i); for (CurrencyAmount ca : mca.getAmounts()) { double[] array = map.computeIfAbsent(ca.getCurrency(), c -> new double[size]); array[i] = ca.getAmount(); } } return new MultiCurrencyAmountArray(size, MapStream.of(map).mapValues(array -> DoubleArray.ofUnsafe(array)).toMap()); }
/** * Obtains an instance from the specified multi-currency amounts. * * @param amounts the amounts * @return an instance with the specified amounts */ public static MultiCurrencyAmountArray of(List<MultiCurrencyAmount> amounts) { int size = amounts.size(); HashMap<Currency, double[]> valueMap = new HashMap<>(); for (int i = 0; i < size; i++) { MultiCurrencyAmount multiCurrencyAmount = amounts.get(i); for (CurrencyAmount currencyAmount : multiCurrencyAmount.getAmounts()) { double[] currencyValues = valueMap.computeIfAbsent(currencyAmount.getCurrency(), ccy -> new double[size]); currencyValues[i] = currencyAmount.getAmount(); } } Map<Currency, DoubleArray> doubleArrayMap = MapStream.of(valueMap).mapValues(v -> DoubleArray.ofUnsafe(v)).toMap(); return new MultiCurrencyAmountArray(size, doubleArrayMap); }
/** * Converts a {@code MultipleCurrencyAmount} into an amount in the * specified currency using the rates in this matrix. * * @param amount the {@code MultipleCurrencyAmount} to be converted * @param targetCurrency the currency to convert all entries to * @return the total amount in the requested currency */ public CurrencyAmount convert(MultiCurrencyAmount amount, Currency targetCurrency) { ArgChecker.notNull(amount, "amount"); ArgChecker.notNull(targetCurrency, "targetCurrency"); // We could do this using the currency amounts but to // avoid creating extra objects we'll use doubles double total = amount.getAmounts() .stream() .mapToDouble(ca -> convert(ca.getAmount(), ca.getCurrency(), targetCurrency)) .sum(); return CurrencyAmount.of(targetCurrency, total); }
private void assertMCA(MultiCurrencyAmount actual, CurrencyAmount... expected) { assertEquals(actual.size(), expected.length); assertEquals(actual.getAmounts().size(), expected.length); assertEquals(actual.getAmounts(), ImmutableSet.copyOf(expected)); Set<Currency> currencies = new HashSet<>(); for (CurrencyAmount expectedAmount : expected) { currencies.add(expectedAmount.getCurrency()); assertEquals(actual.contains(expectedAmount.getCurrency()), true); assertEquals(actual.getAmount(expectedAmount.getCurrency()), expectedAmount); assertEquals(actual.getAmountOrZero(expectedAmount.getCurrency()), expectedAmount); } assertEquals(actual.getCurrencies(), currencies); Currency nonExisting = Currency.of("FRZ"); assertEquals(actual.contains(nonExisting), false); assertThrowsIllegalArg(() -> actual.getAmount(nonExisting)); assertEquals(actual.getAmountOrZero(nonExisting), CurrencyAmount.zero(nonExisting)); }
public void test_currencyExposure() { MultiCurrencyAmount computed = PRICER.currencyExposure(TRADE_STANDARD, RATES_PROVIDER, ISSUER_RATES_PROVIDER); PointSensitivities point = PRICER.presentValueSensitivity( TRADE_STANDARD, RATES_PROVIDER, ISSUER_RATES_PROVIDER); MultiCurrencyAmount expected = RATES_PROVIDER.currencyExposure(point).plus( PRICER.presentValue(TRADE_STANDARD, RATES_PROVIDER, ISSUER_RATES_PROVIDER)); assertEquals(computed.getAmounts().size(), 1); assertEquals(computed.getAmount(USD).getAmount(), expected.getAmount(USD).getAmount(), NOTIONAL * QUANTITY * TOL); }
public void test_currencyExposureFromCleanPrice() { MultiCurrencyAmount computed = PRICER.currencyExposureFromCleanPrice( TRADE_STANDARD, RATES_PROVIDER, ISSUER_RATES_PROVIDER, REF_DATA, TRADE_PRICE); PointSensitivities point = PRICER.presentValueSensitivityFromCleanPrice( TRADE_STANDARD, RATES_PROVIDER, ISSUER_RATES_PROVIDER, REF_DATA, TRADE_PRICE); MultiCurrencyAmount expected = RATES_PROVIDER.currencyExposure(point).plus( PRICER.presentValueFromCleanPrice(TRADE_STANDARD, RATES_PROVIDER, ISSUER_RATES_PROVIDER, REF_DATA, TRADE_PRICE)); assertEquals(computed.getAmounts().size(), 1); assertEquals(computed.getAmount(USD).getAmount(), expected.getAmount(USD).getAmount(), NOTIONAL * QUANTITY * TOL); }
public void test_currencyExposureFromCleanPriceWithZSpread() { MultiCurrencyAmount computed = PRICER.currencyExposureFromCleanPriceWithZSpread( TRADE_STANDARD, RATES_PROVIDER, ISSUER_RATES_PROVIDER, REF_DATA, TRADE_PRICE, Z_SPREAD, PERIODIC, PERIOD_PER_YEAR); PointSensitivities point = PRICER.presentValueSensitivityFromCleanPriceWithZSpread( TRADE_STANDARD, RATES_PROVIDER, ISSUER_RATES_PROVIDER, REF_DATA, TRADE_PRICE, Z_SPREAD, PERIODIC, PERIOD_PER_YEAR); MultiCurrencyAmount expected = RATES_PROVIDER.currencyExposure(point).plus( PRICER.presentValueFromCleanPriceWithZSpread(TRADE_STANDARD, RATES_PROVIDER, ISSUER_RATES_PROVIDER, REF_DATA, TRADE_PRICE, Z_SPREAD, PERIODIC, PERIOD_PER_YEAR)); assertEquals(computed.getAmounts().size(), 1); assertEquals(computed.getAmount(USD).getAmount(), expected.getAmount(USD).getAmount(), NOTIONAL * QUANTITY * TOL); }
public void test_currencyExposureWithZSpread() { MultiCurrencyAmount computed = PRICER.currencyExposureWithZSpread( TRADE_STANDARD, RATES_PROVIDER, ISSUER_RATES_PROVIDER, Z_SPREAD, PERIODIC, PERIOD_PER_YEAR); PointSensitivities point = PRICER.presentValueSensitivityWithZSpread( TRADE_STANDARD, RATES_PROVIDER, ISSUER_RATES_PROVIDER, Z_SPREAD, PERIODIC, PERIOD_PER_YEAR); MultiCurrencyAmount expected = RATES_PROVIDER.currencyExposure(point).plus(PRICER.presentValueWithZSpread( TRADE_STANDARD, RATES_PROVIDER, ISSUER_RATES_PROVIDER, Z_SPREAD, PERIODIC, PERIOD_PER_YEAR)); assertEquals(computed.getAmounts().size(), 1); assertEquals(computed.getAmount(USD).getAmount(), expected.getAmount(USD).getAmount(), NOTIONAL * QUANTITY * TOL); }