org.base.apps.util
Class ListMap<K,V>

java.lang.Object
  extended by org.base.apps.util.ListMap<K,V>
All Implemented Interfaces:
Map<K,List<V>>

public class ListMap<K,V>
extends Object
implements Map<K,List<V>>

Map of lists, backed by a HashMap by default.

Author:
Kevan Simpson

Nested Class Summary
 
Nested classes/interfaces inherited from interface java.util.Map
Map.Entry<K,V>
 
Constructor Summary
ListMap()
          Constructs an empty ListMap.
ListMap(Map<K,List<V>> delegate)
          Constructs a ListMap backed by the given Map.
 
Method Summary
 void add(K key, V item)
          Adds the given item to the List mapped to the specified key, creating the list if it does not already exist.
 void add(K key, V item, int index)
          Inserts at index the given item to the List mapped to the specified key, creating the list if it does not already exist.
 void clear()
           
 boolean contains(K key, V item)
          Returns true if the given item is contained in the list mapped to the specified key, return false if the no list is mapped.
 boolean contains(V item)
          Returns true if the given item is contained in any of the mapped lists.
 boolean containsKey(Object key)
           
 boolean containsValue(Object value)
           
 int count()
          Fetches the total number of items in all mapped lists.
 Set<Map.Entry<K,List<V>>> entrySet()
           
 V get(K key, int index)
          Fetches an item at the given position from the List mapped to the specified key, if such a list exists.
 List<V> get(Object key)
           
protected  Map<K,List<V>> getDelegate()
           
 boolean isEmpty()
           
 boolean isEmpty(K key)
          Returns true if the list mapped to the specified key is null or contains zero items.
 Set<K> keySet()
           
 List<V> put(K key, List<V> value)
           
 void putAll(Map<? extends K,? extends List<V>> m)
           
 V remove(K key, int index)
          Removes the item at the given position from the List mapped to the specified key, if such a list exists.
 boolean remove(K key, V item)
          Removes the given item from the List mapped to the specified key, if such a list exists.
 List<V> remove(Object key)
           
protected  void setDelegate(Map<K,List<V>> delegate)
           
 int size()
           
 String toString()
           
 Collection<List<V>> values()
           
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, wait, wait, wait
 
Methods inherited from interface java.util.Map
equals, hashCode
 

Constructor Detail

ListMap

public ListMap()
Constructs an empty ListMap.


ListMap

public ListMap(Map<K,List<V>> delegate)
Constructs a ListMap backed by the given Map.

Method Detail

add

public void add(K key,
                V item)
Adds the given item to the List mapped to the specified key, creating the list if it does not already exist.

Parameters:
key - The key to which the list is mapped.
item - The item to add to the list.

add

public void add(K key,
                V item,
                int index)
Inserts at index the given item to the List mapped to the specified key, creating the list if it does not already exist. Out of bound index values will be coerced to be within the target list's range (based on its size.

Parameters:
key - The key to which the list is mapped.
item - The item to add to the list.
index - The index at which to add the item.

contains

public boolean contains(V item)
Returns true if the given item is contained in any of the mapped lists.

Parameters:
item - The item to compare against all mapped lists.
Returns:
true if the item is contained in any list, else false.

contains

public boolean contains(K key,
                        V item)
Returns true if the given item is contained in the list mapped to the specified key, return false if the no list is mapped.

Parameters:
key - The specified key to fetch a list, if one exists.
item - The item to compare against the list.
Returns:
true if the item is contained in the list, else false.

get

public V get(K key,
             int index)
Fetches an item at the given position from the List mapped to the specified key, if such a list exists.

Parameters:
key - The specified key.
index - The item's location.
Returns:
an item at the given position, if it exists, else null.

isEmpty

public boolean isEmpty(K key)
Returns true if the list mapped to the specified key is null or contains zero items.

Parameters:
key - The specified key.
Returns:
true if the mapped list is null or empty.

remove

public boolean remove(K key,
                      V item)
Removes the given item from the List mapped to the specified key, if such a list exists.

Parameters:
key - The key to which the list is mapped.
item - The item to remove from the list.
Returns:
true if the given item was removed, else false.

remove

public V remove(K key,
                int index)
Removes the item at the given position from the List mapped to the specified key, if such a list exists.

Parameters:
key - The key to which the list is mapped.
index - The item's position in the list.
Returns:
true if the given item was removed, else false.

count

public int count()
Fetches the total number of items in all mapped lists.

Returns:
the total number of items in all mapped lists.

size

public int size()
Specified by:
size in interface Map<K,List<V>>
See Also:
Map.size()

isEmpty

public boolean isEmpty()
Specified by:
isEmpty in interface Map<K,List<V>>
See Also:
Map.isEmpty()

containsKey

public boolean containsKey(Object key)
Specified by:
containsKey in interface Map<K,List<V>>
See Also:
Map.containsKey(java.lang.Object)

containsValue

public boolean containsValue(Object value)
Specified by:
containsValue in interface Map<K,List<V>>
See Also:
Map.containsValue(java.lang.Object)

get

public List<V> get(Object key)
Specified by:
get in interface Map<K,List<V>>
See Also:
Map.get(java.lang.Object)

put

public List<V> put(K key,
                   List<V> value)
Specified by:
put in interface Map<K,List<V>>
See Also:
Map.put(java.lang.Object, java.lang.Object)

remove

public List<V> remove(Object key)
Specified by:
remove in interface Map<K,List<V>>
See Also:
Map.remove(java.lang.Object)

putAll

public void putAll(Map<? extends K,? extends List<V>> m)
Specified by:
putAll in interface Map<K,List<V>>
See Also:
Map.putAll(java.util.Map)

clear

public void clear()
Specified by:
clear in interface Map<K,List<V>>
See Also:
Map.clear()

keySet

public Set<K> keySet()
Specified by:
keySet in interface Map<K,List<V>>
See Also:
Map.keySet()

values

public Collection<List<V>> values()
Specified by:
values in interface Map<K,List<V>>
See Also:
Map.values()

entrySet

public Set<Map.Entry<K,List<V>>> entrySet()
Specified by:
entrySet in interface Map<K,List<V>>
See Also:
Map.entrySet()

toString

public String toString()
Overrides:
toString in class Object
See Also:
Object.toString()

getDelegate

protected Map<K,List<V>> getDelegate()
Returns:
the delegate

setDelegate

protected void setDelegate(Map<K,List<V>> delegate)
Parameters:
delegate - the delegate to set

Please visit Base Apps, hosted on Sourceforge.net.

Copyright 2011, Blue Agate Software Entity (BASE)