/*
 *  Copyright (c) 2012-2013 Malhar, Inc.
 *  All Rights Reserved.
 */
package com.malhartech.lib.stream;

import com.malhartech.api.Context.OperatorContext;
import com.malhartech.api.DefaultInputPort;
import com.malhartech.api.DefaultOutputPort;
import com.malhartech.api.Operator;
import com.malhartech.api.Operator.Unifier;

/**
 * Counter counts the number of tuples delivered to it in each window and emits the count.
 *
 * @author Chetan Narsude <chetan@malhar-inc.com>
 */
public class Counter implements Operator, Unifier<Integer>
{
  public final transient DefaultInputPort<Object> input = new DefaultInputPort<Object>(this)
  {
    @Override
    public void process(Object tuple)
    {
      count++;
    }

  };
  public final transient DefaultOutputPort<Integer> output = new DefaultOutputPort<Integer>(this)
  {
    @Override
    public Unifier<Integer> getUnifier()
    {
      return Counter.this;
    }

  };

  @Override
  public void beginWindow(long windowId)
  {
    count = 0;
  }

  @Override
  public void merge(Integer tuple)
  {
    count += tuple;
  }

  @Override
  public void endWindow()
  {
    output.emit(count);
  }

  @Override
  public void setup(OperatorContext context)
  {
  }

  @Override
  public void teardown()
  {
  }

  private transient int count;
}

@@ -515,16 +515,18 @@
 Operator
+//
 , Unifie
@@ -814,32 +814,34 @@
 eger%3E(this)%0A  %7B%0A
+//
     @Override%0A  
@@ -830,32 +830,34 @@
 //    @Override%0A
+//
     public Unifi
@@ -881,22 +881,26 @@
 ifier()%0A
+//
     %7B%0A
+//
       re
@@ -918,16 +918,18 @@
 r.this;%0A
+//
     %7D%0A%0A 
@@ -1006,24 +1006,26 @@
 t = 0;%0A  %7D%0A%0A
+//
   @Override%0A

Disable the unifier until the hint is passed to the StramChild about the type of the unifier.