/** * Returns an adjuster that changes the date. * <p> * The adjuster is intended to be used with the method {@link Temporal#with(TemporalAdjuster)}. * For example: * <pre> * threeDaysLater = date.with(businessDays.adjustBy(3)); * twoDaysEarlier = date.with(businessDays.adjustBy(-2)); * </pre> * * @param amount the number of business days to adjust by * @return the first business day after this one * @throws IllegalArgumentException if the calculation is outside the supported range */ public default TemporalAdjuster adjustBy(int amount) { return TemporalAdjusters.ofDateAdjuster(date -> shift(date, amount)); }
private LocalDate shiftOutOfRange(LocalDate date, int amount) { if (date.getYear() >= 0 && date.getYear() < 10000) { return HolidayCalendar.super.shift(date, amount); } throw new IllegalArgumentException("Date is outside the accepted range (year 0000 to 10,000): " + date); }
public void test_THU_FRI_shift() { assertEquals(HolidayCalendars.THU_FRI.shift(WED_2014_07_09, 2), SUN_2014_07_13); assertEquals(HolidayCalendars.THU_FRI.shift(THU_2014_07_10, 2), SUN_2014_07_13); assertEquals(HolidayCalendars.THU_FRI.shift(FRI_2014_07_11, 2), SUN_2014_07_13); assertEquals(HolidayCalendars.THU_FRI.shift(SAT_2014_07_12, 2), MON_2014_07_14); assertEquals(HolidayCalendars.THU_FRI.shift(FRI_2014_07_18, -2), TUE_2014_07_15); assertEquals(HolidayCalendars.THU_FRI.shift(SAT_2014_07_19, -2), TUE_2014_07_15); assertEquals(HolidayCalendars.THU_FRI.shift(SUN_2014_07_20, -2), WED_2014_07_16); assertEquals(HolidayCalendars.THU_FRI.shift(MON_2014_07_21, -2), SAT_2014_07_19); assertEquals(HolidayCalendars.THU_FRI.shift(WED_2014_07_09, 5), WED_2014_07_16); assertEquals(HolidayCalendars.THU_FRI.shift(WED_2014_07_09, 6), SAT_2014_07_19); assertEquals(HolidayCalendars.THU_FRI.shift(WED_2014_07_16, -5), WED_2014_07_09); assertEquals(HolidayCalendars.THU_FRI.shift(SAT_2014_07_19, -6), WED_2014_07_09); }
@Override public LocalDate calculateEffectiveFromFixing(LocalDate fixingDate, ReferenceData refData) { HolidayCalendar fixingCal = fixingCalendar.resolve(refData); return fixingCal.shift(fixingCal.nextOrSame(fixingDate), effectiveDateOffset); }
@Override public LocalDate calculatePublicationFromFixing(LocalDate fixingDate, ReferenceData refData) { HolidayCalendar fixingCal = fixingCalendar.resolve(refData); return fixingCal.shift(fixingCal.nextOrSame(fixingDate), publicationDateOffset); }
@Override public LocalDate calculateMaturityFromFixing(LocalDate fixingDate, ReferenceData refData) { HolidayCalendar fixingCal = fixingCalendar.resolve(refData); return fixingCal.shift(fixingCal.nextOrSame(fixingDate), effectiveDateOffset + 1); }
public void test_FRI_SAT_shift() { assertEquals(HolidayCalendars.FRI_SAT.shift(THU_2014_07_10, 2), MON_2014_07_14); assertEquals(HolidayCalendars.FRI_SAT.shift(FRI_2014_07_11, 2), MON_2014_07_14); assertEquals(HolidayCalendars.FRI_SAT.shift(SUN_2014_07_13, 2), TUE_2014_07_15); assertEquals(HolidayCalendars.FRI_SAT.shift(MON_2014_07_14, 2), WED_2014_07_16); assertEquals(HolidayCalendars.FRI_SAT.shift(FRI_2014_07_11, -2), WED_2014_07_09); assertEquals(HolidayCalendars.FRI_SAT.shift(SAT_2014_07_12, -2), WED_2014_07_09); assertEquals(HolidayCalendars.FRI_SAT.shift(SUN_2014_07_13, -2), WED_2014_07_09); assertEquals(HolidayCalendars.FRI_SAT.shift(MON_2014_07_14, -2), THU_2014_07_10); assertEquals(HolidayCalendars.FRI_SAT.shift(TUE_2014_07_15, -2), SUN_2014_07_13); assertEquals(HolidayCalendars.FRI_SAT.shift(WED_2014_07_16, -2), MON_2014_07_14); assertEquals(HolidayCalendars.FRI_SAT.shift(THU_2014_07_10, 5), THU_2014_07_17); assertEquals(HolidayCalendars.FRI_SAT.shift(THU_2014_07_10, 6), SUN_2014_07_20); assertEquals(HolidayCalendars.FRI_SAT.shift(THU_2014_07_17, -5), THU_2014_07_10); assertEquals(HolidayCalendars.FRI_SAT.shift(SUN_2014_07_20, -6), THU_2014_07_10); }
@Override public LocalDate calculateFixingFromEffective(LocalDate effectiveDate, ReferenceData refData) { HolidayCalendar fixingCal = fixingCalendar.resolve(refData); return fixingCal.shift(fixingCal.nextOrSame(effectiveDate), -effectiveDateOffset); }
@Override public LocalDate calculateMaturityFromEffective(LocalDate effectiveDate, ReferenceData refData) { HolidayCalendar fixingCal = fixingCalendar.resolve(refData); return fixingCal.shift(fixingCal.nextOrSame(effectiveDate), 1); }
private void assertSatSun(HolidayCalendar test) { assertEquals(test.shift(THU_2014_07_10, 2), MON_2014_07_14); assertEquals(test.shift(FRI_2014_07_11, 2), TUE_2014_07_15); assertEquals(test.shift(SUN_2014_07_13, 2), TUE_2014_07_15); assertEquals(test.shift(MON_2014_07_14, 2), WED_2014_07_16); assertEquals(test.shift(FRI_2014_07_11, -2), WED_2014_07_09); assertEquals(test.shift(SAT_2014_07_12, -2), THU_2014_07_10); assertEquals(test.shift(SUN_2014_07_13, -2), THU_2014_07_10); assertEquals(test.shift(MON_2014_07_14, -2), THU_2014_07_10); assertEquals(test.shift(TUE_2014_07_15, -2), FRI_2014_07_11); assertEquals(test.shift(WED_2014_07_16, -2), MON_2014_07_14); assertEquals(test.shift(FRI_2014_07_11, 5), FRI_2014_07_18); assertEquals(test.shift(FRI_2014_07_11, 6), MON_2014_07_21); assertEquals(test.shift(FRI_2014_07_18, -5), FRI_2014_07_11); assertEquals(test.shift(MON_2014_07_21, -6), FRI_2014_07_11); assertEquals(test.shift(SAT_2014_07_12, 5), FRI_2014_07_18); assertEquals(test.shift(SAT_2014_07_12, -5), MON_2014_07_07); }
/** * Adjusts the date, adding the period in days using the holiday calendar * and then applying the business day adjustment. * <p> * The calculation is performed in two steps. * <p> * Step one, use {@link HolidayCalendar#shift(LocalDate, int)} to add the number of days. * If the holiday calendar is 'None' this will effectively add calendar days. * <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) { LocalDate added = calendar.resolve(refData).shift(date, days); return adjustment.adjust(added, refData); }
@Test(dataProvider = "shift") public void test_shift(LocalDate date, int amount, LocalDate expected) { // 16th, 18th, Sat, Sun HolidayCalendar test = new MockHolCal(); assertEquals(test.shift(date, amount), expected); }
/** * Calculates the maturity date from the effective date. * <p> * The effective date is the date on which the implied deposit starts. * The maturity date is the date on which the implied deposit ends. * <p> * No error is thrown if the input date is not a valid effective date. * Instead, the effective date is moved to the next valid effective date and then processed. * * @param effectiveDate the effective date * @return the maturity date */ public default LocalDate calculateMaturityFromEffective(LocalDate effectiveDate) { return getFixingCalendar().shift(getFixingCalendar().nextOrSame(effectiveDate), 1); }
@Override public LocalDate adjust(LocalDate date) { ArgChecker.notNull(date, "date"); LocalDate wed3 = date.with(TemporalAdjusters.dayOfWeekInMonth(3, DayOfWeek.WEDNESDAY)); return canada.previousOrSame(gblo.shift(wed3, -2)); } },
/** * Calculates the effective date from the fixing date. * <p> * The fixing date is the date on which the index is to be observed. * The effective date is the date on which the implied deposit starts. * <p> * No error is thrown if the input date is not a valid fixing date. * Instead, the fixing date is moved to the next valid fixing date and then processed. * * @param fixingDate the fixing date * @return the effective date */ public default LocalDate calculateEffectiveFromFixing(LocalDate fixingDate) { return getFixingCalendar().shift(getFixingCalendar().nextOrSame(fixingDate), getIndex().getEffectiveDateOffset()); }
/** * Calculates the publication date from the fixing date. * <p> * The fixing date is the date on which the index is to be observed. * The publication date is the date on which the fixed rate is actually published. * <p> * No error is thrown if the input date is not a valid fixing date. * Instead, the fixing date is moved to the next valid fixing date and then processed. * * @param fixingDate the fixing date * @return the publication date */ public default LocalDate calculatePublicationFromFixing(LocalDate fixingDate) { return getFixingCalendar().shift(getFixingCalendar().nextOrSame(fixingDate), getIndex().getPublicationDateOffset()); }
/** * Calculates the maturity date from the fixing date. * <p> * The fixing date is the date on which the index is to be observed. * The maturity date is the date on which the implied deposit ends. * <p> * No error is thrown if the input date is not a valid fixing date. * Instead, the fixing date is moved to the next valid fixing date and then processed. * * @param fixingDate the fixing date * @return the maturity date */ public default LocalDate calculateMaturityFromFixing(LocalDate fixingDate) { return getFixingCalendar().shift(getFixingCalendar().nextOrSame(fixingDate), getIndex().getEffectiveDateOffset() + 1); }
/** * Calculates the fixing date from the effective date. * <p> * The fixing date is the date on which the index is to be observed. * The effective date is the date on which the implied deposit starts. * <p> * No error is thrown if the input date is not a valid effective date. * Instead, the effective date is moved to the next valid effective date and then processed. * * @param effectiveDate the effective date * @return the fixing date */ public default LocalDate calculateFixingFromEffective(LocalDate effectiveDate) { return getFixingCalendar().shift(getFixingCalendar().nextOrSame(effectiveDate), -getIndex().getEffectiveDateOffset()); }
/** * 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); }