|
qflib 0.98.1 | ||||||||
PREV CLASS NEXT CLASS | FRAMES NO FRAMES | ||||||||
SUMMARY: INNER | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD |
java.lang.Object | +--de.qfs.lib.util.MultiMap
This is a special implementation of the Map interface that can store more
than one value per key. When a value is retrieved with get
, an
arbitrary one is returned if more than one is available.
The remove(key)
removes all values stored for
that key. To remove only a single value, use the remove(key,value)
method.
Inner Class Summary | |
protected class |
MultiMap.MapEntry
An implementation of the Map.Entry interface. |
protected class |
MultiMap.MapIterator
Iterator for the key, value and entry collections of the MultiMap. |
static class |
MultiMap.UnitTest
Test cases for the MultiMap class. |
protected static interface |
MultiMap.ValueSet
This interface is a marker for the actual Set used to store multiple values for the same key in a MultiMap. |
Inner classes inherited from class java.util.Map |
java.util.Map.Entry |
Field Summary | |
protected static int |
ENTRIES
Type selector for an entry iterator. |
protected java.util.Set |
entrySet
The Set returned by entrySet . |
protected static int |
KEYS
Type selector for a key iterator. |
protected java.util.Set |
keySet
The Set returned by keySet . |
protected int |
modCounter
Since HashMap's modification counter is private the MultiMap needs its own. |
protected int |
valCount
Number of values stored in the MultiMap. |
protected java.util.Collection |
values
The Collection returned by values . |
protected static int |
VALUES
Type selector for a value iterator. |
Constructor Summary | |
MultiMap()
Create a new MultiMap. |
|
MultiMap(int initialCapacity)
Create a new MultiMap. |
|
MultiMap(java.util.Map map)
Create a new MultiMap and initialize it with all the mappings from the passed map. |
Method Summary | |
protected boolean |
_remove(java.lang.Object key,
java.lang.Object value)
This is the actual implementation of the remove(Object,Object) method. |
protected boolean |
areEqual(java.lang.Object o1,
java.lang.Object o2)
Compare two objects. |
void |
clear()
Removes all mappings from this map. |
java.lang.Object |
clone()
Returns a shallow copy of this MultiMap: the keys and values themselves are not cloned. |
protected java.util.Map |
cloneDelegate()
This method is used by the clone method to create a copy
of the delegate. |
boolean |
containsKey(java.lang.Object key)
Returns true if this map contains a mapping for the specified key. |
boolean |
containsValue(java.lang.Object value)
Returns true if this map maps one or more keys to the specified value. |
protected MultiMap.ValueSet |
createValueSet()
This method creates a new ValueSet to store multiple values for the same key. |
java.util.Set |
entrySet()
Returns a set view of the mappings contained in this map. |
boolean |
equals(java.lang.Object o)
Test whether an object is equal to the MultiMap. |
java.lang.Object |
get(java.lang.Object key)
Returns the value to which this map maps the specified key. |
java.util.Set |
getAll(java.lang.Object key)
Get a Set of all Objects to which this map maps the key. |
int |
hashCode()
Get the hashCode of the MultiMap. |
boolean |
isEmpty()
Returns true if this map contains no key-value mappings. |
java.util.Set |
keySet()
Returns a set view of the keys contained in this map. |
boolean |
mapsToMany(java.lang.Object key)
Query whether a key maps to more than one values. |
java.lang.Object |
put(java.lang.Object key,
java.lang.Object value)
Associates the specified value with the specified key in this map. |
void |
putAll(java.util.Map map)
Copies all of the mappings from the specified map to this map. |
java.lang.Object |
remove(java.lang.Object key)
Removes all mappings for this key from this map if present. |
boolean |
remove(java.lang.Object key,
java.lang.Object value)
In contrast to remove(Object) this method
removes only the one binding for key and value. |
int |
size()
Returns the number of key-value mappings in this map. |
java.util.Collection |
values()
Returns a collection view of the values contained in this map. |
Methods inherited from class java.lang.Object |
finalize, getClass, notify, notifyAll, toString, wait, wait, wait |
Field Detail |
protected static final int KEYS
protected static final int VALUES
protected static final int ENTRIES
protected int valCount
protected transient java.util.Set keySet
keySet
.protected transient java.util.Collection values
values
.protected transient java.util.Set entrySet
entrySet
.protected transient int modCounter
Constructor Detail |
public MultiMap()
public MultiMap(int initialCapacity)
initialCapacity
- The initial capacity of the MultiMap.public MultiMap(java.util.Map map)
map
- The map that initializes this MultiMap.Method Detail |
public boolean isEmpty()
isEmpty
in interface java.util.Map
public int size()
size
in interface java.util.Map
public java.lang.Object get(java.lang.Object key)
get
in interface java.util.Map
key
- key whose associated value is to be returned.containsKey(Object)
public boolean containsKey(java.lang.Object key)
containsKey
in interface java.util.Map
key
- key whose presence in this map is to be tested.public boolean containsValue(java.lang.Object value)
containsValue
in interface java.util.Map
value
- value whose presence in this map is to be tested.public java.lang.Object put(java.lang.Object key, java.lang.Object value)
put
in interface java.util.Map
key
- key with which the specified value is to be associated.value
- value to be associated with the specified key.public void putAll(java.util.Map map)
putAll
in interface java.util.Map
map
- Mappings to be stored in this map.public java.lang.Object remove(java.lang.Object key)
remove
in interface java.util.Map
key
- key whose mapping is to be removed from the map.public void clear()
clear
in interface java.util.Map
public java.util.Set keySet()
keySet
in interface java.util.Map
public java.util.Collection values()
values
in interface java.util.Map
public java.util.Set entrySet()
entrySet
in interface java.util.Map
public boolean equals(java.lang.Object o)
equals
in interface java.util.Map
equals
in class java.lang.Object
o
- The object to compare to.public int hashCode()
hashCode
in interface java.util.Map
hashCode
in class java.lang.Object
public java.lang.Object clone() throws java.lang.ClassCastException
cloneDelegate
,
otherwise this method will fail with a ClassCastException.clone
in class java.lang.Object
java.lang.ClassCastException
- If the delegate is not a HashMap and the
derived class doesn't override cloneDelegate
.public boolean remove(java.lang.Object key, java.lang.Object value)
remove(Object)
this method
removes only the one binding for key and value. Since this method is not part of the standard Map interface, it cannot directly be synchronized like all the others with the help of a Collections.SynchronizedMap. Nevertheless you can use a SynchronizedMap wrapper around the MultiMap for synchronized standard Map access and explicitely synchronize on that SynchronizedMap when calling this method.
key
- The key of the binding to remove.value
- The value of the binding to remove.public boolean mapsToMany(java.lang.Object key)
Since this method is not part of the standard Map interface, it cannot directly be synchronized like all the others with the help of a Collections.SynchronizedMap. Nevertheless you can use a SynchronizedMap wrapper around the MultiMap for synchronized standard Map access and explicitely synchronize on that SynchronizedMap when calling this method.
key
- The key to look for.public java.util.Set getAll(java.lang.Object key)
Since this method is not part of the standard Map interface, it cannot directly be synchronized like all the others with the help of a Collections.SynchronizedMap. Nevertheless you can use a SynchronizedMap wrapper around the MultiMap for synchronized standard Map access and explicitely synchronize on that SynchronizedMap when calling this method.
Note: Though a set backed by the map would have been nice, the implications of modification to the set are not quite clear and difficult to implement. Maybe in a later version.
key
- key whose associated values are to be returned.protected MultiMap.ValueSet createValueSet()
protected java.util.Map cloneDelegate() throws java.lang.ClassCastException
clone
method to create a copy
of the delegate. If a derived class uses a delegate that is not a
HashMap it needs to override this method, otherwise clone
will result in a ClassCastException.java.lang.ClassCastException
- If the delegate is not a HashMap and the
derived class doesn't override cloneDelegate
.protected final boolean areEqual(java.lang.Object o1, java.lang.Object o2)
o1
- The first object.o2
- The second object.protected boolean _remove(java.lang.Object key, java.lang.Object value)
remove(Object,Object)
method.key
- The key of the binding to remove.value
- The value of the binding to remove.
|
qflib 0.98.1 | ||||||||
PREV CLASS NEXT CLASS | FRAMES NO FRAMES | ||||||||
SUMMARY: INNER | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD |