public static <T> int batch(Collection<T> collection, int batchSize, BatchOperation<T> callback) {
    if (collection == null || collection.isEmpty() || batchSize == 0) {
      return 0;
    }
    int totalSize = collection.size();
    int totalChunks = (int) Math.ceil((float) totalSize / batchSize);
    int currentChunk = 0;
    int resultSum = 0;

    for (Collection<T> chunk : Iterables.partition(collection, batchSize)) {
      currentChunk += 1;
      resultSum += callback.chunk(chunk, currentChunk, totalChunks, totalSize);
    }
    return resultSum;
  }
Split the collection to the batches

@param collection collection with values to be splitted
@param batchSize  size of the one split
@param callback   logic to process one split
@return Total number of processed items in collection
ambari-server/src/main/java/org/apache/ambari/server/orm/helpers/SQLOperations.java