/*
 * 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.sdk.testing;

import java.util.ArrayList;
import java.util.List;
import org.apache.beam.sdk.annotations.Internal;
import org.apache.beam.sdk.transforms.DoFn;
import org.apache.beam.sdk.transforms.windowing.BoundedWindow;
import org.apache.beam.sdk.transforms.windowing.PaneInfo;
import org.apache.beam.sdk.values.CausedByDrain;
import org.apache.beam.sdk.values.OutputBuilder;
import org.apache.beam.sdk.values.WindowedValues;
import org.joda.time.Instant;

/**
 * An implementation of {@link DoFn.OutputReceiver} that naively collects all output values.
 *
 * <p>Because this API is crude and not designed to be very general, it is for internal use only and
 * will be changed arbitrarily.
 */
@Internal
public class TestOutputReceiver<T> implements DoFn.OutputReceiver<T> {
  private final List<T> records = new ArrayList<>();

  // To simplify testing of a DoFn, we want to be able to collect their outputs even
  // when no window is provided (because processElement is called with only a value in testing).
  private static final BoundedWindow fakeWindow =
      new BoundedWindow() {
        @Override
        public Instant maxTimestamp() {
          return BoundedWindow.TIMESTAMP_MIN_VALUE;
        }
      };

  @Override
  public OutputBuilder<T> builder(T value) {
    return WindowedValues.<T>builder()
        .setValue(value)
        .setWindow(fakeWindow)
        .setPaneInfo(PaneInfo.NO_FIRING)
        .setTimestamp(BoundedWindow.TIMESTAMP_MIN_VALUE)
        .setCausedByDrain(CausedByDrain.NORMAL)
        .setReceiver(windowedValue -> records.add(windowedValue.getValue()));
  }

  public List<T> getOutputs() {
    return records;
  }
}
