io.linguarobot.aws.cdk.maven.MoreCollectors Maven / Gradle / Ivy
package io.linguarobot.aws.cdk.maven;
import java.util.Comparator;
import java.util.stream.Collector;
import java.util.stream.Collectors;
/**
* Contains additional implementations of {@link Collector}.
*/
public final class MoreCollectors {
private MoreCollectors() {
}
/**
* Returns a {@link Collector} that will sort elements of the stream before passing them to the downstream collector.
*
* @param downstreamCollector a collector that will accept the sorted elements
* @param the types of the input elements
* @param the result type of the collector
*
* @return a collector sorting the elements of the stream before passing them to the next, downstream collector
*/
public static , R> Collector sorting(Collector downstreamCollector) {
return sorting(Comparator.naturalOrder(), downstreamCollector);
}
/**
* Returns a {@link Collector} that will sort elements of the stream using the given comparator before passing
* them to the downstream collector.
*
* @param comparator the comparator used to compare the elements of the stream
* @param downstreamCollector a collector that will accept the sorted elements
* @param the types of the input elements
* @param the result type of the collector
*
* @return a collector sorting the elements of the stream before passing them to the next, downstream collector
*/
public static Collector sorting(Comparator super T> comparator, Collector downstreamCollector) {
return Collectors.collectingAndThen(
Collectors.toList(),
values -> values.stream().sorted(comparator).collect(downstreamCollector)
);
}
}
© 2015 - 2025 Weber Informatics LLC | Privacy Policy