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

jp.skypencil.guava.stream.GuavaCollectors Maven / Gradle / Ivy

The newest version!
package jp.skypencil.guava.stream;

import java.util.HashMap;
import java.util.Map;
import java.util.Set;
import java.util.function.BiConsumer;
import java.util.function.BinaryOperator;
import java.util.function.Function;
import java.util.function.Supplier;
import java.util.stream.Collector;

import com.google.common.collect.BiMap;
import com.google.common.collect.ImmutableBiMap;
import com.google.common.collect.ImmutableList;
import com.google.common.collect.ImmutableList.Builder;
import com.google.common.collect.ImmutableMap;
import com.google.common.collect.ImmutableMultimap;
import com.google.common.collect.ImmutableMultiset;
import com.google.common.collect.ImmutableSet;
import com.google.common.collect.ImmutableTable;
import com.google.common.collect.Multimap;
import com.google.common.collect.Multiset;
import com.google.common.collect.Table;

public final class GuavaCollectors {
    private static final  BinaryOperator throwingMerger() {
        return (value, another) -> {
            throw new IllegalStateException(String.format("Duplicated value %s", value));
        };
    }

    public static  Collector> toTable(
            Function rowMapper,
            Function columnMapper,
            Function valueMapper) {
        return toTable(rowMapper, columnMapper, valueMapper, throwingMerger());
    }

    public static  Collector> toTable(
            Function rowMapper,
            Function columnMapper,
            Function valueMapper,
            BinaryOperator mergeFunction) {


        return new TableCollector>(
                rowMapper, columnMapper, valueMapper, mergeFunction) {
            @Override
            public Function, Table> finisher() {
                return Function.identity();
            }

            @Override
            public Set characteristics() {
                return CharacteristicSets.IDENTITY;
            }
        };
    }

    public static  Collector> toBiMap(
            Function keyMapper,
            Function valueMapper) {
        return toBiMap(keyMapper, valueMapper, throwingMerger());
    }

    public static  Collector> toBiMap(
            Function keyMapper,
            Function valueMapper,
            BinaryOperator mergeFunction) {
        return new BiMapCollector>(keyMapper, valueMapper,
                mergeFunction) {
            @Override
            public Function, BiMap> finisher() {
                return Function.identity();
            }

            @Override
            public Set characteristics() {
                return CharacteristicSets.IDENTITY;
            }
        };
    }

    public static  Collector> toImmutableBiMap(
            Function keyMapper,
            Function valueMapper,
            BinaryOperator mergeFunction) {
        return new BiMapCollector>(keyMapper,
                valueMapper, mergeFunction) {
            @Override
            public Function, ImmutableBiMap> finisher() {
                return map -> {
                    ImmutableBiMap.Builder builder = ImmutableBiMap
                            .builder();
                    return builder.putAll(map).build();
                };
            }

            @Override
            public Set characteristics() {
                return CharacteristicSets.EMPTY;
            }
        };
    }

    public static  Collector> toMultimap(
            Function keyMapper,
            Function valueMapper) {
        return new MultimapCollector>(keyMapper,
                valueMapper) {
            @Override
            public Function, Multimap> finisher() {
                return Function.identity();
            }

            @Override
            public Set characteristics() {
                return CharacteristicSets.IDENTITY;
            }
        };
    }

    public static  Collector> toImmutableMultimap(
            Function keyMapper,
            Function valueMapper) {
        return new MultimapCollector>(
                keyMapper, valueMapper) {
            @Override
            public Function, ImmutableMultimap> finisher() {
                return map -> {
                    ImmutableMultimap.Builder builder = ImmutableMultimap
                            .builder();
                    return builder.putAll(map).build();
                };
            }

            @Override
            public Set characteristics() {
                return CharacteristicSets.EMPTY;
            }
        };
    }

    public static  Collector> toMultiset() {
        return new MultisetCollector>() {
            @Override
            public Function, Multiset> finisher() {
                return Function.identity();
            }

            @Override
            public Set characteristics() {
                return CharacteristicSets.IDENTITY;
            }
        };
    }

    private GuavaCollectors() {}
    // TODO groupingBy
}