com.google.cloud.dataflow.sdk.transforms.windowing

## Class WindowFn<T,W extends BoundedWindow>

- java.lang.Object
- - com.google.cloud.dataflow.sdk.transforms.windowing.WindowFn<T,W>

Type Parameters:
  `T` - type of elements being windowed
  `W` - `BoundedWindow` subclass used to represent the windows used by this `WindowFn`

All Implemented Interfaces:
  HasDisplayData, Serializable

Direct Known Subclasses:
  InvalidWindows, NonMergingWindowFn, Sessions

    public abstract class WindowFn<T,W extends BoundedWindow>
    extends Object
    implements Serializable, HasDisplayData

The argument to the `Window` transform used to assign elements into windows and to determine how windows are merged. See `Window` for more information on how `WindowFn`s are used and for a library of predefined `WindowFn`s.

Users will generally want to use the predefined `WindowFn`s, but it is also possible to create new subclasses.

To create a custom `WindowFn`, inherit from this class and override all required methods. If no merging is required, inherit from `NonMergingWindowFn` instead. If no merging is required and each element is assigned to a single window, inherit from `PartitioningWindowFn`. Inheriting from the most specific subclass will enable more optimizations in the runner.

See Also:
  Serialized Form

### Nested Class Summary

Nested Classes
`class` `WindowFn.AssignContext` Information available when running `assignWindows(com.google.cloud.dataflow.sdk.transforms.windowing.WindowFn<T, W>.AssignContext)`.
`class` `WindowFn.MergeContext` Information available when running `mergeWindows(com.google.cloud.dataflow.sdk.transforms.windowing.WindowFn<T, W>.MergeContext)`.

### Constructor Summary

Constructors
Constructor and Description
`WindowFn()`

### Method Summary

All Methods Instance Methods Abstract Methods Concrete Methods Deprecated Methods
`boolean` `assignsToSingleWindow()` Returns true if this `WindowFn` assigns each element to a single window.
`abstract Collection<W>` `assignWindows(WindowFn.AssignContext c)` Given a timestamp and element, returns the set of windows into which it should be placed.
`Instant` `getOutputTime(Instant inputTimestamp, W window)` Deprecated. Implement `getOutputTimeFn()` to return one of the appropriate `OutputTimeFns`, or a custom `OutputTimeFn` extending `OutputTimeFn.Defaults`.
`OutputTimeFn<? super W>` `getOutputTimeFn()` Provides a default implementation for `WindowingStrategy.getOutputTimeFn()`.
`abstract W` `getSideInputWindow(BoundedWindow window)` Returns the window of the side input corresponding to the given window of the main input.
`abstract boolean` `isCompatible(WindowFn<?,?> other)` Returns whether this performs the same merging as the given `WindowFn`.
`boolean` `isNonMerging()` Returns true if this `WindowFn` never needs to merge any windows.
`abstract void` `mergeWindows(WindowFn.MergeContext c)` Does whatever merging of windows is necessary.
`void` `populateDisplayData(DisplayData.Builder builder)` Register display data for the given transform or component.
`abstract Coder<W>` `windowCoder()` Returns the `Coder` used for serializing the windows used by this windowFn.

### Methods inherited from class java.lang.Object

`clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait`

### Constructor Detail

#### WindowFn

    public WindowFn()

### Method Detail

#### assignWindows

    public abstract Collection<W> assignWindows(WindowFn.AssignContext c)
                                         throws Exception

Given a timestamp and element, returns the set of windows into which it should be placed.

Throws:
  `Exception`

#### mergeWindows

    public abstract void mergeWindows(WindowFn.MergeContext c)
                               throws Exception

Does whatever merging of windows is necessary.

See `MergeOverlappingIntervalWindows.mergeWindows(com.google.cloud.dataflow.sdk.transforms.windowing.WindowFn<?, com.google.cloud.dataflow.sdk.transforms.windowing.IntervalWindow>.MergeContext)` for an example of how to override this method.

Throws:
  `Exception`

#### isCompatible

    public abstract boolean isCompatible(WindowFn<?,?> other)

Returns whether this performs the same merging as the given `WindowFn`.

#### windowCoder

    public abstract Coder<W> windowCoder()

Returns the `Coder` used for serializing the windows used by this windowFn.

#### getSideInputWindow

    public abstract W getSideInputWindow(BoundedWindow window)

Returns the window of the side input corresponding to the given window of the main input.

Authors of custom `WindowFn`s should override this.

#### getOutputTime

    @Deprecated
     @Experimental(value=OUTPUT_TIME)
    public Instant getOutputTime(Instant inputTimestamp,
                                                                               W window)

Deprecated. Implement `getOutputTimeFn()` to return one of the appropriate `OutputTimeFns`, or a custom `OutputTimeFn` extending `OutputTimeFn.Defaults`.

#### getOutputTimeFn

    @Experimental(value=OUTPUT_TIME)
    public OutputTimeFn<? super W> getOutputTimeFn()

Provides a default implementation for `WindowingStrategy.getOutputTimeFn()`. See the full specification there.

If this `WindowFn` doesn't produce overlapping windows, this need not (and probably should not) override any of the default implementations in `OutputTimeFn.Defaults`.

If this `WindowFn` does produce overlapping windows that can be predicted here, it is suggested that the result in later overlapping windows is past the end of earlier windows so that the later windows don't prevent the watermark from progressing past the end of the earlier window.

For example, a timestamp in a sliding window should be moved past the beginning of the next sliding window. See `SlidingWindows.getOutputTimeFn()`.

#### isNonMerging

    public boolean isNonMerging()

Returns true if this `WindowFn` never needs to merge any windows.

#### assignsToSingleWindow

    public boolean assignsToSingleWindow()

Returns true if this `WindowFn` assigns each element to a single window.

#### populateDisplayData

    public void populateDisplayData(DisplayData.Builder builder)

Register display data for the given transform or component.

`populateDisplayData(DisplayData.Builder)` is invoked by Pipeline runners to collect display data via `DisplayData.from(HasDisplayData)`. Implementations may call `super.populateDisplayData(builder)` in order to register display data in the current namespace, but should otherwise use `subcomponent.populateDisplayData(builder)` to use the namespace of the subcomponent.

By default, does not register any display data. Implementors may override this method to provide their own display data.

Specified by:
  `populateDisplayData` in interface `HasDisplayData`
Parameters:
  `builder` - The builder to populate with display data.
See Also:
  `HasDisplayData`