/*
 * Licensed to the Apache Software Foundation (ASF) under one
 * or more contributor license agreements.  See the NOTICE file
 * distributed with this work for additional information
 * regarding copyright ownership.  The ASF licenses this file
 * to you under the Apache License, Version 2.0 (the
 * "License"); you may not use this file except in compliance
 * with the License.  You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package org.apache.beam.runners.spark.translation;

import org.apache.beam.sdk.util.WindowedValue;
import org.apache.spark.api.java.function.Function;

/**
 * Helper functions for working with windows.
 */
public final class WindowingHelpers {
  private WindowingHelpers() {
  }

  /**
   * A function for converting a value to a {@link WindowedValue}. The resulting
   * {@link WindowedValue} will be in no windows, and will have the default timestamp
   * and pane.
   *
   * @param <T>   The type of the object.
   * @return A function that accepts an object and returns its {@link WindowedValue}.
   */
  public static <T> Function<T, WindowedValue<T>> windowFunction() {
    return new Function<T, WindowedValue<T>>() {
      @Override
      public WindowedValue<T> call(T t) {
        return WindowedValue.valueInEmptyWindows(t);
      }
    };
  }

  /**
   * A function for extracting the value from a {@link WindowedValue}.
   *
   * @param <T>   The type of the object.
   * @return A function that accepts a {@link WindowedValue} and returns its value.
   */
  public static <T> Function<WindowedValue<T>, T> unwindowFunction() {
    return new Function<WindowedValue<T>, T>() {
      @Override
      public T call(WindowedValue<T> t) {
        return t.getValue();
      }
    };
  }
}

@@ -1085,32 +1085,38 @@
 %7D%0A%0A  /**%0A   * A 
+Spark 
 function for con
@@ -1211,18 +1211,24 @@
 l be in 
-no
+a global
  windows
@@ -1264,16 +1264,23 @@
 imestamp
+ == MIN
 %0A   * an
@@ -1642,20 +1642,20 @@
 ueIn
-Empty
+Global
 Window
-s
 (t);
@@ -1688,16 +1688,22 @@
 %0A   * A 
+Spark 
 function
@@ -2120,11 +2120,1043 @@
   %7D;%0A  %7D
+%0A%0A  /**%0A   * Same as windowFunction but for non-RDD values - not an RDD transformation!%0A   *%0A   * @param %3CT%3E   The type of the object.%0A   * @return A function that accepts an object and returns its %7B@link WindowedValue%7D.%0A   */%0A  public static %3CT%3E com.google.common.base.Function%3CT, WindowedValue%3CT%3E%3E windowValueFunction() %7B%0A    return new com.google.common.base.Function%3CT, WindowedValue%3CT%3E%3E() %7B%0A      @Override%0A      public WindowedValue%3CT%3E apply(T t) %7B%0A        return WindowedValue.valueInGlobalWindow(t);%0A      %7D%0A    %7D;%0A  %7D%0A%0A  /**%0A   * Same as unwindowFunction but for non-RDD values - not an RDD transformation!%0A   *%0A   * @param %3CT%3E   The type of the object.%0A   * @return A function that accepts an object and returns its %7B@link WindowedValue%7D.%0A   */%0A  public static %3CT%3E com.google.common.base.Function%3CWindowedValue%3CT%3E, T%3E unwindowValueFunction() %7B%0A    return new com.google.common.base.Function%3CWindowedValue%3CT%3E, T%3E() %7B%0A      @Override%0A      public T apply(WindowedValue%3CT%3E t) %7B%0A        return t.getValue();%0A      %7D%0A    %7D;%0A  %7D
 %0A%7D%0A

Replace valueInEmptyWindows with valueInGlobalWindow in Spark Function, and add per-value (non-RDD) windowing functions