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

org.omnifaces.utils.stream.Collectors 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 static java.util.Comparator.naturalOrder;
import static java.util.function.Function.identity;

import java.util.Comparator;
import java.util.LinkedHashMap;
import java.util.LinkedHashSet;
import java.util.List;
import java.util.Map;
import java.util.Optional;
import java.util.Set;
import java.util.function.Consumer;
import java.util.function.Function;
import java.util.stream.Collector;
import java.util.stream.Stream;

public final class Collectors {

	private Collectors() {
	}

	public static  Collector> toMap(Function keyMapper) {
		return java.util.stream.Collectors.toMap(keyMapper, identity());
	}

	public static  Collector> toLinkedMap(Function keyMapper) {
		return java.util.stream.Collectors.toMap(keyMapper, identity(), (l, r) -> l, LinkedHashMap::new);
	}

	public static  Collector> toLinkedSet() {
		return java.util.stream.Collectors.toCollection(LinkedHashSet::new);
	}

	public static  Collector forEachBatch(Consumer> batchConsumer, int batchSize) {
		return new ForEachBatchCollector<>(batchConsumer, batchSize);
	}

	public static  Collector> combine(Collector collector1, Collector collector2) {
		return new CombinedCollector<>(collector1, collector2);
	}

	/**
	 * Returns a collector which takes the elements of the current stream and returns a new stream with the same elements in reverse order.
	 *
	 * 

This collector will collect all elements from a stream into memory in order to return the reversed stream. As a result this collector may not * be suitable for extremely large or infinite streams. *

* * @param The type of the elements * @return A Collector that returns the elements of a stream in reverse order. */ public static Collector> reversedStream() { return new ReversedStreamCollector<>(); } /** * Returns a collector which will return the last element of a stream, if present. * * @param The type of the elements * * @return An {@link Optional} containing the last element of the stream or {@link Optional#empty()} if the stream is empty. */ public static Collector> findLast() { return new FindLastCollector<>(); } public static > Collector> summary() { return summaryBy(naturalOrder()); } public static Collector> summaryBy(Comparator comparator) { return new SummaryCollector<>(comparator); } }




© 2015 - 2025 Weber Informatics LLC | Privacy Policy