ch.javasoft.util.map
Class DefaultIntIntMultiValueMap

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

public class DefaultIntIntMultiValueMap
extends AbstractMutableMultiValueMap<Integer,Integer>
implements IntIntMultiValueMap, Cloneable, Serializable

The IntIntMultiValueMap is a mutable multi value map for int keys and values.

See Also:
Serialized Form

Constructor Summary
DefaultIntIntMultiValueMap()
          Constructor for an empty multi value map
DefaultIntIntMultiValueMap(DefaultIntIntMultiValueMap copy)
          Constructor for a multi value map based on an existing one
DefaultIntIntMultiValueMap(IntMap<IntCollection> collectionMap)
          Constructor for a multi value map based on the given collection map.
DefaultIntIntMultiValueMap(MultiValueMap<? extends Integer,? extends Integer> copy)
          Constructor for a multi value map based on an existing one
 
Method Summary
 boolean add(Integer key, Integer value)
          Adds the given value to the multi value map
 boolean add(int key, int value)
          Adds the given value to the key collection
 boolean addAll(Integer key, Collection<? extends Integer> values)
          Adds the given values to the key collection
 boolean addAll(int key, int... values)
          Adds the given values to the key collection
 boolean addAll(int key, IntCollection values)
          Adds the given values to the key collection
 boolean addAll(IntIntMultiValueMap mapping)
          Adds the key/value-collection entries of the given mapping to this key collection
 boolean addAll(IntMap<? extends Integer> map)
          Adds the key/value map entries of the given map to this key collection
 boolean addAllNested(IntMap<? extends IntCollection> map)
          Adds the key/value-collection entries of the given map to this key collection
 IntMap<? extends IntCollection> asCollectionMap()
          Implementation returns an IntHashMap containing DefaultIntList instances.
 IntMap<Integer> asSingleValueMap()
          Implementation returns an IntHashMap containing the single values.
 boolean clear()
          Removes all entries from this mapping
 DefaultIntIntMultiValueMap clone()
           
 boolean contains(int key)
          Returns true if the value collection is non-empty
 boolean contains(int key, int value)
          Returns true if the value collection contains at least one occurrence of the given value
 boolean contains(Object key)
          Returns true if the value collection is non-empty
 boolean contains(Object key, Integer value)
          Returns true if the value collection contains at least one occurrence of the given value
 int count(int key)
          Returns the number of values in the value collection
 int count(Object key)
          Returns the number of values in the value collection
protected  IntCollection createCollection(int key)
          Returns a new int list instance.
static DefaultIntIntMultiValueMap createFromCollectionMap(Map<? extends Integer,? extends Collection<? extends Integer>> collectionMap)
          Creates a multi value map from an existing collection map.
static DefaultIntIntMultiValueMap createFromSingleValueMap(Map<? extends Integer,? extends Integer> singleMap)
          Creates a multi value map from an existing (single value) map
protected  IntMap<IntCollection> createMap()
          Returns a new map instance.
 IntCollection get(int key)
          Returns an unmodifiable value collection.
 IntCollection get(Object key)
          Returns an unmodifiable value collection.
 int getFirst(int key)
          Returns the first value associated with this key, or throws an exception if no such value exists
 Integer getFirst(Object key)
          Returns the first value of the value collection, or null if no such value exists
protected  IntCollection getOrCreateCollection(int key)
          Returns the value collection for the specified key.
 IntIntMultiValueMap invert()
          Implementation returns a new DefaultIntIntMultiValueMap instance.
 boolean isEmpty()
          Returns true if this map is empty
 IntSet keySet()
          Returns an modifiable key set, i.e.
 boolean remove(int key)
          Returns all values belonging to this key, i.e.
 boolean remove(int key, int value)
          Returns a single value of the value collection, if it exists
 boolean remove(Object key)
          Returns all values belonging to this key, i.e.
 boolean remove(Object key, Integer value)
          Returns a single value of the value collection, if it exists
 IntIterable 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
