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>
Direct Known Subclasses:
LocationTree.Builder
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 type-safe but not thread-safe. Use external synchronization if needed.

  • Constructor Details

    • MapBuilder

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

    • accept

      @Deprecated(since="1.1", forRemoval=false) public void accept(K key, V value)
      Deprecated.
      Use this::add or this::put instead.
      Adds an entry for a new key.

      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 for a new key.

      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, for method chaining
      Throws:
      IllegalArgumentException - if the given key already has a value
      IllegalStateException - if the close() operation has already been invoked
    • put

      public Collections.MapBuilder<K,V> put(K key, V value)
      Adds or overwrites 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, for method chaining
      Throws:
      IllegalStateException - if the close() operation has already been invoked
      Since:
      1.1
    • 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
    • close

      public Map<K,V> close(Function<? super Map<K,V>,? extends Map<K,V>> create)
      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.

      Parameters:
      create - an operation that creates the result map from a temporary
      Returns:
      a map of the specified kind containing all the entries previously added
      Throws:
      IllegalStateException - if this operation has already been invoked
      NullPointerException - if create is null
      Since:
      1.3