/** * Parses business day convention from the input string. * <p> * Parsing is case insensitive. * It leniently handles a variety of known variants of business day conventions. * * @param str the string to parse * @return the parsed value * @throws IllegalArgumentException if the string cannot be parsed */ public static BusinessDayConvention parseBusinessDayConvention(String str) { return BusinessDayConvention.extendedEnum().findLenient(str) .orElseThrow(() -> new IllegalArgumentException( "Unknown BusinessDayConvention value, must be one of " + BusinessDayConvention.extendedEnum().lookupAllNormalized().keySet() + " but was '" + str + "'")); }
/** * Adjusts the date as necessary if it is not a business day. * <p> * If the date is a business day it will be returned unaltered. * If the date is not a business day, the convention will be applied. * * @param date the date to adjust * @param refData the reference data, used to find the holiday calendar * @return the adjusted date */ public LocalDate adjust(LocalDate date, ReferenceData refData) { HolidayCalendar holCal = calendar.resolve(refData); return convention.adjust(date, holCal); }
@Test(dataProvider = "name") public void test_name(BusinessDayConvention convention, String name) { assertEquals(convention.getName(), name); }
@Test(dataProvider = "name") public void test_of_lookup(BusinessDayConvention convention, String name) { assertEquals(BusinessDayConvention.of(name), convention); }
public void test_of_lookup_notFound() { assertThrowsIllegalArg(() -> BusinessDayConvention.of("Rubbish")); }
/** * Obtains an instance from the specified unique name. * * @param uniqueName the unique name * @return the business convention * @throws IllegalArgumentException if the name is not known */ @FromString public static BusinessDayConvention of(String uniqueName) { return extendedEnum().lookup(uniqueName); }
/** * Resolves this adjustment using the specified reference data, returning an adjuster. * <p> * This returns a {@link DateAdjuster} that performs the same calculation as this adjustment. * It binds the holiday calendar, looked up from the reference data, into the result. * As such, there is no need to pass the reference data in again. * * @param refData the reference data, used to find the holiday calendar * @return the adjuster, bound to a specific holiday calendar */ @Override public DateAdjuster resolve(ReferenceData refData) { HolidayCalendar holCal = calendar.resolve(refData); return date -> convention.adjust(date, holCal); }
public void test_of_lookup_null() { assertThrowsIllegalArg(() -> BusinessDayConvention.of(null)); }
/** * Converts an FpML business day convention string to a {@code BusinessDayConvention}. * * @param fmplBusinessDayConventionName the business day convention name used by FpML * @return the business day convention * @throws IllegalArgumentException if the business day convention is not known */ public BusinessDayConvention convertBusinessDayConvention(String fmplBusinessDayConventionName) { return BusinessDayConvention.extendedEnum().externalNames(ENUM_FPML).lookup(fmplBusinessDayConventionName); }
@Test(dataProvider = "convention") public void test_convention(BusinessDayConvention convention, LocalDate input, LocalDate expected) { assertEquals(convention.adjust(input, HolidayCalendars.SAT_SUN), expected); }
@Test(dataProvider = "name") public void test_extendedEnum(BusinessDayConvention convention, String name) { ImmutableMap<String, BusinessDayConvention> map = BusinessDayConvention.extendedEnum().lookupAll(); assertEquals(map.get(name), convention); }
public void test_nearest() { HolidayCalendar cal = ImmutableHolidayCalendar.of( HolidayCalendarId.of("Test"), ImmutableList.of(MON_2014_07_14), SATURDAY, SUNDAY); assertEquals(NEAREST.adjust(FRI_2014_07_11, cal), FRI_2014_07_11); assertEquals(NEAREST.adjust(SAT_2014_07_12, cal), FRI_2014_07_11); assertEquals(NEAREST.adjust(SUN_2014_07_13, cal), TUE_2014_07_15); assertEquals(NEAREST.adjust(MON_2014_07_14, cal), TUE_2014_07_15); }
@Test(dataProvider = "name") public void test_lenientLookup_standardNames(BusinessDayConvention convention, String name) { assertEquals(BusinessDayConvention.extendedEnum().findLenient(name.toLowerCase(Locale.ENGLISH)).get(), convention); }
/** * Resolves this adjustment using the specified reference data, returning an adjuster. * <p> * This returns a {@link DateAdjuster} that performs the same calculation as this adjustment. * It binds the holiday calendar, looked up from the reference data, into the result. * As such, there is no need to pass the reference data in again. * * @param refData the reference data, used to find the holiday calendar * @return the adjuster, bound to a specific holiday calendar */ @Override public DateAdjuster resolve(ReferenceData refData) { HolidayCalendar holCal = adjustment.getCalendar().resolve(refData); BusinessDayConvention bda = adjustment.getConvention(); return date -> bda.adjust(additionConvention.adjust(date, period, holCal), holCal); }
@Test(dataProvider = "lenient") public void test_lenientLookup_specialNames(String name, BusinessDayConvention convention) { assertEquals(BusinessDayConvention.extendedEnum().findLenient(name.toLowerCase(Locale.ENGLISH)), Optional.of(convention)); }
/** * Resolves this adjustment using the specified reference data, returning an adjuster. * <p> * This returns a {@link DateAdjuster} that performs the same calculation as this adjustment. * It binds the holiday calendar, looked up from the reference data, into the result. * As such, there is no need to pass the reference data in again. * <p> * The resulting adjuster will be {@link #normalized() normalized}. * * @param refData the reference data, used to find the holiday calendar * @return the adjuster, bound to a specific holiday calendar */ @Override public DateAdjuster resolve(ReferenceData refData) { HolidayCalendar holCalAdj = adjustment.getCalendar().resolve(refData); if (calendar == HolidayCalendarIds.NO_HOLIDAYS) { BusinessDayConvention adjustmentConvention = adjustment.getConvention(); return date -> adjustmentConvention.adjust(LocalDateUtils.plusDays(date, days), holCalAdj); } HolidayCalendar holCalAdd = calendar.resolve(refData); BusinessDayConvention adjustmentConvention = adjustment.getConvention(); return date -> adjustmentConvention.adjust(holCalAdd.shift(date, days), holCalAdj); }
.orElse(PeriodAdditionConventions.NONE); BusinessDayConvention tenorBusinessConvention = BusinessDayConvention.extendedEnum().find(row.getField(TENOR_CONVENTION_FIELD)) .orElse(isEndOfMonth(periodAdditionConvention) ? MODIFIED_FOLLOWING : FOLLOWING); BusinessDayAdjustment adj = BusinessDayAdjustment.of(tenorBusinessConvention, effectiveCal);
/** * Adjusts the date, adding the period and then applying the business day adjustment. * <p> * The calculation is performed in two steps. * <p> * Step one, use {@link PeriodAdditionConvention#adjust(LocalDate, Period, HolidayCalendar)} to add the period. * <p> * Step two, use {@link BusinessDayAdjustment#adjust(LocalDate, ReferenceData)} to adjust the result of step one. * * @param date the date to adjust * @param refData the reference data, used to find the holiday calendar * @return the adjusted date */ public LocalDate adjust(LocalDate date, ReferenceData refData) { HolidayCalendar holCal = adjustment.getCalendar().resolve(refData); BusinessDayConvention bda = adjustment.getConvention(); return bda.adjust(additionConvention.adjust(date, period, holCal), holCal); }
/** * Adjusts the date, adding the tenor and then applying the business day adjustment. * <p> * The calculation is performed in two steps. * <p> * Step one, use {@link PeriodAdditionConvention#adjust(LocalDate, Period, HolidayCalendar)} to add the period. * <p> * Step two, use {@link BusinessDayAdjustment#adjust(LocalDate, ReferenceData)} to adjust the result of step one. * * @param date the date to adjust * @param refData the reference data, used to find the holiday calendar * @return the adjusted date */ public LocalDate adjust(LocalDate date, ReferenceData refData) { HolidayCalendar holCal = adjustment.getCalendar().resolve(refData); BusinessDayConvention bda = adjustment.getConvention(); return bda.adjust(additionConvention.adjust(date, tenor.getPeriod(), holCal), holCal); }
/** * Resolves this adjustment using the specified reference data, returning an adjuster. * <p> * This returns a {@link DateAdjuster} that performs the same calculation as this adjustment. * It binds the holiday calendar, looked up from the reference data, into the result. * As such, there is no need to pass the reference data in again. * * @param refData the reference data, used to find the holiday calendar * @return the adjuster, bound to a specific holiday calendar */ @Override public DateAdjuster resolve(ReferenceData refData) { HolidayCalendar holCal = adjustment.getCalendar().resolve(refData); BusinessDayConvention bda = adjustment.getConvention(); Period period = tenor.getPeriod(); return date -> bda.adjust(additionConvention.adjust(date, period, holCal), holCal); }