equals, hashCode, keySize, toString, valueSize
 
Methods inherited from class java.lang.Object
finalize, getClass, notify, notifyAll, wait, wait, wait
 
Methods inherited from interface ch.javasoft.util.map.IntIntMultiValueMap
keySize, valueSize
 
Methods inherited from interface ch.javasoft.util.map.MultiValueMap
addAll, addAll, addAll, addAllNested
 

Constructor Detail

DefaultIntIntMultiValueMap

public DefaultIntIntMultiValueMap()
Constructor for an empty multi value map


DefaultIntIntMultiValueMap

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


DefaultIntIntMultiValueMap

public DefaultIntIntMultiValueMap(DefaultIntIntMultiValueMap copy)
Constructor for a multi value map based on an existing one


DefaultIntIntMultiValueMap

public DefaultIntIntMultiValueMap(IntMap<IntCollection> 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 DefaultIntIntMultiValueMap createFromSingleValueMap(Map<? extends Integer,? extends Integer> singleMap)
Creates a multi value map from an existing (single value) map

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 DefaultIntIntMultiValueMap createFromCollectionMap(Map<? extends Integer,? extends Collection<? extends Integer>> collectionMap)
Creates a multi value map from an existing collection map.

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(Integer key,
                   Integer value)
Adds the given value to the multi value map

Specified by:
add in interface MultiValueMap<Integer,Integer>
Overrides:
add in class AbstractMultiValueMap<Integer,Integer>
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

add

public boolean add(int key,
                   int value)
Description copied from interface: IntIntMultiValueMap
Adds the given value to the key collection

Specified by:
add in interface IntIntMultiValueMap
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.

addAll

public boolean addAll(int key,
                      IntCollection values)
Description copied from interface: IntIntMultiValueMap
Adds the given values to the key collection

Specified by:
addAll in interface IntIntMultiValueMap
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

addAll

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

Specified by:
addAll in interface MultiValueMap<Integer,Integer>
Overrides:
addAll in class AbstractMutableMultiValueMap<Integer,Integer>
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

addAll

public boolean addAll(int key,
                      int... values)
Description copied from interface: IntIntMultiValueMap
Adds the given values to the key collection

Specified by:
addAll in interface IntIntMultiValueMap
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

addAll

public boolean addAll(IntMap<? extends Integer> map)
Description copied from interface: IntIntMultiValueMap
Adds the key/value map entries of the given map to this key collection

Specified by:
addAll in interface IntIntMultiValueMap
Parameters:
map - the map containing key/value-collection entries to add
Returns:
true if this map has changed as a consequence of this operation

addAll

public boolean addAll(IntIntMultiValueMap mapping)
Description copied from interface: IntIntMultiValueMap
Adds the key/value-collection entries of the given mapping to this key collection

Specified by:
addAll in interface IntIntMultiValueMap
Parameters:
mapping - the mapping containing key/value-collection entries to add
Returns:
true if this map has changed as a consequence of this operation

addAllNested

public boolean addAllNested(IntMap<? extends IntCollection> map)
Description copied from interface: IntIntMultiValueMap
Adds the key/value-collection entries of the given map to this key collection

Specified by:
addAllNested in interface IntIntMultiValueMap
Parameters:
map - the map containing key/value-collection entries to add
Returns:
true if this map has changed as a consequence of this operation

get

public IntCollection get(Object key)
Returns an unmodifiable value collection.

Specified by:
get in interface MultiValueMap<Integer,Integer>
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)

get

public IntCollection get(int key)
Returns an unmodifiable value collection.

Specified by:
get in interface IntIntMultiValueMap
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 Integer 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<Integer,Integer>
Overrides:
getFirst in class AbstractMultiValueMap<Integer,Integer>
Parameters:
key - the key which identifies the value collection
Returns:
the first value of the value collection, or null if no such value exists

