RussellFutureExpiryCalculator implements ExchangeTradedInstrumentExpiryCalculator { @Override public LocalDate getExpiryDate(int n, LocalDate today, Calendar holidayCalendar) { ArgumentChecker.notNegativeOrZero(n, "nth expiry"); ArgumentChecker.notNull(today, "date"); ArgumentChecker.notNull(holidayCalendar, "holidayCalendar"); LocalDate thirdFriday = getThirdFriday(today, holidayCalendar); if (today.isAfter(thirdFriday)) { thirdFriday = getThirdFriday(today.plusMonths(1), holidayCalendar); } int nQuartersRemaining = QUARTERLY_CYCLE_MONTHS.contains(Month.from(thirdFriday)) ? n - 1 : n; if (nQuartersRemaining == 0) { return thirdFriday; } return getQuarterlyExpiry(nQuartersRemaining, thirdFriday, holidayCalendar); } private  RussellFutureExpiryCalculator(); static RussellFutureExpiryCalculator getInstance(); @Override /** * Quarterly expiries along March cycle * @param nthFuture nth future * @param valDate The date from which to start * @return the expiry date of the nth option */ LocalDate getExpiryDate(int n, LocalDate today, Calendar holidayCalendar); @Override LocalDate getExpiryMonth(int n, LocalDate today); @Override String getName();  }
@Test(expectedExceptions = IllegalArgumentException.class) public void testNegativeN() { CALCULATOR.getExpiryDate(-1, AUGUST, MY_CALENDAR); }
@Test(expectedExceptions = IllegalArgumentException.class) public void testZeroN() { CALCULATOR.getExpiryDate(0, AUGUST, MY_CALENDAR); }
@Test(expectedExceptions = IllegalArgumentException.class) public void testNullDate() { CALCULATOR.getExpiryDate(1, null, MY_CALENDAR); }
@Test(expectedExceptions = IllegalArgumentException.class) public void testNullCalendar() { CALCULATOR.getExpiryDate(2, AUGUST, null); }
@Test public void testCases() { assertEquals(LocalDate.of(2012, 9, 21), CALCULATOR.getExpiryDate(1, SEPTEMBER_START, WEEKEND_CALENDAR)); assertEquals(LocalDate.of(2012, 9, 21), CALCULATOR.getExpiryDate(1, SEPTEMBER_EXPIRY, WEEKEND_CALENDAR)); assertEquals(LocalDate.of(2012, 12, 21), CALCULATOR.getExpiryDate(1, SEPTEMBER_EXPIRY, MY_CALENDAR)); assertEquals(LocalDate.of(2012, 12, 21), CALCULATOR.getExpiryDate(1, SEPTEMBER_END, MY_CALENDAR)); assertEquals(LocalDate.of(2012, 12, 21), CALCULATOR.getExpiryDate(2, SEPTEMBER_START, WEEKEND_CALENDAR)); assertEquals(LocalDate.of(2013, 3, 14), CALCULATOR.getExpiryDate(2, SEPTEMBER_END, MY_CALENDAR)); assertEquals(LocalDate.of(2013, 3, 15), CALCULATOR.getExpiryDate(3, SEPTEMBER_EXPIRY, WEEKEND_CALENDAR)); assertEquals(LocalDate.of(2013, 6, 21), CALCULATOR.getExpiryDate(3, SEPTEMBER_EXPIRY, MY_CALENDAR)); assertEquals(LocalDate.of(2013, 12, 20), CALCULATOR.getExpiryDate(6, SEPTEMBER_START, MY_CALENDAR)); assertEquals(LocalDate.of(2014, 3, 21), CALCULATOR.getExpiryDate(6, SEPTEMBER_END, MY_CALENDAR)); assertEquals(LocalDate.of(2012, 9, 21), CALCULATOR.getExpiryDate(1, AUGUST, WEEKEND_CALENDAR)); assertEquals(LocalDate.of(2012, 9, 20), CALCULATOR.getExpiryDate(1, AUGUST, MY_CALENDAR)); assertEquals(LocalDate.of(2012, 12, 21), CALCULATOR.getExpiryDate(2, AUGUST, WEEKEND_CALENDAR)); assertEquals(LocalDate.of(2013, 12, 20), CALCULATOR.getExpiryDate(6, AUGUST, MY_CALENDAR)); assertEquals(LocalDate.of(2012, 12, 21), CALCULATOR.getExpiryDate(1, OCTOBER, WEEKEND_CALENDAR)); assertEquals(LocalDate.of(2012, 12, 21), CALCULATOR.getExpiryDate(1, OCTOBER, MY_CALENDAR)); assertEquals(LocalDate.of(2013, 3, 15), CALCULATOR.getExpiryDate(2, OCTOBER, WEEKEND_CALENDAR)); assertEquals(LocalDate.of(2013, 3, 14), CALCULATOR.getExpiryDate(2, OCTOBER, MY_CALENDAR)); assertEquals(LocalDate.of(2014, 3, 21), CALCULATOR.getExpiryDate(6, OCTOBER, MY_CALENDAR)); }