DaysFromEndOfMonthExpiryAdjuster implements ExchangeTradedInstrumentExpiryCalculator { @Override public LocalDate getExpiryDate(final int n, final LocalDate today, final Calendar holidayCalendar) { ArgumentChecker.isTrue(n > 0, "n must be greater than 0; have {}", n); ArgumentChecker.notNull(today, "today"); ArgumentChecker.notNull(holidayCalendar, "holidayCalendar"); LocalDate date = today.with(TemporalAdjusters.lastDayOfMonth()); while (!WEEKEND.isWorkingDay(date)) { date = date.minusDays(1); } int i = 0; while (i < _nWorkingDays) { date = date.minusDays(1); if (WEEKEND.isWorkingDay(date)) { i++; } } if (today.isAfter(date)) { return getExpiryDate(n, today.plusMonths(1).with(TemporalAdjusters.firstDayOfMonth()), WEEKEND); } date = today.plusMonths(n - 1).with(TemporalAdjusters.lastDayOfMonth()); while (!WEEKEND.isWorkingDay(date)) { date = date.minusDays(1); } i = 0; while (i < _nWorkingDays) { date = date.minusDays(1); if (WEEKEND.isWorkingDay(date)) { i++; } } return date; } DaysFromEndOfMonthExpiryAdjuster(final int nWorkingDays); @Override LocalDate getExpiryDate(final int n, final LocalDate today, final Calendar holidayCalendar); @Override LocalDate getExpiryMonth(final int n, final LocalDate today); @Override String getName(); @Override int hashCode(); @Override boolean equals(final Object obj);  }
@Test public void testOneDayAdjuster() { final DaysFromEndOfMonthExpiryAdjuster adjuster = new DaysFromEndOfMonthExpiryAdjuster(1); LocalDate date = LocalDate.of(2014, 2, 14); LocalDate expected = LocalDate.of(2014, 2, 27); assertEquals(expected, adjuster.getExpiryDate(1, date, CALENDAR)); expected = LocalDate.of(2014, 3, 28); assertEquals(expected, adjuster.getExpiryDate(2, date, CALENDAR)); expected = LocalDate.of(2014, 4, 29); assertEquals(expected, adjuster.getExpiryDate(3, date, CALENDAR)); expected = LocalDate.of(2014, 6, 27); assertEquals(expected, adjuster.getExpiryDate(5, date, CALENDAR)); date = LocalDate.of(2014, 1, 31); expected = LocalDate.of(2014, 2, 27); assertEquals(expected, adjuster.getExpiryDate(1, date, CALENDAR)); expected = LocalDate.of(2014, 3, 28); assertEquals(expected, adjuster.getExpiryDate(2, date, CALENDAR)); expected = LocalDate.of(2014, 4, 29); assertEquals(expected, adjuster.getExpiryDate(3, date, CALENDAR)); expected = LocalDate.of(2014, 6, 27); assertEquals(expected, adjuster.getExpiryDate(5, date, CALENDAR)); }
@Test public void testFiveDayAdjuster() { final DaysFromEndOfMonthExpiryAdjuster adjuster = new DaysFromEndOfMonthExpiryAdjuster(5); LocalDate date = LocalDate.of(2014, 2, 14); LocalDate expected = LocalDate.of(2014, 2, 21); assertEquals(expected, adjuster.getExpiryDate(1, date, CALENDAR)); expected = LocalDate.of(2014, 3, 24); assertEquals(expected, adjuster.getExpiryDate(2, date, CALENDAR)); expected = LocalDate.of(2014, 4, 23); assertEquals(expected, adjuster.getExpiryDate(3, date, CALENDAR)); expected = LocalDate.of(2014, 6, 23); assertEquals(expected, adjuster.getExpiryDate(5, date, CALENDAR)); date = LocalDate.of(2014, 1, 31); expected = LocalDate.of(2014, 2, 21); assertEquals(expected, adjuster.getExpiryDate(1, date, CALENDAR)); expected = LocalDate.of(2014, 3, 24); assertEquals(expected, adjuster.getExpiryDate(2, date, CALENDAR)); expected = LocalDate.of(2014, 4, 23); assertEquals(expected, adjuster.getExpiryDate(3, date, CALENDAR)); expected = LocalDate.of(2014, 6, 23); assertEquals(expected, adjuster.getExpiryDate(5, date, CALENDAR)); }