org.agzamovr.collectors.RankingCollector 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.function.Supplier;
import java.util.stream.Collector;
import static java.util.Comparator.nullsLast;
import static java.util.stream.Collectors.toList;
class RankingCollector {
static final RankingCollector RANKING_COLLECTOR = new RankingCollector();
private void validateInput(Comparator> comparator,
Comparator> rankOrder,
Collector, ?, ?> downstream) {
Objects.requireNonNull(comparator, "Comparator cannot be null");
Objects.requireNonNull(rankOrder, "Rank comparator cannot be null");
Objects.requireNonNull(downstream, "Downstream collector cannot be null");
}
SortedMap rankFinisher(List list,
Comparator super T> comparator,
Comparator rankOrder,
boolean denseRank,
Collector super T, A, R> downstream) {
validateInput(comparator, rankOrder, downstream);
list.sort(comparator);
SortedMap map = new TreeMap<>(rankOrder);
Integer rank = 0;
T prev = null;
Supplier downstreamSupplier = downstream.supplier();
Function downstreamFinisher = downstream.finisher();
BiConsumer downstreamAccumulator = downstream.accumulator();
A container = null;
for (int i = 0; i < list.size(); i++) {
T current = list.get(i);
boolean startNextRank = rank == 0 || comparator.compare(current, prev) != 0;
if (startNextRank) {
if (container != null)
map.put(rank, downstreamFinisher.apply(container));
rank = denseRank ? rank + 1 : i + 1;
container = downstreamSupplier.get();
}
downstreamAccumulator.accept(container, current);
prev = current;
}
if (container != null)
map.put(rank, downstreamFinisher.apply(container));
return map;
}
Map mapObjToRankFinisher(List list,
Function super T, R> mapper,
Comparator super T> comparator,
boolean denseRank) {
SortedMap> rankedMap = rankFinisher(list, comparator, Integer::compareTo, denseRank, toList());
Map mapObjToRank = new HashMap<>();
rankedMap.forEach((rank, items) -> items.forEach((item) -> mapObjToRank.put(mapper.apply(item), rank)));
return mapObjToRank;
}
>
Collector>> denseRank() {
return denseRank(nullsLast(Comparator.naturalOrder()));
}
Collector>> denseRank(Comparator super T> comparator) {
return denseRank(comparator, Integer::compareTo);
}
, R>
Collector> denseRank(Collector super T, ?, R> downstream) {
return rank(nullsLast(Comparator.naturalOrder()), Integer::compareTo, true, downstream);
}
Collector>> denseRank(Comparator super T> comparator,
Comparator rankOrder) {
return rank(comparator, rankOrder, true, toList());
}
>
Collector>> rank() {
return rank(nullsLast(Comparator.naturalOrder()));
}
Collector>> rank(Comparator super T> comparator) {
return rank(comparator, Integer::compareTo);
}
, R>
Collector> rank(Collector super T, ?, R> downstream) {
return rank(nullsLast(Comparator.naturalOrder()), Integer::compareTo, false, downstream);
}
Collector>> rank(Comparator super T> comparator,
Comparator rankOrder) {
return rank(comparator, rankOrder, false, toList());
}
Collector> rank(Comparator super T> comparator,
Comparator rankOrder,
boolean denseRank,
Collector super T, ?, R> downstream) {
return Collector.of((Supplier>) ArrayList::new,
List::add,
CollectorEx::listCombiner,
(list) -> rankFinisher(list, comparator, rankOrder, denseRank, downstream));
}
>
Collector> mapObjToDenseRank() {
return mapObjToRank(nullsLast(Comparator.naturalOrder()), true);
}
Collector> mapObjToDenseRank(Comparator super T> comparator) {
return mapObjToRank(comparator, true);
}
, R>
Collector> mapObjToDenseRank(Function super T, R> mapper) {
return mapObjToRank(mapper, nullsLast(Comparator.naturalOrder()), true);
}
>
Collector> mapObjToRank() {
return mapObjToRank(nullsLast(Comparator.naturalOrder()), false);
}
Collector> mapObjToRank(Comparator super T> comparator) {
return mapObjToRank(comparator, false);
}
, R>
Collector> mapObjToRank(Function super T, R> mapper) {
return mapObjToRank(mapper, nullsLast(Comparator.naturalOrder()), false);
}
Collector> mapObjToRank(Comparator super T> comparator,
boolean denseRank) {
return mapObjToRank(Function.identity(), comparator, denseRank);
}
Collector, Map> mapObjToRank(Function super T, R> mapper,
Comparator super T> comparator,
boolean denseRank) {
return Collector.of(ArrayList::new,
List::add,
CollectorEx::listCombiner,
(list) -> mapObjToRankFinisher(list, mapper, comparator, denseRank));
}
}