/** * Parses currency from the input string. * <p> * Parsing is case insensitive. * * @param str the string to parse * @return the parsed currency * @throws IllegalArgumentException if the string cannot be parsed */ public static Currency parseCurrency(String str) { try { return Currency.parse(str); } catch (RuntimeException ex) { throw new IllegalArgumentException( "Unknown Currency, must be 3 letter ISO-4217 format but was '" + str + "'"); } }
/** * Tries to parse a currency from the input string. * <p> * Parsing is case insensitive. * * @param str the string to parse, may be null * @return the parsed currency, empty if unable to parse */ public static Optional<Currency> tryParseCurrency(String str) { if (str != null && str.length() == 3 && CURRENCY_MATCHER.matchesAllOf(str)) { try { return Optional.of(Currency.parse(str)); } catch (RuntimeException ex) { // ignore } } return Optional.empty(); }
/** * Parses a currency pair from a string with format AAA/BBB. * <p> * The parsed format is '${baseCurrency}/${counterCurrency}'. * Currency parsing is case insensitive. * * @param pairStr the currency pair as a string AAA/BBB * @return the currency pair * @throws IllegalArgumentException if the pair cannot be parsed */ @FromString public static CurrencyPair parse(String pairStr) { ArgChecker.notNull(pairStr, "pairStr"); Matcher matcher = REGEX_FORMAT.matcher(pairStr.toUpperCase(Locale.ENGLISH)); if (!matcher.matches()) { throw new IllegalArgumentException("Invalid currency pair: " + pairStr); } Currency base = Currency.parse(matcher.group(1)); Currency counter = Currency.parse(matcher.group(2)); return new CurrencyPair(base, counter); }
@Test(dataProvider = "parseBad", expectedExceptions = IllegalArgumentException.class) public void test_parse_String_bad(String input) { Currency.parse(input); }
/** * Parses a rate from a string with format AAA/BBB RATE. * <p> * The parsed format is '${baseCurrency}/${counterCurrency} ${rate}'. * Currency parsing is case insensitive. * * @param rateStr the rate as a string AAA/BBB RATE * @return the FX rate * @throws IllegalArgumentException if the FX rate cannot be parsed */ public static FxRate parse(String rateStr) { ArgChecker.notNull(rateStr, "rateStr"); Matcher matcher = REGEX_FORMAT.matcher(rateStr.toUpperCase(Locale.ENGLISH)); if (!matcher.matches()) { throw new IllegalArgumentException("Invalid rate: " + rateStr); } try { Currency base = Currency.parse(matcher.group(1)); Currency counter = Currency.parse(matcher.group(2)); double rate = Double.parseDouble(matcher.group(3)); return new FxRate(CurrencyPair.of(base, counter), rate); } catch (RuntimeException ex) { throw new IllegalArgumentException("Unable to parse rate: " + rateStr, ex); } }
/** * Parses the string to produce a {@link Money}. * <p> * This parses the {@code toString} format of '${currency} ${amount}'. * * @param amountStr the amount string * @return the currency amount * @throws IllegalArgumentException if the amount cannot be parsed */ @FromString public static Money parse(String amountStr) { ArgChecker.notNull(amountStr, "amountStr"); List<String> split = Splitter.on(' ').splitToList(amountStr); if (split.size() != 2) { throw new IllegalArgumentException("Unable to parse amount, invalid format: " + amountStr); } try { Currency cur = Currency.parse(split.get(0)); return new Money(cur, new BigDecimal(split.get(1))); } catch (RuntimeException ex) { throw new IllegalArgumentException("Unable to parse amount: " + amountStr, ex); } }
/** * Parses the string to produce a {@code CurrencyAmount}. * <p> * This parses the {@code toString} format of '${currency} ${amount}'. * * @param amountStr the amount string * @return the currency amount * @throws IllegalArgumentException if the amount cannot be parsed */ @FromString public static CurrencyAmount parse(String amountStr) { ArgChecker.notNull(amountStr, "amountStr"); List<String> split = Splitter.on(' ').splitToList(amountStr); if (split.size() != 2) { throw new IllegalArgumentException("Unable to parse amount, invalid format: " + amountStr); } try { Currency cur = Currency.parse(split.get(0)); double amount = Double.parseDouble(split.get(1)); return new CurrencyAmount(cur, amount); } catch (RuntimeException ex) { throw new IllegalArgumentException("Unable to parse amount: " + amountStr, ex); } }
public void test_parse_String_lowerCase() { Currency test = Currency.parse("gbp"); assertEquals(test.getCode(), "GBP"); assertSame(test, Currency.GBP); }
public void test_parse_String() { Currency test = Currency.parse("GBP"); assertEquals(test.getCode(), "GBP"); assertSame(test, Currency.GBP); }
public void test_parse_String_unknownCurrencyCreated() { Currency test = Currency.parse("zyx"); assertEquals(test.getCode(), "ZYX"); assertEquals(test.getMinorUnitDigits(), 0); assertSame(test, Currency.of("ZYX")); }
Optional<LocalDate> startDateOpt = row.findValue(START_DATE_FIELD).map(s -> LoaderUtils.parseDate(s)); Optional<LocalDate> endDateOpt = row.findValue(END_DATE_FIELD).map(s -> LoaderUtils.parseDate(s)); Optional<Currency> currencyOpt = row.findValue(CURRENCY_FIELD).map(s -> Currency.parse(s)); Optional<DayCount> dayCountOpt = row.findValue(DAY_COUNT_FIELD).map(s -> LoaderUtils.parseDayCount(s)); BusinessDayConvention dateCnv = row.findValue(DATE_ADJ_CNV_FIELD)
private static FxIndex parseFxIndex(CsvRow row) { String name = row.getField(NAME_FIELD); Currency baseCurrency = Currency.parse(row.getField(BASE_CURRENCY_FIELD)); Currency counterCurrency = Currency.parse(row.getField(COUNTER_CURRENCY_FIELD)); HolidayCalendarId fixingCal = HolidayCalendarId.of(row.getField(FIXING_CALENDAR_FIELD)); int maturityDays = Integer.parseInt(row.getField(MATURITY_DAYS_FIELD)); HolidayCalendarId maturityCal = HolidayCalendarId.of(row.getField(MATURITY_CALENDAR_FIELD)); // build result return ImmutableFxIndex.builder() .name(name) .currencyPair(CurrencyPair.of(baseCurrency, counterCurrency)) .fixingCalendar(fixingCal) .maturityDateOffset(DaysAdjustment.ofBusinessDays(maturityDays, maturityCal)) .build(); }
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 PriceIndex parsePriceIndex(CsvRow row) { String name = row.getField(NAME_FIELD); Currency currency = Currency.parse(row.getField(CURRENCY_FIELD)); Country region = Country.of(row.getField(COUNTRY_FIELD)); boolean active = Boolean.parseBoolean(row.getField(ACTIVE_FIELD)); Frequency frequency = Frequency.parse(row.getField(PUBLICATION_FREQUENCY_FIELD)); // build result return ImmutablePriceIndex.builder() .name(name) .currency(currency) .region(region) .active(active) .publicationFrequency(frequency) .build(); }
private static OvernightIndex parseOvernightIndex(CsvRow row) { String name = row.getValue(NAME_FIELD); Currency currency = Currency.parse(row.getValue(CURRENCY_FIELD)); boolean active = Boolean.parseBoolean(row.getValue(ACTIVE_FIELD)); DayCount dayCount = DayCount.of(row.getValue(DAY_COUNT_FIELD)); HolidayCalendarId fixingCal = HolidayCalendarId.of(row.getValue(FIXING_CALENDAR_FIELD)); int publicationDays = Integer.parseInt(row.getValue(PUBLICATION_DAYS_FIELD)); int effectiveDays = Integer.parseInt(row.getValue(EFFECTIVE_DAYS_FIELD)); DayCount fixedLegDayCount = DayCount.of(row.getValue(FIXED_LEG_DAY_COUNT)); // build result return ImmutableOvernightIndex.builder() .name(name) .currency(currency) .active(active) .dayCount(dayCount) .fixingCalendar(fixingCal) .publicationDateOffset(publicationDays) .effectiveDateOffset(effectiveDays) .defaultFixedLegDayCount(fixedLegDayCount) .build(); }
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); }
private static IborIndex parseIborIndex(CsvRow row) { String name = row.getValue(NAME_FIELD); Currency currency = Currency.parse(row.getValue(CURRENCY_FIELD)); boolean active = Boolean.parseBoolean(row.getValue(ACTIVE_FIELD)); DayCount dayCount = DayCount.of(row.getValue(DAY_COUNT_FIELD));