Class Collections.MapBuilder<K,V>

java.lang.Object
eu.bandm.tools.util.java.Collections.MapBuilder<K,V>
Type Parameters:
K - the type of keys
V - the type of values
All Implemented Interfaces:
BiConsumer<K,V>
Enclosing class:
Collections

public static class Collections.MapBuilder<K,V> extends Object implements BiConsumer<K,V>
Helper class for building maps incrementally.

Operations of this class are not thread-safe. Use external synchronization if needed.

  • Constructor Details

    • MapBuilder

      public MapBuilder()
      Creates a new empty instance.
  • Method Details

    • accept

      public void accept(K key, V value)
      Adds an entry.

      Null keys and/or values are not recommended; they may or may not be supported by the map implementation to be used.

      Specified by:
      accept in interface BiConsumer<K,V>
      Parameters:
      key - the key
      value - the value
      Throws:
      IllegalArgumentException - if the given key already has a value
      IllegalStateException - if the close() operation has already been invoked
    • add

      public Collections.MapBuilder<K,V> add(K key, V value)
      Adds an entry.

      Null keys and/or values are not recommended; they may or may not be supported by the map implementation to be used.

      Parameters:
      key - the key
      value - the value
      Returns:
      this
      Throws:
      IllegalArgumentException - if the given key already has a value
      IllegalStateException - if the close() operation has already been invoked
    • close

      public Map<K,V> close()
      Returns the completed map and releases resources.

      This method can be called only once; the map builder is then rendered useless and should be discarded.

      Returns:
      an unmodifiable map containing all the entries previously added
      Throws:
      IllegalStateException - if this operation has already been invoked