GoldFutureOptionExpiryCalculator implements ExchangeTradedInstrumentExpiryCalculator { @Override public LocalDate getExpiryDate(final int n, final LocalDate today, final Calendar holidayCalendar) { ArgumentChecker.isTrue(n > 0, "n must be greater than zero; have {}", n); ArgumentChecker.notNull(today, "today"); ArgumentChecker.notNull(holidayCalendar, "holiday calendar"); final LocalDate futuresExpiry = GoldFutureExpiryCalculator.getInstance().getExpiryMonth(n + 1, today); int nBusinessDays = 4; LocalDate date = futuresExpiry.minusMonths(1).with(LAST_DAY_ADJUSTER); if (holidayCalendar.isWorkingDay(date)) { nBusinessDays--; } while (nBusinessDays > 0) { date = date.minusDays(1); if (holidayCalendar.isWorkingDay(date)) { nBusinessDays--; } } if (!holidayCalendar.isWorkingDay(date.plusDays(1))) { date = date.minusDays(1); while (!holidayCalendar.isWorkingDay(date)) { date = date.minusDays(1); } } return date; } private  GoldFutureOptionExpiryCalculator(); static GoldFutureOptionExpiryCalculator getInstance(); @Override LocalDate getExpiryDate(final int n, final LocalDate today, final Calendar holidayCalendar); @Override LocalDate getExpiryMonth(final int n, final LocalDate today); @Override String getName(); static final String NAME; }
@Test public void getExpiryDate() { assertEquals(LocalDate.of(2013, 2, 25), EXPIRY_CALC.getExpiryDate(1, DATE, WEEKEND_CALENDAR)); assertEquals(LocalDate.of(2013, 3, 26), EXPIRY_CALC.getExpiryDate(2, DATE, WEEKEND_CALENDAR)); assertEquals(LocalDate.of(2013, 5, 28), EXPIRY_CALC.getExpiryDate(3, DATE, WEEKEND_CALENDAR)); assertEquals(LocalDate.of(2013, 7, 25), EXPIRY_CALC.getExpiryDate(4, DATE, WEEKEND_CALENDAR)); assertEquals(LocalDate.of(2013, 9, 25), EXPIRY_CALC.getExpiryDate(5, DATE, WEEKEND_CALENDAR)); assertEquals(LocalDate.of(2013, 11, 26), EXPIRY_CALC.getExpiryDate(6, DATE, WEEKEND_CALENDAR)); }