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

org.agzamovr.collectors.RankingCollector Maven / Gradle / Ivy

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 comparator,
                                       Comparator rankOrder,
                                       boolean denseRank,
                                       Collector 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 mapper,
                                         Comparator 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 comparator) {
        return denseRank(comparator, Integer::compareTo);
    }

    , R>
    Collector> denseRank(Collector downstream) {
        return rank(nullsLast(Comparator.naturalOrder()), Integer::compareTo, true, downstream);
    }

    
    Collector>> denseRank(Comparator comparator,
                                                           Comparator rankOrder) {
        return rank(comparator, rankOrder, true, toList());
    }

    >
    Collector>> rank() {
        return rank(nullsLast(Comparator.naturalOrder()));
    }

    
    Collector>> rank(Comparator comparator) {
        return rank(comparator, Integer::compareTo);
    }

    , R>
    Collector> rank(Collector downstream) {
        return rank(nullsLast(Comparator.naturalOrder()), Integer::compareTo, false, downstream);
    }

    
    Collector>> rank(Comparator comparator,
                                                      Comparator rankOrder) {
        return rank(comparator, rankOrder, false, toList());
    }

    
    Collector> rank(Comparator comparator,
                                                Comparator rankOrder,
                                                boolean denseRank,
                                                Collector 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 comparator) {
        return mapObjToRank(comparator, true);
    }

    , R>
    Collector> mapObjToDenseRank(Function mapper) {
        return mapObjToRank(mapper, nullsLast(Comparator.naturalOrder()), true);
    }

    >
    Collector> mapObjToRank() {
        return mapObjToRank(nullsLast(Comparator.naturalOrder()), false);
    }

    
    Collector> mapObjToRank(Comparator comparator) {
        return mapObjToRank(comparator, false);
    }

    , R>
    Collector> mapObjToRank(Function mapper) {
        return mapObjToRank(mapper, nullsLast(Comparator.naturalOrder()), false);
    }

    
    Collector> mapObjToRank(Comparator comparator,
                                                  boolean denseRank) {
        return mapObjToRank(Function.identity(), comparator, denseRank);
    }

    
    Collector, Map> mapObjToRank(Function mapper,
                                                        Comparator comparator,
                                                        boolean denseRank) {
        return Collector.of(ArrayList::new,
                List::add,
                CollectorEx::listCombiner,
                (list) -> mapObjToRankFinisher(list, mapper, comparator, denseRank));
    }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy