jp.skypencil.guava.stream.GuavaCollectors Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of helper Show documentation
Show all versions of helper Show documentation
Helper classes to help Java8 to collaborate with Guava
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 super T, ? extends R> rowMapper,
Function super T, ? extends C> columnMapper,
Function super T, ? extends V> valueMapper) {
return toTable(rowMapper, columnMapper, valueMapper, throwingMerger());
}
public static Collector> toTable(
Function super T, ? extends R> rowMapper,
Function super T, ? extends C> columnMapper,
Function super T, ? extends V> 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 super T, ? extends K> keyMapper,
Function super T, ? extends U> valueMapper) {
return toBiMap(keyMapper, valueMapper, throwingMerger());
}
public static Collector> toBiMap(
Function super T, ? extends K> keyMapper,
Function super T, ? extends U> 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 super T, ? extends K> keyMapper,
Function super T, ? extends U> 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 super T, ? extends K> keyMapper,
Function super T, ? extends U> 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 super T, ? extends K> keyMapper,
Function super T, ? extends U> 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
}
© 2015 - 2024 Weber Informatics LLC | Privacy Policy