@Override
    public Tuple2<Integer, Integer> reduce(
        Tuple2<Integer, Integer> value1, Tuple2<Integer, Integer> value2) throws Exception {
      state.update(state.value() + 1);
      globalCounts.put(value1.f0, state.value());

      return new Tuple2<>(value1.f0, value1.f1 + value2.f1);
    }
    @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 run(SourceContext<String> ctx) throws Exception {
      final Object lockingObject = ctx.getCheckpointLock();

      while (isRunning && index.value() < numElements) {
        char first = (char) ((index.value() % 40) + 40);

        stringBuilder.setLength(0);
        stringBuilder.append(first);

        String result = randomString(stringBuilder, rnd);

        synchronized (lockingObject) {
          index.update(index.value() + step);
          ctx.collect(result);
        }
      }
    }
  @Override
  public TriggerResult onElement(Object element, long timestamp, W window, TriggerContext ctx)
      throws Exception {

    OperatorState<Boolean> first = ctx.getKeyValueState("first", true);

    if (first.value()) {
      long start = timestamp - (timestamp % interval);
      long nextFireTimestamp = start + interval;

      ctx.registerEventTimeTimer(nextFireTimestamp);

      first.update(false);
      return TriggerResult.CONTINUE;
    }
    return TriggerResult.CONTINUE;
  }
 @Override
 public void close() throws IOException {
   counts[getRuntimeContext().getIndexOfThisSubtask()] = count.value();
 }
 @Override
 public PrefixCount map(String value) throws IOException {
   count.update(count.value() + 1);
   return new PrefixCount(value.substring(0, 1), value, 1L);
 }
 @Override
 public PrefixCount map(PrefixCount value) throws Exception {
   count.update(count.value() + 1);
   return value;
 }