<commit_before>/*-
 * #%L
 * Vert.x utilities from Braintags
 * %%
 * Copyright (C) 2017 Braintags GmbH
 * %%
 * All rights reserved. This program and the accompanying materials
 * are made available under the terms of the Eclipse Public License v1.0
 * which accompanies this distribution, and is available at
 * http://www.eclipse.org/legal/epl-v10.html
 * #L%
 */
package de.braintags.vertx.util.async;

import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
import java.util.function.BiConsumer;
import java.util.function.Function;

import io.vertx.core.AsyncResult;
import io.vertx.core.CompositeFuture;
import io.vertx.core.Future;
import io.vertx.core.Handler;
import io.vertx.core.Vertx;

/**
 * Utility class for {@link CompositeFuture}
 * 
 * @author sschmitt
 * 
 */
public class CompositeUtil {

  private CompositeUtil() {
    // hide constructor
  }

  /**
   * Like a composite future with the "join" method, but executes only a given amount, and waits until that amount is
   * finished before starting the next chunk.
   * 
   * Because a normal "join" execution would execute for all objects, no matter if there is a failure, this method will
   * ignore any failures inside chunks and complete until the iterator is empty.
   * This is why the handler returns a list of all futures. The caller MUST check each future for failure to determine
   * if (and how many) errors occurred during execution. The handler itself will ALWAYS be successful.
   * 
   * @param iterator
   *          the iterator of all data that should be processed
   * @param chunkSize
   *          the size of each individual chunk. A chunk may be smaller if there is not enough data to fill it
   *          completely
   * @param biConsumer
   *          the method that should be executed for each object of the iterator, combined with the completer of
   *          the future for the object
   * @param handler
   *          returns all futures created during the execution. WILL ALWAYS RETURN SUCCESS! Check each future to
   *          determine if the operation really was successful
   */
  @Deprecated
  public static <T, U> void executeChunked(final Iterator<T> iterator, final int chunkSize,
      final BiConsumer<T, Handler<AsyncResult<U>>> biConsumer, final Handler<AsyncResult<List<Future<U>>>> handler) {
    executeChunked(iterator, chunkSize, 0, null, biConsumer, handler);
  }

  public static <T, U> Future<List<Future<U>>> executesChunked(final Iterator<T> iterator, final int chunkSize,
      final Function<T, Future<U>> biConsumer) {
    Future<List<Future<U>>> f = Future.future();
    executeChunked(iterator, chunkSize, 0, null, (a, h) -> biConsumer.apply(a).setHandler(h), f);
    return f;
  }

  /**
   * Like a composite future with the "join" method, but executes only a given amount, and waits until that amount is
   * finished, plus a custom sleep period, before starting the next chunk.
   * 
   * Because a normal "join" execution would execute for all objects, no matter if there is a failure, this method will
   * ignore any failures inside chunks and complete until the iterator is empty.
   * This is why the handler returns a list of all futures. The caller MUST check each future for failure to determine
   * if (and how many) errors occurred during execution. The handler itself will ALWAYS be successful.<commit_msg>fixing ambiguous method<commit_after>/*-
 * #%L
 * Vert.x utilities from Braintags
 * %%
 * Copyright (C) 2017 Braintags GmbH
 * %%
 * All rights reserved. This program and the accompanying materials
 * are made available under the terms of the Eclipse Public License v1.0
 * which accompanies this distribution, and is available at
 * http://www.eclipse.org/legal/epl-v10.html
 * #L%
 */
package de.braintags.vertx.util.async;

import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
import java.util.function.BiConsumer;
import java.util.function.Function;

import io.vertx.core.AsyncResult;
import io.vertx.core.CompositeFuture;
import io.vertx.core.Future;
import io.vertx.core.Handler;
import io.vertx.core.Vertx;

/**
 * Utility class for {@link CompositeFuture}
 * 
 * @author sschmitt
 * 
 */
public class CompositeUtil {

  private CompositeUtil() {
    // hide constructor
  }

  /**
   * Like a composite future with the "join" method, but executes only a given amount, and waits until that amount is
   * finished before starting the next chunk.
   * 
   * Because a normal "join" execution would execute for all objects, no matter if there is a failure, this method will
   * ignore any failures inside chunks and complete until the iterator is empty.
   * This is why the handler returns a list of all futures. The caller MUST check each future for failure to determine
   * if (and how many) errors occurred during execution. The handler itself will ALWAYS be successful.
   * 
   * @param iterator
   *          the iterator of all data that should be processed
   * @param chunkSize
   *          the size of each individual chunk. A chunk may be smaller if there is not enough data to fill it
   *          completely
   * @param biConsumer
   *          the method that should be executed for each object of the iterator, combined with the completer of
   *          the future for the object
   * @param handler
   *          returns all futures created during the execution. WILL ALWAYS RETURN SUCCESS! Check each future to
   *          determine if the operation really was successful
   */
  @Deprecated
  public static <T, U> void executeChunked(final Iterator<T> iterator, final int chunkSize,
      final BiConsumer<T, Handler<AsyncResult<U>>> biConsumer, final Handler<AsyncResult<List<Future<U>>>> handler) {
    executeChunked(iterator, chunkSize, 0, null, biConsumer, handler);
  }

  public static <T, U> Future<List<Future<U>>> executeChunked(final Iterator<T> iterator, final int chunkSize,
      final Function<T, Future<U>> biConsumer) {
    Future<List<Future<U>>> f = Future.future();
    executeChunked(iterator, chunkSize, 0, null, (a, h) -> biConsumer.apply(a).setHandler(h), f);
    return f;
  }

  /**
   * Like a composite future with the "join" method, but executes only a given amount, and waits until that amount is
   * finished, plus a custom sleep period, before starting the next chunk.
   * 
   * Because a normal "join" execution would execute for all objects, no matter if there is a failure, this method will
   * ignore any failures inside chunks and complete until the iterator is empty.
   * This is why the handler returns a list of all futures. The caller MUST check each future for failure to determine
   * if (and how many) errors occurred during execution. The handler itself will ALWAYS be successful.
{'fraction_non_alphanumeric': 0.07859358841778696, 'fraction_numerical': 0.002954646181119811, 'mean_word_length': 3.486414844267727, 'pattern_counts': {'":': 0, '<': 41, '<?xml version=': 0, '>': 43, 'https://': 0, 'lorem ipsum': 0, 'www.': 2, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '4822735', 'n_tokens_mistral': 1871, 'n_tokens_neox': 1788, 'n_words': 813}