org.jvnet.hk2.component
Class MultiMap<K,V>

java.lang.Object
  extended by org.jvnet.hk2.component.MultiMap<K,V>

public final class MultiMap<K,V>
extends java.lang.Object

Map from a key to multiple values. Order is significant among values, and null values are allowed, although null keys are not.

Author:
Kohsuke Kawaguchi

Constructor Summary
MultiMap()
          Creates an empty multi-map.
MultiMap(MultiMap<K,V> base)
          Copy constructor.
 
Method Summary
 void add(K k, V v)
          Adds one more value.
 MultiMap<K,V> clone()
          Creates a copy of the map that contains the exact same key and value set.
 boolean containsKey(K key)
          Checks if the map contains the given key.
static
<K,V> MultiMap<K,V>
emptyMap()
          Gets the singleton read-only empty multi-map.
 java.util.Set<java.util.Map.Entry<K,java.util.List<V>>> entrySet()
          Lists up all entries.
 java.util.List<V> get(K k)
           
 V getOne(K k)
          Gets the first value if any, or null.
 void set(K k, java.util.Collection<? extends V> v)
          Replaces all the existing values associated with the key by the given value.
 void set(K k, V v)
          Replaces all the existing values associated wit hthe key by the given single value.
 int size()
          Returns the size of the map
 java.lang.String toCommaSeparatedString()
          Format the map as "key=value1,key=value2,...."
 java.lang.String toString()
           
 
Methods inherited from class java.lang.Object
equals, finalize, getClass, hashCode, notify, notifyAll, wait, wait, wait
 

Constructor Detail

MultiMap

public MultiMap()
Creates an empty multi-map.


MultiMap

public MultiMap(MultiMap<K,V> base)
Copy constructor.

Method Detail

toString

public final java.lang.String toString()
Overrides:
toString in class java.lang.Object

add

public final void add(K k,
                      V v)
Adds one more value.


set

public void set(K k,
                java.util.Collection<? extends V> v)
Replaces all the existing values associated with the key by the given value.

Parameters:
v - Can be null or empty.

set

public void set(K k,
                V v)
Replaces all the existing values associated wit hthe key by the given single value.

This is short for set(k,Collections.singleton(v))


get

public final java.util.List<V> get(K k)
Returns:
Can be empty but never null. Read-only.

containsKey

public boolean containsKey(K key)
Checks if the map contains the given key.


getOne

public final V getOne(K k)
Gets the first value if any, or null.

This is useful when you know the given key only has one value and you'd like to get to that value.

Returns:
null if the key has no values or it has a value but the value is null.

entrySet

public java.util.Set<java.util.Map.Entry<K,java.util.List<V>>> entrySet()
Lists up all entries.


toCommaSeparatedString

public java.lang.String toCommaSeparatedString()
Format the map as "key=value1,key=value2,...."


clone

public MultiMap<K,V> clone()
Creates a copy of the map that contains the exact same key and value set. Keys and values won't cloned.

Overrides:
clone in class java.lang.Object

size

public int size()
Returns the size of the map

Returns:
integer or 0 if the map is empty

emptyMap

public static <K,V> MultiMap<K,V> emptyMap()
Gets the singleton read-only empty multi-map.

See Also:
Collections.emptyMap()


Copyright © 2009 Sun Microsystems, Inc.. All Rights Reserved.