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

org.agzamovr.collectors.RankDistinctCollector 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.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 comparator,
                       Function mapper,
                       Collector 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 comparator) {
        return rankDistinct(comparator, Function.identity(), toList());
    }

    , R>
    Collector rankDistinct(Collector downstream) {
        return rankDistinct(nullsLast(Comparator.naturalOrder()), Function.identity(), downstream);
    }

    >
    Collector> rankDistinct(Function mapper) {
        return rankDistinct(nullsLast(Comparator.naturalOrder()), mapper, toList());
    }

    
    Collector rankDistinct(Comparator comparator,
                                    Collector downstream) {
        return rankDistinct(comparator, Function.identity(), downstream);
    }

    
    Collector> rankDistinct(Comparator comparator,
                                          Function mapper) {
        return rankDistinct(comparator, mapper, toList());
    }

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

    
    Collector, R> rankDistinct(Comparator comparator,
                                          Function mapper,
                                          Collector downstream) {
        return Collector.of(ArrayList::new,
                List::add,
                CollectorEx::listCombiner,
                (list) -> distinctFinisher(list, comparator, mapper, downstream));
    }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy