ch.javasoft.util.map
Class DefaultMultiValueMap<K,V>

java.lang.Object
  extended by ch.javasoft.util.map.AbstractMultiValueMap<K,V>
      extended by ch.javasoft.util.map.AbstractMutableMultiValueMap<K,V>
          extended by ch.javasoft.util.map.DefaultMultiValueMap<K,V>
All Implemented Interfaces:
MultiValueMap<K,V>, Serializable, Cloneable

public class DefaultMultiValueMap<K,V>
extends AbstractMutableMultiValueMap<K,V>
implements Cloneable, Serializable

Default implementation for a mutable MultiValueMap.

The standard implementation is backed by a LinkedHashMap containing an ArrayList per key. The backing map and collection can be changed by overriding createMap() and createCollection(Object), respectively.

See Also:
Serialized Form

Constructor Summary
DefaultMultiValueMap()
          Constructor for an empty multi value map
DefaultMultiValueMap(Map<K,Collection<V>> collectionMap)
          Constructor for a multi value map based on the given collection map.
DefaultMultiValueMap(MultiValueMap<? extends K,? extends V> copy)
          Constructor for a multi value map based on an existing one
 
Method Summary
 boolean add(K key, V value)
          Adds the given value to the multi value map
 boolean addAll(K key, Collection<? extends V> values)
          Adds the given values to the key collection
 boolean clear()
          Removes all entries from this mapping
 DefaultMultiValueMap<K,V> clone()
           
 boolean contains(Object key)
          Returns true if the value collection is non-empty
 boolean contains(Object key, V value)
          Returns true if the value collection contains at least one occurrence of the given value
 int count(Object key)
          Returns the number of values in the value collection
protected  Collection<V> createCollection(K key)
          Returns a new collection instance.
static
<K,V> DefaultMultiValueMap<K,V>
createFromCollectionMap(Map<? extends K,? extends Collection<? extends V>> collectionMap)
          Creates a multi value map from an existing collection map.
static
<K,V> DefaultMultiValueMap<K,V>
createFromSingleValueMap(Map<? extends K,? extends V> singleMap)
          Creates a multi value map from an existing (single value) map
protected  Map<K,Collection<V>> createMap()
          Returns a new map instance.
 Collection<V> get(Object key)
          Returns an unmodifiable value collection.
 V getFirst(Object key)
          Returns the first value of the value collection, or null if no such value exists
protected  Collection<V> getOrCreateCollection(K key)
          Returns the value collection for the specified key.
 boolean isEmpty()
          Returns true if this map is empty
 Set<K> keySet()
          Returns an modifiable key set, i.e.
 boolean remove(Object key)
          Returns all values belonging to this key, i.e.
 boolean remove(Object key, V value)
          Returns a single value of the value collection, if it exists
 Iterable<V> values()
          Returns an unmodifiable iterator for all values
 
Methods inherited from class ch.javasoft.util.map.AbstractMutableMultiValueMap
addAll, addAll, addAll, addAllNested
 
Methods inherited from class ch.javasoft.util.map.AbstractMultiValueMap
asCollectionMap, asSingleValueMap, equals, hashCode, invert, keySize, toString, valueSize
 
Methods inherited from class java.lang.Object
finalize, getClass, notify, notifyAll, wait, wait, wait
 

Constructor Detail

DefaultMultiValueMap

public DefaultMultiValueMap()
Constructor for an empty multi value map


DefaultMultiValueMap

public DefaultMultiValueMap(MultiValueMap<? extends K,? extends V> copy)
Constructor for a multi value map based on an existing one


DefaultMultiValueMap

public DefaultMultiValueMap(Map<K,Collection<V>> collectionMap)
Constructor for a multi value map based on the given collection map. The submitted map and its contents are not copied, thus, changes to it will also affect the newly created multi value map instance. To copy the submitted map, createFromCollectionMap(Map) can be used.

Parameters:
collectionMap - the map to use for this multi value map
Method Detail

createFromSingleValueMap

public static <K,V> DefaultMultiValueMap<K,V> createFromSingleValueMap(Map<? extends K,? extends V> singleMap)
Creates a multi value map from an existing (single value) map

Type Parameters:
K - the key type
V - the value type
Parameters:
singleMap - the map containing key/value pairs to add to the new map
Returns:
a new multi value map containing the entries of the submitted single value map

createFromCollectionMap

public static <K,V> DefaultMultiValueMap<K,V> createFromCollectionMap(Map<? extends K,? extends Collection<? extends V>> collectionMap)
Creates a multi value map from an existing collection map.

Type Parameters:
K - the key type
V - the value type
Parameters:
collectionMap - the map containing key/list-of-value entries to add to the new map
Returns:
a new multi value map containing the entries of the submitted collection map

add

public boolean add(K key,
                   V value)
Adds the given value to the multi value map

Specified by:
add in interface MultiValueMap<K,V>
Overrides:
add in class AbstractMultiValueMap<K,V>
Parameters:
key - the key which identifies the value collection
value - the value to add
Returns:
true if this map has changed, which is usually the case. If sets are used as key collections, however, the method could also return false if the given value already existed.
Throws:
UnsupportedOperationException - if this is an immutable map

addAll

public boolean addAll(K key,
                      Collection<? extends V> values)
Description copied from class: AbstractMutableMultiValueMap
Adds the given values to the key collection

Specified by:
addAll in interface MultiValueMap<K,V>
Overrides:
addAll in class AbstractMutableMultiValueMap<K,V>
Parameters:
key - the key which identifies the value collection
values - the values to add
Returns:
true if this map has changed as a consequence of this operation

get

public Collection<V> get(Object key)
Returns an unmodifiable value collection.

Specified by:
get in interface MultiValueMap<K,V>
Parameters:
key - the key which identifies the value collection
Returns:
the value collection for the given key, never null, and non-empty for existing keys
See Also:
MultiValueMap.get(Object)

getFirst

public V getFirst(Object key)
Description copied from interface: MultiValueMap
Returns the first value of the value collection, or null if no such value exists

Specified by:
getFirst in interface MultiValueMap<K,V>
Overrides:
getFirst in class AbstractMultiValueMap<K,V>
Parameters:
key - the key which identifies the value collection
Returns:
the first value of the value collection, or null if no such value exists

count

public int count(Object key)
Description copied from interface: MultiValueMap
Returns the number of values in the value collection

Specified by:
count in interface MultiValueMap<K,V>
Parameters:
key - the key which identifies the value collection
Returns:
the number of values in the value collection

isEmpty

public boolean isEmpty()
Description copied from interface: MultiValueMap
Returns true if this map is empty

Specified by:
isEmpty in interface MultiValueMap<K,V>
Overrides:
isEmpty in class AbstractMultiValueMap<K,V>
Returns:
true if this map is empty

contains

public boolean contains(Object key)
Description copied from interface: MultiValueMap
Returns true if the value collection is non-empty

Specified by:
contains in interface MultiValueMap<K,V>
Overrides:
contains in class AbstractMultiValueMap<K,V>
Parameters:
key - the key which identifies the value collection
Returns:
true if at least one value exists in the value collection

contains

public boolean contains(Object key,
                        V value)
Description copied from interface: MultiValueMap
Returns true if the value collection contains at least one occurrence of the given value

Specified by:
contains in interface MultiValueMap<K,V>
Parameters:
key - the key which identifies the value collection
value - the value to look for
Returns:
true if at least one such value exists in the value collection

remove

public boolean remove(Object key)
Returns all values belonging to this key, i.e. the value collection is cleared

Specified by:
remove in interface MultiValueMap<K,V>
Overrides:
remove in class AbstractMultiValueMap<K,V>
Parameters:
key - the key which identifies the value collection
Returns:
true if this map has changed as a consequence of this operation
Throws:
UnsupportedOperationException - if this is an immutable map

remove

public boolean remove(Object key,
                      V value)
Returns a single value of the value collection, if it exists

Specified by:
remove in interface MultiValueMap<K,V>
Overrides:
remove in class AbstractMultiValueMap<K,V>
Parameters:
key - the key which identifies the value collection
value - the value to remove
Returns:
true if this map has changed as a consequence of this operation
Throws:
UnsupportedOperationException - if this is an immutable map

clear

public boolean clear()
Removes all entries from this mapping

Specified by:
clear in interface MultiValueMap<K,V>
Overrides:
clear in class AbstractMultiValueMap<K,V>
Returns:
true if this mapping has changed as a consequence of this operation
Throws:
UnsupportedOperationException - if this is an immutable map

keySet

public Set<K> keySet()
Returns an modifiable key set, i.e. removal of keys is also reflected in this multi value map

Specified by:
keySet in interface MultiValueMap<K,V>
Returns:
the set of keys identifying non-empty value collections
See Also:
MultiValueMap.get(Object)

values

public Iterable<V> values()
Returns an unmodifiable iterator for all values

Specified by:
values in interface MultiValueMap<K,V>
Overrides:
values in class AbstractMultiValueMap<K,V>
Returns:
an iterator for all values contained in this mapping
See Also:
MultiValueMap.values()

clone

public DefaultMultiValueMap<K,V> clone()
Overrides:
clone in class Object

getOrCreateCollection

protected Collection<V> getOrCreateCollection(K key)
Returns the value collection for the specified key. If no such collection exists, a new collection is created and added to the backing map.

Parameters:
key - the key identifying the value collection
Returns:
the value collection

createCollection

protected Collection<V> createCollection(K key)
Returns a new collection instance. By default, an ArrayList of size one is created. The method shall never return null.

Parameters:
key - the key for which a value collection is to be created
Returns:
the new array list instance

createMap

protected Map<K,Collection<V>> createMap()
Returns a new map instance. By default, a LinkedHashMap is created. The method shall never return null.

Returns:
the new linked hash map instance