com.google.common.collect
Class ImmutableBiMapBuilder<K,V>

java.lang.Object
  extended by com.google.common.collect.ImmutableBiMapBuilder<K,V>

public class ImmutableBiMapBuilder<K,V>
extends Object

A convenient way to populate immutable BiMap instances, especially static-final "constant BiMaps". Code such as

   static final BiMap<String,Integer> ENGLISH_TO_INTEGER_BIMAP
       = createNumbersMap();

   static BiMap<String,Integer> createNumbersMap() {
     BiMap<String,Integer> bimap = StandardBiMap.newInstance();
     bimap.put("one", 1);
     bimap.put("two", 2);
     bimap.put("three", 3);
     return StandardBiMap.unmodifiableBiMap(bimap);
   }
 
... can be rewritten far more simply as ...
   static final BiMap<String,Integer> ENGLISH_TO_INTEGER_BIMAP
     = new ImmutableBiMapBuilder<String,Integer>()
       .put("one", 1)
       .put("two", 2)
       .put("three", 3)
       .getBiMap();
 
(Actually, for small immutable bimaps, you can use members of the even-more-convenient Maps.immutableBiMap() family of methods.)

Author:
Alex Dovlecel

Constructor Summary
ImmutableBiMapBuilder()
          Creates a new ImmutableBiMapBuilder with an unspecified expected size.
ImmutableBiMapBuilder(int expectedSize)
          Creates a new ImmutableBiMapBuilder with the given expected size.
 
Method Summary
 BiMap<K,V> getBiMap()
          Returns a newly-created, immutable BiMap instance containing the keys and values that were specified using put().
 ImmutableBiMapBuilder<K,V> put(K key, V value)
          Adds a key-value mapping to the bimap that will be returned by getBiMap().
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Constructor Detail

ImmutableBiMapBuilder

public ImmutableBiMapBuilder()
Creates a new ImmutableBiMapBuilder with an unspecified expected size.


ImmutableBiMapBuilder

public ImmutableBiMapBuilder(int expectedSize)
Creates a new ImmutableBiMapBuilder with the given expected size.

Parameters:
expectedSize - the approximate number of key-value pairs you expect this bimap to contain
Method Detail

put

public ImmutableBiMapBuilder<K,V> put(@Nullable
                                      K key,
                                      @Nullable
                                      V value)
Adds a key-value mapping to the bimap that will be returned by getBiMap().

Parameters:
key - key with which the specified value is to be associated
value - value to be associated with the specified key
Returns:
this bimap builder (to enable call chaining)
Throws:
IllegalStateException - if getBiMap() has already been called

getBiMap

public BiMap<K,V> getBiMap()
Returns a newly-created, immutable BiMap instance containing the keys and values that were specified using put().

Returns:
a new, immutable BiMap instance
Throws:
IllegalStateException - if getBiMap() has already been called