org.agzamovr.collectors.RankDistinctCollector Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of collectorex Show documentation
Show all versions of collectorex Show documentation
Java Collectors extensions for using with Java 8 streams
The newest version!
package org.agzamovr.collectors;
import java.util.*;
import java.util.function.BiConsumer;
import java.util.function.Function;
import java.util.stream.Collector;
import static java.util.Comparator.nullsLast;
import static java.util.stream.Collectors.toList;
class RankDistinctCollector {
static final RankDistinctCollector RANK_DISTINCT_COLLECTOR = new RankDistinctCollector();
R distinctFinisher(Collection collection,
Comparator super D> comparator,
Function super T, D> mapper,
Collector super D, A, R> downstream) {
Map> rankedMap = collection.stream()
.map(mapper)
.collect(CollectorEx.rank(comparator));
Function downstreamFinisher = downstream.finisher();
BiConsumer downstreamAccumulator = downstream.accumulator();
A container = downstream.supplier().get();
rankedMap.forEach((rank, items) -> downstreamAccumulator.accept(container, items.get(0)));
return downstreamFinisher.apply(container);
}
>
Collector> rankDistinct() {
return rankDistinct(nullsLast(Comparator.naturalOrder()), Function.identity(), toList());
}
Collector> rankDistinct(Comparator super T> comparator) {
return rankDistinct(comparator, Function.identity(), toList());
}
, R>
Collector rankDistinct(Collector super T, ?, R> downstream) {
return rankDistinct(nullsLast(Comparator.naturalOrder()), Function.identity(), downstream);
}
>
Collector> rankDistinct(Function super T, D> mapper) {
return rankDistinct(nullsLast(Comparator.naturalOrder()), mapper, toList());
}
Collector rankDistinct(Comparator super T> comparator,
Collector super T, ?, R> downstream) {
return rankDistinct(comparator, Function.identity(), downstream);
}
Collector> rankDistinct(Comparator super D> comparator,
Function super T, D> mapper) {
return rankDistinct(comparator, mapper, toList());
}
, R>
Collector rankDistinct(Function super T, D> mapper,
Collector super D, ?, R> downstream) {
return rankDistinct(nullsLast(Comparator.naturalOrder()), mapper, downstream);
}
Collector, R> rankDistinct(Comparator super D> comparator,
Function super T, D> mapper,
Collector super D, ?, R> downstream) {
return Collector.of(ArrayList::new,
List::add,
CollectorEx::listCombiner,
(list) -> distinctFinisher(list, comparator, mapper, downstream));
}
}