private static ImmutableMap<CurrencyPair, Integer> parsePairs(IniFile ini) { ImmutableMap.Builder<CurrencyPair, Integer> builder = ImmutableMap.builder(); for (Entry<String, PropertySet> entry : ini.asMap().entrySet()) { String pairStr = entry.getKey(); if (CurrencyPair.REGEX_FORMAT.matcher(pairStr).matches()) { CurrencyPair pair = CurrencyPair.parse(pairStr); PropertySet properties = entry.getValue(); Integer rateDigits = Integer.parseInt(properties.value("rateDigits")); builder.put(pair, rateDigits); } } return builder.build(); }
@Test(dataProvider = "parseBad", expectedExceptions = IllegalArgumentException.class) public void test_parse_String_bad(String input) { CurrencyPair.parse(input); }
@Test(dataProvider = "parseGood") public void test_parse_String_good(String input, Currency base, Currency counter) { assertEquals(CurrencyPair.parse(input), CurrencyPair.of(base, counter)); }
private static void parseSingle( Predicate<LocalDate> datePredicate, CharSource resource, Map<LocalDate, ImmutableMap.Builder<FxRateId, FxRate>> mutableMap) { try { CsvFile csv = CsvFile.of(resource, true); for (CsvRow row : csv.rows()) { String dateText = row.getField(DATE_FIELD); LocalDate date = LoaderUtils.parseDate(dateText); if (datePredicate.test(date)) { String currencyPairStr = row.getField(CURRENCY_PAIR_FIELD); String valueStr = row.getField(VALUE_FIELD); CurrencyPair currencyPair = CurrencyPair.parse(currencyPairStr); double value = Double.valueOf(valueStr); ImmutableMap.Builder<FxRateId, FxRate> builderForDate = mutableMap.computeIfAbsent(date, k -> ImmutableMap.builder()); builderForDate.put(FxRateId.of(currencyPair), FxRate.of(currencyPair, value)); } } } catch (RuntimeException ex) { throw new IllegalArgumentException( Messages.format("Error processing resource as CSV file: {}", resource), ex); } }
public void coverage_builder() { MetaBean meta = MetaBean.of(FxRateId.class); Bean test1 = meta.builder() .set("pair", CurrencyPair.parse("EUR/GBP")) .set("observableSource", OBS_SOURCE) .build(); Bean test2 = meta.builder() .set("pair", CurrencyPair.parse("EUR/GBP")) .set("observableSource", OBS_SOURCE) .build(); coverBeanEquals(test1, test2); }
private static FxSingleTrade parseConvention(CsvRow row, TradeInfo info) { CurrencyPair pair = CurrencyPair.parse(row.getValue(CONVENTION_FIELD)); BuySell buySell = LoaderUtils.parseBuySell(row.getValue(BUY_SELL_FIELD)); Currency currency = Currency.parse(row.getValue(CURRENCY_FIELD)); double notional = LoaderUtils.parseDouble(row.getValue(NOTIONAL_FIELD)); double fxRate = LoaderUtils.parseDouble(row.getValue(FX_RATE_FIELD)); LocalDate paymentDate = LoaderUtils.parseDate(row.getValue(PAYMENT_DATE_FIELD)); Optional<BusinessDayAdjustment> paymentAdj = parsePaymentDateAdjustment(row); CurrencyAmount amount = CurrencyAmount.of(currency, buySell.normalize(notional)); FxSingle fx = paymentAdj .map(adj -> FxSingle.of(amount, FxRate.of(pair, fxRate), paymentDate, adj)) .orElseGet(() -> FxSingle.of(amount, FxRate.of(pair, fxRate), paymentDate)); return FxSingleTrade.of(info, fx); }
private static FxSwapTrade parseConvention(CsvRow row, TradeInfo info) { CurrencyPair pair = CurrencyPair.parse(row.getValue(CONVENTION_FIELD)); BuySell buySell = LoaderUtils.parseBuySell(row.getValue(BUY_SELL_FIELD)); Currency currency = Currency.parse(row.getValue(CURRENCY_FIELD)); double notional = LoaderUtils.parseDouble(row.getValue(NOTIONAL_FIELD)); double nearFxRate = LoaderUtils.parseDouble(row.getValue(FX_RATE_FIELD)); double farFxRate = LoaderUtils.parseDouble(row.getValue(FAR_FX_RATE_DATE_FIELD)); LocalDate nearPaymentDate = LoaderUtils.parseDate(row.getValue(PAYMENT_DATE_FIELD)); LocalDate farPaymentDate = LoaderUtils.parseDate(row.getValue(FAR_PAYMENT_DATE_FIELD)); Optional<BusinessDayAdjustment> paymentAdj = FxSingleTradeCsvLoader.parsePaymentDateAdjustment(row); CurrencyAmount amount = CurrencyAmount.of(currency, buySell.normalize(notional)); FxRate nearRate = FxRate.of(pair, nearFxRate); FxRate farRate = FxRate.of(pair, farFxRate); FxSwap fx = paymentAdj .map(adj -> FxSwap.of(amount, nearRate, nearPaymentDate, farRate, farPaymentDate, adj)) .orElseGet(() -> FxSwap.of(amount, nearRate, nearPaymentDate, farRate, farPaymentDate)); return FxSwapTrade.of(info, fx); }
public void test_builder() { assertThrowsIllegalArg(() -> DiscountFxForwardRates.meta().builder() .set(DiscountFxForwardRates.meta().currencyPair(), CurrencyPair.parse("GBP/USD")).build()); assertThrowsIllegalArg(() -> DiscountFxForwardRates.meta().builder() .set(DiscountFxForwardRates.meta().currencyPair().name(), CurrencyPair.parse("GBP/USD")).build()); }