Start line:  
End line:  

Snippet Preview

Snippet HTML Code

Stack Overflow Questions
  package org.bitbucket.cowwoc.guava.stream;
  
 import static java.util.stream.Collector.Characteristics.CONCURRENT;
 import static java.util.stream.Collector.Characteristics.UNORDERED;
 
 public class MoreCollectors
 {
 
 	private MoreCollectors()
 	{
 	}
 
 	public static <T, K, V> Collector<T, ?, ImmutableMap<K, V>> toImmutableMap(
 		Function<? super T, ? extends K> keyMapper,
 		Function<? super T, ? extends V> valueMapper)
 	{
 		return toImmutableMap(ImmutableMap::builderkeyMappervalueMapper);
 	}
 
 	public static <T, K extends Comparable<K>, V> Collector<T, ?, ImmutableSortedMap<K, V>> toImmutableSortedMap(
 		Function<? super T, ? extends K> keyMapper,
 		Function<? super T, ? extends V> valueMapper)
 	{
 		return toImmutableSortedMap(keyMappervalueMapper, Comparator.naturalOrder());
 	}
 
 	public static <T, K, V> Collector<T, ?, ImmutableSortedMap<K, V>> toImmutableSortedMap(
 		Function<? super T, ? extends K> keyMapper,
 		Function<? super T, ? extends V> valueMapper,
 		Comparator<K> comparator)
 	{
 		return toImmutableMap(() -> ImmutableSortedMap.orderedBy(comparator), keyMappervalueMapper,
 	}
 
 	public static <T, K, V> Collector<T, ?, ImmutableBiMap<K, V>> toImmutableBiMap(
 		Function<? super T, ? extends K> keyMapper,
 		Function<? super T, ? extends V> valueMapper)
 	{
 		return toImmutableMap(ImmutableBiMap::builderkeyMappervalueMapper);
 	}
 
 	private static <T, K, V, M extends ImmutableMap<K, V>> Collector<T, ?, M> toImmutableMap(
 		Supplier<ImmutableMap.Builder<K, V>> supplier,
 		Function<? super T, ? extends K> keyMapper,
 		Function<? super T, ? extends V> valueMapper,
 		Collector.Characteristics... characteristics)
 	{
 		return Collector.of(
 			supplier,
 			new BiConsumer<ImmutableMap.Builder<K, V>, T>()
 		{
 			public void accept(ImmutableMap.Builder<K, V> objectObjectBuilder, T t)
 			{
 				objectObjectBuilder.put(keyMapper.apply(t), valueMapper.apply(t));
 			}
 		},
 		{
 			public ImmutableMap.Builder<K, V> apply(ImmutableMap.Builder<K, V> kuBuilder,
 				ImmutableMap.Builder<K, V> kuBuilder2)
 			{
 				return kuBuilder.putAll(kuBuilder2.build());
 			}
 		},
 			new Function<ImmutableMap.Builder<K, V>, M>()
 		{
 			@SuppressWarnings("unchecked")
 			public M apply(ImmutableMap.Builder<K, V> kuBuilder)
 			{
 				return (M) kuBuilder.build();
 			}
 		},
 			characteristics);
 	}
 
 	public static <T> Collector<T, ?, ImmutableList<T>> toImmutableList()
 	{
		return Collector.of(
			ImmutableList::builder,
		{
			public void accept(ImmutableList.Builder<T> objectBuilder, T t)
			{
				objectBuilder.add(t);
			}
		},
		{
			public ImmutableList.Builder<T> apply(ImmutableList.Builder<T> objectBuilder,
				ImmutableList.Builder<T> objectBuilder2)
			{
				return objectBuilder.addAll(objectBuilder2.build());
			}
		},
		{
			public ImmutableList<T> apply(ImmutableList.Builder<T> tBuilder)
			{
				return tBuilder.build();
			}
		},
	}
	public static <T> Collector<T, ?, ImmutableSet<T>> toImmutableSet()
	{
		return toImmutableSet(ImmutableSet::builder);
	}
	public static <T extends Comparable<T>> Collector<T, ?, ImmutableSortedSet<T>> toImmutableSortedSet()
	{
		return toImmutableSortedSet(Comparator.<T>naturalOrder());
	}
	public static <T> Collector<T, ?, ImmutableSortedSet<T>> toImmutableSortedSet(
		Comparator<T> comparator)
	{
		return toImmutableSet(() ->
		{
			return ImmutableSortedSet.orderedBy(comparator);
	}
	private static <T, S extends ImmutableSet<T>, B extends ImmutableSet.Builder<T>> Collector<T, B, S> toImmutableSet(
		Supplier<B> supplier,
		Collector.Characteristics... characteristics)
	{
		return Collector.of(
			supplier,
			new BiConsumer<B, T>()
		{
			public void accept(B objectBuilder, T t)
			{
				objectBuilder.add(t);
			}
		},
		{
			@SuppressWarnings("unchecked")
			public B apply(B objectBuilder, B objectBuilder2)
			{
				return (B) objectBuilder.addAll(objectBuilder2.build());
			}
		},
			new Function<B, S>()
		{
			@SuppressWarnings("unchecked")
			public S apply(B tBuilder)
			{
				return (S) tBuilder.build();
			}
		},
			characteristics);
	}
	public static <T> Collector<T, ?, ImmutableMultiset<T>> toImmutableMultiset()
	{
		return toImmutableMultiset(ImmutableMultiset::builder);
	}
	public static <T extends Comparable<T>> Collector<T, ?, ImmutableSortedMultiset<T>> toImmutableSortedMultiset()
	{
		return toImmutableSortedMultiset(Comparator.<T>naturalOrder());
	}
		Comparator<T> comparator)
	{
		return toImmutableMultiset(() -> ImmutableSortedMultiset.orderedBy(comparator), );
	}
	private static <T, B extends ImmutableMultiset.Builder<T>, M extends ImmutableMultiset<T>>  Collector<T, ?, M> toImmutableMultiset(
		Supplier<B> supplier,
		Collector.Characteristics... characteristics)
	{
		return Collector.of(
			supplier,
			new BiConsumer<B, T>()
		{
			public void accept(B objectBuilder, T t)
			{
				objectBuilder.add(t);
			}
		},
		{
			@SuppressWarnings("unchecked")
			public B apply(B objectBuilder, B objectBuilder2)
			{
				return (B) objectBuilder.addAll(objectBuilder2.build());
			}
		},
			new Function<B, M>()
		{
			@SuppressWarnings("unchecked")
			public M apply(B tBuilder)
			{
				return (M) tBuilder.build();
			}
		},
			characteristics);
	}
	public static <T, K, V> Collector<T, ?, ImmutableMultimap<K, V>> toImmutableMultimap(
		Function<? super T, ? extends K> keyMapper,
		Function<? super T, ? extends V> valueMapper)
	{
		return toImmutableMultimap(ImmutableMultimap::builderkeyMappervalueMapper,
	}
	public static <T, K, V> Collector<T, ?, ImmutableListMultimap<K, V>> toImmutableListMultimap(
		Function<? super T, ? extends K> keyMapper,
		Function<? super T, ? extends V> valueMapper)
	{
		return toImmutableMultimap(ImmutableListMultimap::builderkeyMappervalueMapper,
	}
	public static <T, K, V> Collector<T, ?, ImmutableSetMultimap<K, V>> toImmutableSetMultimap(
		Function<? super T, ? extends K> keyMapper,
		Function<? super T, ? extends V> valueMapper)
	{
		return toImmutableMultimap(ImmutableSetMultimap::builderkeyMappervalueMapper,
	}
	public static <T, K extends Comparable<K>, V extends Comparable<V>>  Collector<T, ?, ImmutableSetMultimap<K, V>> toImmutableSetMultimapNaturalOrder(
		Function<? super T, ? extends K> keyMapper,
		Function<? super T, ? extends V> valueMapper)
	{
		return toImmutableMultimap(() -> ImmutableSetMultimap.<K, V>builder()
			keyMappervalueMapper);
	}
	public static <T, K, V> Collector<T, ?, ImmutableSetMultimap<K, V>> toImmutableSetMultimap(
		Function<? super T, ? extends K> keyMapper,
		Function<? super T, ? extends V> valueMapper,
		Comparator<? super K> keyComparator,
		Comparator<? super V> valueComparator)
	{
		return toImmutableMultimap(() -> ImmutableSetMultimap.<K, V>builder()
			.orderKeysBy(keyComparator)
			.orderValuesBy(valueComparator),
			keyMappervalueMapper);
	}
	private static <T, K, V, B extends ImmutableMultimap.Builder<K, V>, M extends ImmutableMultimap<K, V>>  Collector<T, ?, M> toImmutableMultimap(
		Supplier<B> supplier,
		Function<? super T, ? extends K> keyMapper,
		Function<? super T, ? extends V> valueMapper,
		Collector.Characteristics... characteristics)
	{
		return Collector.of(
			supplier,
			new BiConsumer<B, T>()
		{
			public void accept(B objectBuilder, T t)
			{
				objectBuilder.put(keyMapper.apply(t), valueMapper.apply(t));
			}
		},
		{
			@SuppressWarnings("unchecked")
			public B apply(B objectBuilder, B objectBuilder2)
			{
				return (B) objectBuilder.putAll(objectBuilder2.build());
			}
		},
			new Function<B, M>()
		{
			@SuppressWarnings("unchecked")
			public M apply(B tBuilder)
			{
				return (M) tBuilder.build();
			}
		},
			characteristics);
	}
	public static <T, R, C, V> Collector<T, ?, ImmutableTable<R, C, V>> toImmutableTable(
		Function<? super T, ? extends R> rowMapper,
		Function<? super T, ? extends C> columnMapper,
		Function<? super T, ? extends V> valueMapper)
	{
		return Collector.of(
			ImmutableTable::builder,
			new BiConsumer<ImmutableTable.Builder<R, C, V>, T>()
		{
			public void accept(ImmutableTable.Builder<R, C, V> rcvBuilder, T t)
			{
				rcvBuilder.put(rowMapper.apply(t), columnMapper.apply(t), valueMapper.apply(t));
			}
		},
		{
			public ImmutableTable.Builder<R, C, V> apply(ImmutableTable.Builder<R, C, V> rcvBuilder,
				ImmutableTable.Builder<R, C, V> rcvBuilder2)
			{
				return rcvBuilder.putAll(rcvBuilder2.build());
			}
		},
			new Function<ImmutableTable.Builder<R, C, V>, ImmutableTable<R, C, V>>()
		{
			public ImmutableTable<R, C, V> apply(ImmutableTable.Builder<R, C, V> rcvBuilder)
			{
				return rcvBuilder.build();
			}
		},
	}
	public static <T, R extends Comparable<R>, C extends Comparable<C>, V> Collector<T, ?, ImmutableTable<R, C, V>> toImmutableSortedTable(
		Function<? super T, ? extends R> rowMapper,
		Function<? super T, ? extends C> columnMapper,
		Function<? super T, ? extends V> valueMapper)
	{
		return toImmutableSortedTable(rowMappercolumnMappervalueMapper, Comparator.<R>naturalOrder(),
			Comparator.<C>naturalOrder());
	}
	public static <T, R, C, V> Collector<T, ?, ImmutableTable<R, C, V>> toImmutableSortedTable(
		Function<? super T, ? extends R> rowMapper,
		Function<? super T, ? extends C> columnMapper,
		Function<? super T, ? extends V> valueMapper,
		Comparator<R> rowComparator,
		Comparator<C> columnComparator)
	{
		return Collector.of(
			new Supplier<ImmutableTable.Builder<R, C, V>>()
		{
			public ImmutableTable.Builder<R, C, V> get()
			{
				return ImmutableTable.<R, C, V>builder()
					.orderRowsBy(rowComparator)
					.orderColumnsBy(columnComparator);
			}
		},
			new BiConsumer<ImmutableTable.Builder<R, C, V>, T>()
		{
			public void accept(ImmutableTable.Builder<R, C, V> rcvBuilder, T t)
			{
				rcvBuilder.put(rowMapper.apply(t), columnMapper.apply(t), valueMapper.apply(t));
			}
		},
		{
			public ImmutableTable.Builder<R, C, V> apply(ImmutableTable.Builder<R, C, V> rcvBuilder,
				ImmutableTable.Builder<R, C, V> rcvBuilder2)
			{
				return rcvBuilder.putAll(rcvBuilder2.build());
			}
		},
			new Function<ImmutableTable.Builder<R, C, V>, ImmutableTable<R, C, V>>()
		{
			public ImmutableTable<R, C, V> apply(ImmutableTable.Builder<R, C, V> rcvBuilder)
			{
				return rcvBuilder.build();
			}
		},
	}
New to GrepCode? Check out our FAQ X