All Downloads are FREE. Search and download functionalities are using the official Maven repository.

org.omnifaces.utils.stream.ForEachBatchCollector Maven / Gradle / Ivy

There is a newer version: 0.14
Show newest version
/*
 * Copyright 2018 OmniFaces
 *
 * Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with
 * the License. You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on
 * an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the
 * specific language governing permissions and limitations under the License.
 */
package org.omnifaces.utils.stream;

import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import java.util.Set;
import java.util.function.BiConsumer;
import java.util.function.BinaryOperator;
import java.util.function.Consumer;
import java.util.function.Function;
import java.util.function.Supplier;
import java.util.stream.Collector;

class ForEachBatchCollector implements Collector, Void> {

	private final Consumer> batchConsumer;
	private final int batchSize;

	ForEachBatchCollector(Consumer> batchConsumer, int batchSize) {
		if (batchSize <= 0) {
			throw new IllegalArgumentException("Batch size must be greater than 0");
		}

		this.batchConsumer = batchConsumer;
		this.batchSize = batchSize;
	}


	@Override
	public Supplier> supplier() {
		return ArrayList::new;
	}

	@Override
	public BiConsumer, T> accumulator() {
		return (list, element) -> {
			list.add(element);

			if (list.size() == batchSize) {
				batchConsumer.accept(list);
				list.clear();
			}
		};
	}

	@Override
	public BinaryOperator> combiner() {
		return (list1, list2) -> {
			if (list1.size() + list2.size() < batchSize) {
				list1.addAll(list2);
				return list1;
			}

			while (list1.size() < batchSize) {
				list1.add(list2.remove(0));
			}

			batchConsumer.accept(list1);

			return list2;
		};
	}

	@Override
	public Function, Void> finisher() {
		return (list) -> {
			if (!list.isEmpty()) {
				batchConsumer.accept(list);
			}

			return null;
		};
	}

	@Override
	public Set characteristics() {
		return Collections.emptySet();
	}
}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy