Class GuavaCollectors

java.lang.Object
org.jdbi.v3.guava.GuavaCollectors

public class GuavaCollectors extends Object
Provides Collectors for Guava collection types.

Supported container types:

  • com.google.common.base.Optional<T> (throws an exception if more than one row in result)
  • com.google.common.collect.ImmutableList<T>
  • com.google.common.collect.ImmutableSet<T>
  • com.google.common.collect.ImmutableSortedSet<T extends Comparable>

Supported Maps and Multimaps types - for rows mapped to Map.Entry<K, V>:

  • com.google.common.collect.BiMap<K, V>
  • com.google.common.collect.ImmutableMap<K, V>
  • com.google.common.collect.Multimap<K, V>
  • com.google.common.collect.ListMultimap<K, V>
  • com.google.common.collect.ArrayListMultimap<K, V>
  • com.google.common.collect.LinkedListMultimap<K, V>
  • com.google.common.collect.SetMultimap<K, V>
  • com.google.common.collect.HashMultimap<K, V>
  • com.google.common.collect.TreeMultimap<K, V>
  • com.google.common.collect.ImmutableMultimap<K, V>
  • com.google.common.collect.ImmutableListMultimap<K, V>
  • com.google.common.collect.ImmutableSetMultimap<K, V>
  • Method Summary

    Modifier and Type
    Method
    Description
    Returns a CollectorFactory which knows how to create all supported Guava types.
    static <K, V> Collector<Map.Entry<K,V>,?,com.google.common.collect.ArrayListMultimap<K,V>>
    Returns a Collector that accumulates Map.Entry<K, V> input elements into an ArrayListMultimap<K, V>.
    static <K, V> Collector<Map.Entry<K,V>,?,com.google.common.collect.BiMap<K,V>>
    Returns a Collector that accumulates Map.Entry<K, V> input elements into a HashBiMap<K, V>.
    static <K, V> Collector<Map.Entry<K,V>,?,com.google.common.collect.HashMultimap<K,V>>
    Returns a Collector that accumulates Map.Entry<K, V> input elements into a HashMultimap<K, V>.
    static <K, V> Collector<Map.Entry<K,V>,?,com.google.common.collect.ImmutableListMultimap<K,V>>
    Returns a Collector that accumulates Map.Entry<K, V> input elements into an ImmutableListMultimap<K, V>.
    static <K, V> Collector<Map.Entry<K,V>,?,com.google.common.collect.ImmutableMap<K,V>>
    Returns a Collector that accumulates Map.Entry<K, V> input elements into an ImmutableMap<K, V>.
    static <K, V> Collector<Map.Entry<K,V>,?,com.google.common.collect.ImmutableSetMultimap<K,V>>
    Returns a Collector that accumulates Map.Entry<K, V> input elements into an ImmutableSetMultimap<K, V>.
    static <K, V> Collector<Map.Entry<K,V>,?,com.google.common.collect.LinkedListMultimap<K,V>>
    Returns a Collector that accumulates Map.Entry<K, V> input elements into a LinkedListMultimap<K, V>.
    static <K, V, M extends com.google.common.collect.Multimap<K, V>>
    Collector<Map.Entry<K,V>,?,M>
    toMultimap(Supplier<M> multimapFactory)
    Returns a Collector that accumulates Map.Entry<K, V> input elements into a Multimap<K, V> of the supplied type.
    static <T> Collector<T,?,com.google.common.base.Optional<T>>
    Returns a Collector that accumulates 0 or 1 input elements into Guava's Optional<T>.
    static <K extends Comparable, V extends Comparable>
    Collector<Map.Entry<K,V>,?,com.google.common.collect.TreeMultimap<K,V>>
    Returns a Collector that accumulates Map.Entry<K, V> input elements into a TreeMultimap<K, V>.

    Methods inherited from class java.lang.Object

    equals, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
  • Method Details

    • factory

      public static CollectorFactory factory()
      Returns a CollectorFactory which knows how to create all supported Guava types.
      Returns:
      A CollectorFactory which knows how to create all supported Guava types
    • toOptional

      public static <T> Collector<T,?,com.google.common.base.Optional<T>> toOptional()
      Returns a Collector that accumulates 0 or 1 input elements into Guava's Optional<T>. The returned collector will throw IllegalStateException whenever 2 or more elements are present in a stream. Null elements are mapped to Optional.absent().
      Type Parameters:
      T - the collected type
      Returns:
      a Collector which collects 0 or 1 input elements into a Guava Optional<T>.
    • toImmutableMap

      public static <K, V> Collector<Map.Entry<K,V>,?,com.google.common.collect.ImmutableMap<K,V>> toImmutableMap()
      Returns a Collector that accumulates Map.Entry<K, V> input elements into an ImmutableMap<K, V>.
      Type Parameters:
      K - the type of map keys
      V - the type of map values
      Returns:
      a Collector which collects map entry elements into an ImmutableMap, in encounter order.
    • toHashBiMap

      public static <K, V> Collector<Map.Entry<K,V>,?,com.google.common.collect.BiMap<K,V>> toHashBiMap()
      Returns a Collector that accumulates Map.Entry<K, V> input elements into a HashBiMap<K, V>. The returned collector will throw IllegalStateException whenever a set of input elements contains multiple entries with the same key.
      Type Parameters:
      K - the type of map keys
      V - the type of map values
      Returns:
      a Collector which collects map entry elements into a HashBiMap, in encounter order.
    • toImmutableListMultimap

      public static <K, V> Collector<Map.Entry<K,V>,?,com.google.common.collect.ImmutableListMultimap<K,V>> toImmutableListMultimap()
      Returns a Collector that accumulates Map.Entry<K, V> input elements into an ImmutableListMultimap<K, V>.
      Type Parameters:
      K - the type of map keys
      V - the type of map values
      Returns:
      a Collector which collects map entry elements into an ImmutableListMultimap, in encounter order.
    • toImmutableSetMultimap

      public static <K, V> Collector<Map.Entry<K,V>,?,com.google.common.collect.ImmutableSetMultimap<K,V>> toImmutableSetMultimap()
      Returns a Collector that accumulates Map.Entry<K, V> input elements into an ImmutableSetMultimap<K, V>.
      Type Parameters:
      K - the type of map keys
      V - the type of map values
      Returns:
      a Collector which collects map entry elements into an ImmutableSetMultimap, in encounter order.
    • toArrayListMultimap

      public static <K, V> Collector<Map.Entry<K,V>,?,com.google.common.collect.ArrayListMultimap<K,V>> toArrayListMultimap()
      Returns a Collector that accumulates Map.Entry<K, V> input elements into an ArrayListMultimap<K, V>.
      Type Parameters:
      K - the type of map keys
      V - the type of map values
      Returns:
      a Collector which collects map entry elements into an ArrayListMultimap, in encounter order.
    • toLinkedListMultimap

      public static <K, V> Collector<Map.Entry<K,V>,?,com.google.common.collect.LinkedListMultimap<K,V>> toLinkedListMultimap()
      Returns a Collector that accumulates Map.Entry<K, V> input elements into a LinkedListMultimap<K, V>.
      Type Parameters:
      K - the type of map keys
      V - the type of map values
      Returns:
      a Collector which collects map entry elements into a LinkedListMultimap, in encounter order.
    • toHashMultimap

      public static <K, V> Collector<Map.Entry<K,V>,?,com.google.common.collect.HashMultimap<K,V>> toHashMultimap()
      Returns a Collector that accumulates Map.Entry<K, V> input elements into a HashMultimap<K, V>.
      Type Parameters:
      K - the type of map keys
      V - the type of map values
      Returns:
      a Collector which collects map entry elements into a ArrayListMultimap, in encounter order.
    • toTreeMultimap

      public static <K extends Comparable, V extends Comparable> Collector<Map.Entry<K,V>,?,com.google.common.collect.TreeMultimap<K,V>> toTreeMultimap()
      Returns a Collector that accumulates Map.Entry<K, V> input elements into a TreeMultimap<K, V>.
      Type Parameters:
      K - the type of map keys
      V - the type of map values
      Returns:
      a Collector which collects map entry elements into a TreeMultimap, in encounter order.
    • toMultimap

      public static <K, V, M extends com.google.common.collect.Multimap<K, V>> Collector<Map.Entry<K,V>,?,M> toMultimap(Supplier<M> multimapFactory)
      Returns a Collector that accumulates Map.Entry<K, V> input elements into a Multimap<K, V> of the supplied type.
      Type Parameters:
      K - the type of map keys
      V - the type of map values
      M - a supplier of your multimap type
      Parameters:
      multimapFactory - a Supplier which return a new, empty Multimap of the appropriate type.
      Returns:
      a Collector which collects map entry elements into a Multiamp, in encounter order.