getFirst

public int getFirst(int key)
Returns the first value associated with this key, or throws an exception if no such value exists

Specified by:
getFirst in interface IntIntMultiValueMap
Parameters:
key - the key to search for
Returns:
the first value associated with this key
Throws:
NoSuchElementException - if no value exists for the given key

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<Integer,Integer>
Parameters:
key - the key which identifies the value collection
Returns:
the number of values in the value collection

count

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

Specified by:
count in interface IntIntMultiValueMap
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 IntIntMultiValueMap
Specified by:
isEmpty in interface MultiValueMap<Integer,Integer>
Overrides:
isEmpty in class AbstractMultiValueMap<Integer,Integer>
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<Integer,Integer>
Overrides:
contains in class AbstractMultiValueMap<Integer,Integer>
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(int key)
Description copied from interface: IntIntMultiValueMap
Returns true if the value collection is non-empty

Specified by:
contains in interface IntIntMultiValueMap
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,
                        Integer 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<Integer,Integer>
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

contains

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

Specified by:
contains in interface IntIntMultiValueMap
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<Integer,Integer>
Overrides:
remove in class AbstractMultiValueMap<Integer,Integer>
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(int key)
Description copied from interface: IntIntMultiValueMap
Returns all values belonging to this key, i.e. the value collection is cleared

Specified by:
remove in interface IntIntMultiValueMap
Parameters:
key - the key which identifies the value collection
Returns:
true if this map has changed as a consequence of this operation

remove

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

Specified by:
remove in interface MultiValueMap<Integer,Integer>
Overrides:
remove in class AbstractMultiValueMap<Integer,Integer>
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

remove

public boolean remove(int key,
                      int value)
Description copied from interface: IntIntMultiValueMap
Returns a single value of the value collection, if it exists

Specified by:
remove in interface IntIntMultiValueMap
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

clear

public boolean clear()
Removes all entries from this mapping

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

keySet

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

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

values

public IntIterable values()
Returns an unmodifiable iterator for all values

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

asCollectionMap

public IntMap<? extends IntCollection> asCollectionMap()
Implementation returns an IntHashMap containing DefaultIntList instances.

Changing the returned map does not affect this multi value map.

Specified by:
asCollectionMap in interface IntIntMultiValueMap
Specified by:
asCollectionMap in interface MultiValueMap<Integer,Integer>
Overrides:
asCollectionMap in class AbstractMultiValueMap<Integer,Integer>
Returns:
a map with key/collection-of-value entries
See Also:
MultiValueMap.asCollectionMap()

asSingleValueMap

public IntMap<Integer> asSingleValueMap()
Implementation returns an IntHashMap containing the single values.

Changing the returned map does not affect this multi value map.

Specified by:
asSingleValueMap in interface IntIntMultiValueMap
Specified by:
asSingleValueMap in interface MultiValueMap<Integer,Integer>
Overrides:
asSingleValueMap in class AbstractMultiValueMap<Integer,Integer>
Returns:
a map with key and first associated value
See Also:
MultiValueMap.asSingleValueMap()

invert

public IntIntMultiValueMap invert()
Implementation returns a new DefaultIntIntMultiValueMap instance.

Changing the returned map does not affect this multi value map.

Specified by:
invert in interface IntIntMultiValueMap
Specified by:
invert in interface MultiValueMap<Integer,Integer>
Overrides:
invert in class AbstractMultiValueMap<Integer,Integer>
Returns:
the inverted value-key mapping
See Also:
MultiValueMap.invert()

clone

public DefaultIntIntMultiValueMap clone()
Overrides:
clone in class Object

getOrCreateCollection

protected IntCollection getOrCreateCollection(int 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 IntCollection createCollection(int key)
Returns a new int list 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 IntMap<IntCollection> createMap()
Returns a new map instance. By default, a IntHashMap is returned. The method shall never return null.

Returns:
the new int hash map instance