Package io.cui.tools.collect
Class MapBuilder<K,V>
- java.lang.Object
-
- io.cui.tools.collect.MapBuilder<K,V>
-
- Type Parameters:
K- the type of keys maintained by this mapV- the type of mapped values
public final class MapBuilder<K,V> extends Object
Overview
Builder for creating
Maps providing some convenience methods. The class writes everything through into the contained collector. Using the default constructor a newly createdHashMapwill be used as collector, but you can pass you own collector as constructor-argument. Of course this should be mutable in order to work.Although not being a
Mapitself it provides the same methods with different semantics -> Builder approach.Standard Usage
MapBuilder<String, String> builder = new MapBuilder<>(); builder.put("key1", "value1").put("key2", "value2"); assertEquals(2, builder.size()); assertMutable(builder.toMutableMap()); assertImmutable(builder.toImmutableMap());Using from()
This methods can be used for ensuring a real copyassertEquals(4, MapBuilder.from("key1", 1, "key2", 2, "key3", 3, "key4", 4).size());- Author:
- Oliver Wolff
-
-
Constructor Summary
Constructors Constructor Description MapBuilder()Default Constructor initializing the collector with anHashMapMapBuilder(Map<K,V> collector)Constructor for MapBuilder.
-
Method Summary
All Methods Static Methods Instance Methods Concrete Methods Modifier and Type Method Description MapBuilder<K,V>clear()Clears the contained collectorbooleancontainsKey(Object key)Returnstrueif this map contains a mapping for the specified key.booleancontainsValue(Object value)Returnstrueif this map maps one or more keys to the specified value.static <K,V>
MapBuilder<K,V>copyFrom(Map<K,V> original)copyFrom.booleanequals(Object o)static <K,V>
MapBuilder<K,V>from(K k1, V v1)Shorthand for creating aMapBuilderfrom a given key/value pairstatic <K,V>
MapBuilder<K,V>from(K k1, V v1, K k2, V v2)Shorthand for creating aMapBuilderfrom given key/value pairsstatic <K,V>
MapBuilder<K,V>from(K k1, V v1, K k2, V v2, K k3, V v3)Shorthand for creating aMapBuilderfrom given key/value pairsstatic <K,V>
MapBuilder<K,V>from(K k1, V v1, K k2, V v2, K k3, V v3, K k4, V v4)Shorthand for creating aMapBuilderfrom given key/value pairsinthashCode()booleanisEmpty()isEmpty.MapBuilder<K,V>put(Map.Entry<? extends K,? extends V> entry)put.MapBuilder<K,V>put(K key, V value)put.MapBuilder<K,V>putAll(Map<? extends K,? extends V> map)putAll.MapBuilder<K,V>putIfNotNull(K key, V value)Puts the entry into the map, if the value is notnull.MapBuilder<K,V>remove(Object key)remove.intsize()size.Map<K,V>toImmutableMap()toImmutableMap.Map<K,V>toMutableMap()toMutableMap.StringtoString()
-
-
-
Constructor Detail
-
MapBuilder
public MapBuilder()
Default Constructor initializing the collector with anHashMap
-
MapBuilder
public MapBuilder(Map<K,V> collector)
Constructor for MapBuilder.
- Parameters:
collector- to be used for storage. Must not be null
-
-
Method Detail
-
size
public int size()
size.
- Returns:
- the number of key-value mappings in this map
- See Also:
Map.size()
-
isEmpty
public boolean isEmpty()
isEmpty.
- Returns:
- true if this map contains no key-value mappings
- See Also:
Map.isEmpty()
-
containsKey
public boolean containsKey(Object key)
Returnstrueif this map contains a mapping for the specified key. More formally, returnstrueif and only if this map contains a mapping for a keyksuch that(key==null ? k==null : key.equals(k)). (There can be at most one such mapping.)- Parameters:
key- key whose presence in this map is to be tested- Returns:
trueif this map contains a mapping for the specified key- See Also:
Map.containsKey(Object)
-
containsValue
public boolean containsValue(Object value)
Returnstrueif this map maps one or more keys to the specified value. More formally, returnstrueif and only if this map contains at least one mapping to a valuevsuch that(value==null ? v==null : value.equals(v)). This operation will probably require time linear in the map size for most implementations of theMapinterface.- Parameters:
value- value whose presence in this map is to be tested- Returns:
trueif this map maps one or more keys to the specified value- See Also:
Map.containsValue(Object)
-
put
public MapBuilder<K,V> put(K key, V value)
put.
- Parameters:
key- to be put as key, must not be emptyvalue- to be put as value, must not be empty- Returns:
- the instance itself in order to use it in a fluent way.
-
putIfNotNull
public MapBuilder<K,V> putIfNotNull(K key, V value)
Puts the entry into the map, if the value is notnull.- Parameters:
key- to be put as key, must not be emptyvalue- to be put as value- Returns:
- the instance itself in order to use it in a fluent way.
-
put
public MapBuilder<K,V> put(Map.Entry<? extends K,? extends V> entry)
put.
- Parameters:
entry- to be put, must notnull- Returns:
- the instance itself in order to use it in a fluent way.
-
remove
public MapBuilder<K,V> remove(Object key)
remove.
- Parameters:
key- to be removed- Returns:
- the instance itself in order to use it in a fluent way.
-
putAll
public MapBuilder<K,V> putAll(Map<? extends K,? extends V> map)
putAll.
- Parameters:
map- to be put- Returns:
- the instance itself in order to use it in a fluent way.
-
clear
public MapBuilder<K,V> clear()
Clears the contained collector- Returns:
- the instance itself in order to use it in a fluent way.
-
toMutableMap
public Map<K,V> toMutableMap()
toMutableMap.
-
toImmutableMap
public Map<K,V> toImmutableMap()
toImmutableMap.
- Returns:
- an immutable
Maprepresentation of the builders content, the actual implementation does not create a copy but provides an unmodifiable view usingCollections.unmodifiableMap(Map)
-
copyFrom
public static <K,V> MapBuilder<K,V> copyFrom(Map<K,V> original)
copyFrom.
- Type Parameters:
K- the type of keys maintained by this mapV- the type of mapped values- Parameters:
original- map used to initialize the contained collector- Returns:
- an instance of
MapBuilderinitialized with a copy of the given Map.
-
from
public static <K,V> MapBuilder<K,V> from(K k1, V v1)
Shorthand for creating aMapBuilderfrom a given key/value pair- Type Parameters:
K- the type of keys maintained by this mapV- the type of mapped values- Parameters:
k1- key to be addedv1- value to be added- Returns:
- an instance of
MapBuilderinitialized with the given key/value pair.
-
from
public static <K,V> MapBuilder<K,V> from(K k1, V v1, K k2, V v2)
Shorthand for creating aMapBuilderfrom given key/value pairs- Type Parameters:
K- the type of keys maintained by this mapV- the type of mapped values- Parameters:
k1- key to be addedv1- value to be addedk2- key to be addedv2- value to be added- Returns:
- an instance of
MapBuilderinitialized with the given key/value pairs.
-
from
public static <K,V> MapBuilder<K,V> from(K k1, V v1, K k2, V v2, K k3, V v3)
Shorthand for creating aMapBuilderfrom given key/value pairs- Type Parameters:
K- the type of keys maintained by this mapV- the type of mapped values- Parameters:
k1- key to be addedv1- value to be addedk2- key to be addedv2- value to be addedk3- key to be addedv3- value to be added- Returns:
- an instance of
MapBuilderinitialized with the given key/value pairs.
-
from
public static <K,V> MapBuilder<K,V> from(K k1, V v1, K k2, V v2, K k3, V v3, K k4, V v4)
Shorthand for creating aMapBuilderfrom given key/value pairs- Type Parameters:
K- the type of keys maintained by this mapV- the type of mapped values- Parameters:
k1- key to be addedv1- value to be addedk2- key to be addedv2- value to be addedk3- key to be addedv3- value to be addedk4- key to be addedv4- value to be added- Returns:
- an instance of
MapBuilderinitialized with the given key/value pairs.
-
-