/** * Converts an FpML tenor string to a {@code Tenor}. * * @param multiplier the multiplier * @param unit the unit * @return the tenor * @throws IllegalArgumentException if the tenor is not known */ public Tenor convertIndexTenor(String multiplier, String unit) { String periodStr = multiplier + unit; Tenor tenor = TENOR_MAP.get(periodStr); return tenor != null ? tenor : Tenor.parse(periodStr); }
/** * Parses a tenor from the input string. * <p> * A tenor cannot be zero or negative. * * @param str the string to parse * @return the parsed value * @throws IllegalArgumentException if the string cannot be parsed */ public static Tenor parseTenor(String str) { try { return Tenor.parse(str); } catch (DateTimeParseException ex) { throw new IllegalArgumentException("Unknown tenor format: " + str); } }
/** * Tries to parse a tenor from the input string. * <p> * Parsing is case insensitive. * * @param str the string to parse, may be null * @return the parsed tenor, empty if unable to parse */ public static Optional<Tenor> tryParseTenor(String str) { if (str != null && str.length() > 1) { try { return Optional.of(Tenor.parse(str)); } catch (RuntimeException ex) { // ignore } } return Optional.empty(); }
@Test(dataProvider = "parseBad", expectedExceptions = IllegalArgumentException.class) public void test_parse_String_bad(String input) { Tenor.parse(input); }
@Test(dataProvider = "parseGood") public void test_parse_String_good_noP(String input, Tenor expected) { assertEquals(Tenor.parse(input), expected); }
@Test(dataProvider = "parseGood") public void test_parse_String_good_withP(String input, Tenor expected) { assertEquals(Tenor.parse("P" + input), expected); }
public void test_parse_String_roundTrip() { assertEquals(Tenor.parse(TENOR_10M.toString()), TENOR_10M); }
private static Period parseInflationLag(Optional<String> strOpt, Currency currency) { if (!strOpt.isPresent()) { if (Currency.GBP.equals(currency)) { return Period.ofMonths(2); } return Period.ofMonths(3); } String str = strOpt.get(); Integer months = Ints.tryParse(str); if (months != null) { return Period.ofMonths(months); } return Tenor.parse(str).getPeriod(); }
private static SwapIndex parseSwapIndex(CsvRow row) { String name = row.getField(NAME_FIELD); boolean active = Boolean.parseBoolean(row.getField(ACTIVE_FIELD)); FixedIborSwapConvention convention = FixedIborSwapConvention.of(row.getField(CONVENTION_FIELD)); Tenor tenor = Tenor.parse(row.getField(TENOR_FIELD)); LocalTime time = LocalTime.parse(row.getField(FIXING_TIME_FIELD), TIME_FORMAT); ZoneId zoneId = ZoneId.of(row.getField(FIXING_ZONE_FIELD)); // build result return ImmutableSwapIndex.builder() .name(name) .active(active) .fixingTime(time) .fixingZone(zoneId) .template(FixedIborSwapTemplate.of(tenor, convention)) .build(); }
HolidayCalendarId offsetCal = HolidayCalendarId.of(row.getValue(OFFSET_CALENDAR_FIELD)); HolidayCalendarId effectiveCal = HolidayCalendarId.of(row.getValue(EFFECTIVE_DATE_CALENDAR_FIELD)); Tenor tenor = Tenor.parse(row.getValue(TENOR_FIELD)); LocalTime time = LocalTime.parse(row.getValue(FIXING_TIME_FIELD), TIME_FORMAT); ZoneId zoneId = ZoneId.of(row.getValue(FIXING_ZONE_FIELD));