@Override
    public void apply(
        Integer key, TimeWindow window, Iterable<Integer> values, Collector<Integer> out)
        throws Exception {
      for (Integer i : values) {
        // we need to update this state before emitting elements. Else, the test's main
        // thread will have received all output elements before the state is updated and
        // the checks may fail
        state.update(state.value() + 1);
        globalCounts.put(key, state.value());

        out.collect(i);
      }
    }
    @Override
    public void flatMap(Integer value, Collector<Tuple2<Integer, Integer>> out) throws Exception {

      int count = counter.value() + 1;
      counter.update(count);

      int s = sum.value() + value;
      sum.update(s);

      if (count % numberElements == 0) {
        out.collect(Tuple2.of(getRuntimeContext().getIndexOfThisSubtask(), s));
        workCompletedLatch.countDown();
      }
    }