DateRangeCalculator { static Period parsePeriod(String periodText) { if (periodText == null) { throw new NullPointerException("periodText"); } periodText = periodText.trim(); if (periodText.isEmpty()) { throw new IllegalArgumentException("periodText is empty"); } Matcher m = DAY_REGEX.matcher(periodText); if (m.matches()) { return new DayPeriod(Integer.parseInt(m.group(1))); } m = INT_REGEX.matcher(periodText); if (m.matches()) { return new DayPeriod(Integer.parseInt(m.group(1))); } m = WEEK_REGEX.matcher(periodText); if (m.matches()) { return new WeekPeriod(Integer.parseInt(m.group(1))); } m = MONTH_REGEX.matcher(periodText); if (m.matches()) { return new MonthPeriod(Integer.parseInt(m.group(1))); } m = YEAR_REGEX.matcher(periodText); if (m.matches()) { return new YearPeriod(Integer.parseInt(m.group(1))); } if (periodText.equals("-7")) { return new WeekPeriod(1); } if (periodText.equals("-10")) { return new TenDaysPeriod(1); } if (periodText.equals("-30")) { return new MonthPeriod(1); } throw new IllegalArgumentException("can not parse periodText: " + periodText); }  static List<DateRange> fromDateList(Date... dateList); static List<DateRange> fromMinMax(Date minDate, Date maxDate, String steppingPeriodLength, String compositingPeriodLength);  }
@Test public void testParsePeriod() throws Exception { assertEquals("DayPeriod{2}", DateRangeCalculator.parsePeriod("2d").toString()); assertEquals("DayPeriod{2}", DateRangeCalculator.parsePeriod("2").toString()); try { DateRangeCalculator.parsePeriod("0"); fail(); } catch (IllegalArgumentException ignored) { } try { DateRangeCalculator.parsePeriod("-1"); fail(); } catch (IllegalArgumentException ignored) { } try { DateRangeCalculator.parsePeriod("2.5"); fail(); } catch (IllegalArgumentException ignored) { } assertEquals("WeekPeriod{1}", DateRangeCalculator.parsePeriod("1w").toString()); assertEquals("WeekPeriod{1}", DateRangeCalculator.parsePeriod("-7").toString()); assertEquals("WeekPeriod{3}", DateRangeCalculator.parsePeriod("3w").toString()); assertEquals("MonthPeriod{1}", DateRangeCalculator.parsePeriod("1m").toString()); assertEquals("MonthPeriod{1}", DateRangeCalculator.parsePeriod("-30").toString()); assertEquals("MonthPeriod{3}", DateRangeCalculator.parsePeriod("3m").toString()); assertEquals("YearPeriod{1}", DateRangeCalculator.parsePeriod("1y").toString()); assertEquals("YearPeriod{5}", DateRangeCalculator.parsePeriod("5y").toString()); }
@Test public void testNext() throws ParseException { DateRangeCalculator.Period period = DateRangeCalculator.parsePeriod("-10"); GregorianCalendar cal = new GregorianCalendar(); cal.setTime(DATE_FORMAT.parse("2016-01-01")); period.next(cal); assertEquals("2016-01-11", DATE_FORMAT.format(cal.getTime())); period.next(cal); assertEquals("2016-01-21", DATE_FORMAT.format(cal.getTime())); period.next(cal); assertEquals("2016-02-01", DATE_FORMAT.format(cal.getTime())); period.next(cal); assertEquals("2016-02-11", DATE_FORMAT.format(cal.getTime())); period.next(cal); assertEquals("2016-02-21", DATE_FORMAT.format(cal.getTime())); period.next(cal); assertEquals("2016-03-01", DATE_FORMAT.format(cal.getTime())); period.next(cal); assertEquals("2016-03-11", DATE_FORMAT.format(cal.getTime())); }