public MonthsWindows withStartingMonth(int year, int month) { return new MonthsWindows( number, dayOfMonth, new DateTime(year, month, 1, 0, 0, timeZone), timeZone); }
@Override public void verifyCompatibility(WindowFn<?, ?> other) throws IncompatibleWindowException { if (!this.isCompatible(other)) { throw new IncompatibleWindowException( other, String.format( "Only %s objects with the same number of months, " + "day of month, start date and time zone are compatible.", MonthsWindows.class.getSimpleName())); } }
@Test public void testMultiMonths() throws Exception { Map<IntervalWindow, Set<String>> expected = new HashMap<>(); final List<Long> timestamps = Arrays.asList( makeTimestamp(2014, 3, 5, 0, 0).getMillis(), makeTimestamp(2014, 10, 4, 23, 59).getMillis(), makeTimestamp(2014, 10, 5, 0, 0).getMillis(), makeTimestamp(2015, 3, 1, 0, 0).getMillis(), makeTimestamp(2016, 1, 5, 0, 0).getMillis(), makeTimestamp(2016, 1, 31, 5, 5).getMillis()); expected.put( new IntervalWindow(makeTimestamp(2014, 3, 5, 0, 0), makeTimestamp(2014, 10, 5, 0, 0)), set(timestamps.get(0), timestamps.get(1))); expected.put( new IntervalWindow(makeTimestamp(2014, 10, 5, 0, 0), makeTimestamp(2015, 5, 5, 0, 0)), set(timestamps.get(2), timestamps.get(3))); expected.put( new IntervalWindow(makeTimestamp(2015, 12, 5, 0, 0), makeTimestamp(2016, 7, 5, 0, 0)), set(timestamps.get(4), timestamps.get(5))); assertEquals( expected, runWindowFn( CalendarWindows.months(7).withStartingMonth(2014, 3).beginningOnDay(5), timestamps)); }
@Test public void testDisplayData() { DateTimeZone timeZone = DateTimeZone.forID("America/Los_Angeles"); Instant jan1 = new DateTime(1990, 1, 1, 0, 0, timeZone).toInstant(); CalendarWindows.DaysWindows daysWindow = CalendarWindows.days(5).withStartingDay(1990, 1, 1).withTimeZone(timeZone); DisplayData daysDisplayData = DisplayData.from(daysWindow); assertThat(daysDisplayData, hasDisplayItem("numDays", 5)); assertThat(daysDisplayData, hasDisplayItem("startDate", jan1)); CalendarWindows.MonthsWindows monthsWindow = CalendarWindows.months(2).withStartingMonth(1990, 1).withTimeZone(timeZone); DisplayData monthsDisplayData = DisplayData.from(monthsWindow); assertThat(monthsDisplayData, hasDisplayItem("numMonths", 2)); assertThat(monthsDisplayData, hasDisplayItem("startDate", jan1)); CalendarWindows.YearsWindows yearsWindow = CalendarWindows.years(4).withStartingYear(1990).withTimeZone(timeZone); DisplayData yearsDisplayData = DisplayData.from(yearsWindow); assertThat(yearsDisplayData, hasDisplayItem("numYears", 4)); assertThat(yearsDisplayData, hasDisplayItem("startDate", jan1)); } }
@Test public void testDefaultWindowMappingFn() { MonthsWindows windowFn = CalendarWindows.months(2); WindowMappingFn<?> mapping = windowFn.getDefaultWindowMappingFn(); assertThat( mapping.getSideInputWindow( new BoundedWindow() { @Override public Instant maxTimestamp() { return new Instant(100L); } }), equalTo(windowFn.assignWindow(new Instant(100L)))); assertThat(mapping.maximumLookback(), equalTo(Duration.ZERO)); }
public MonthsWindows beginningOnDay(int dayOfMonth) { return new MonthsWindows(number, dayOfMonth, startDate, timeZone); }
@Test public void testDefaultWindowMappingFnGlobal() { MonthsWindows windowFn = CalendarWindows.months(2); WindowMappingFn<?> mapping = windowFn.getDefaultWindowMappingFn(); thrown.expect(IllegalArgumentException.class); mapping.getSideInputWindow(GlobalWindow.INSTANCE); }
public MonthsWindows withTimeZone(DateTimeZone timeZone) { return new MonthsWindows( number, dayOfMonth, startDate.withZoneRetainFields(timeZone), timeZone); }
@Test public void testMonthsCompatibility() throws IncompatibleWindowException { CalendarWindows.MonthsWindows monthsWindows = CalendarWindows.months(10).beginningOnDay(15); monthsWindows.verifyCompatibility(CalendarWindows.months(10).beginningOnDay(15)); thrown.expect(IncompatibleWindowException.class); monthsWindows.verifyCompatibility(CalendarWindows.months(10).beginningOnDay(30)); }
/** * Returns a {@link WindowFn} that windows elements into periods measured by months. * * <p>For example, {@code CalendarWindows.months(8).withStartingMonth(2014, 1).beginningOnDay(10)} * will window elements into 8 month windows where that start on the 10th day of month, and the * first window begins in January 2014. */ public static MonthsWindows months(int number) { return new MonthsWindows(number, 1, DEFAULT_START_DATE, DateTimeZone.UTC); }