util
Class Builders

java.lang.Object
  extended by util.Builders

public final class Builders
extends Object

Utility methods for specifying data concisely in code. The next-best thing to map and collection literals.

The purpose is easiest to see by example:

      Map numbers = Builders.buildMap(
          new TreeMap())
          .put(0, "zero")
          .put(1, "one")
          .put(2, "two")
          .put(3, "three")
          .finishMap();
  


Nested Class Summary
static interface Builders.Builder<C>
          An object with methods that add to a collection and can be chained together.
static interface Builders.CollectionBuilder<E,C extends Collection<?>>
          The return type of buildCollection(C).
static interface Builders.MapBuilder<K,V,C extends Map<?,?>>
          The return type of buildMap(C).
static interface Builders.MultiMapBuilder<K,V,C extends Map<?,?>>
          The return type of buildMultiMap(C).
 
Constructor Summary
Builders()
           
 
Method Summary
static
<E,C extends Collection<? super E>>
Builders.CollectionBuilder<E,C>
buildCollection(C coll)
          Build a collection in one line of code.
static
<K,V,C extends Map<? super K,? super V>>
Builders.MapBuilder<K,V,C>
buildMap(C map)
          Build a map in one line of code.
static
<K,V,C extends Map<? super K,? super List<V>>>
Builders.MultiMapBuilder<K,V,C>
buildMultiMap(C map)
          Build a map from things to immutable lists in one line of code.
static
<E,C extends Collection<? super E>>
C
makeCollection(C coll, E... elems)
          Build a collection in one function call.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Constructor Detail

Builders

public Builders()
Method Detail

buildMap

public static <K,V,C extends Map<? super K,? super V>> Builders.MapBuilder<K,V,C> buildMap(C map)
Build a map in one line of code.

Type Parameters:
K - The type of the keys.
V - The type of the values.
C - The type of the map.
Parameters:
map - The map to build onto.
Returns:
A Builders.MapBuilder whose put() method adds an entry to the map and returns itself so that calls can be chained together.

buildMultiMap

public static <K,V,C extends Map<? super K,? super List<V>>> Builders.MultiMapBuilder<K,V,C> buildMultiMap(C map)
Build a map from things to immutable lists in one line of code.

Type Parameters:
K - The type of the keys.
V - The type of the (individual) values.
C - The type of the map.
Parameters:
map - The map to build onto.
Returns:
A Builders.MapBuilder whose put() method adds an entry to the map and returns itself so that calls can be chained together. All values in the map will be immutable lists.

buildCollection

public static <E,C extends Collection<? super E>> Builders.CollectionBuilder<E,C> buildCollection(C coll)
Build a collection in one line of code.

Type Parameters:
E - The type of the elements.
C - The type of the collection.
Parameters:
coll - The collection to build onto.
Returns:
A Builders.CollectionBuilder whose add() method adds an element to the collection and returns itself so that calls can be chained together.

makeCollection

public static <E,C extends Collection<? super E>> C makeCollection(C coll,
                                                                   E... elems)
Build a collection in one function call.

Type Parameters:
E - The type of the elements.
C - The type of the collection.
Parameters:
coll - The collection to build onto.
elems - Zero or more values to add to the collection.
Returns:
coll, having had each element in elems added to it in order.