|
||||||||||
PREV CLASS NEXT CLASS | FRAMES NO FRAMES | |||||||||
SUMMARY: NESTED | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD |
java.lang.Object org.apache.commons.collections.map.MultiKeyMap<K,V>
public class MultiKeyMap<K,V>
A Map
implementation that uses multiple keys to map the value.
get
, containsKey
, put
and
remove
for individual keys which operate without extra object creation.
The additional methods are the main interface of this map.
As such, you will not normally hold this map in a variable of type Map
.
The normal map methods take in and return a MultiKey
.
If you try to use put()
with any other object type a
ClassCastException
is thrown. If you try to use null
as
the key in put()
a NullPointerException
is thrown.
This map is implemented as a decorator of a AbstractHashedMap
which
enables extra behaviour to be added easily.
MultiKeyMap.decorate(new LinkedMap())
creates an ordered map.
MultiKeyMap.decorate(new LRUMap())
creates an least recently used map.
MultiKeyMap.decorate(new ReferenceMap())
creates a garbage collector sensitive map.
IdentityMap
and ReferenceIdentityMap
are unsuitable
for use as the key comparison would work on the whole MultiKey, not the elements within.
As an example, consider a least recently used cache that uses a String airline code
and a Locale to lookup the airline's name:
private MultiKeyMap cache = MultiKeyMap.decorate(new LRUMap(50)); public String getAirlineName(String code, String locale) { String name = (String) cache.get(code, locale); if (name == null) { name = getAirlineNameFromDB(code, locale); cache.put(code, locale, name); } return name; }
Nested Class Summary |
---|
Nested classes/interfaces inherited from interface java.util.Map |
---|
Map.Entry<K,V> |
Field Summary | |
---|---|
protected AbstractHashedMap<MultiKey<K>,V> |
map
The decorated map |
Constructor Summary | |
---|---|
|
MultiKeyMap()
Constructs a new MultiKeyMap that decorates a HashedMap . |
protected |
MultiKeyMap(AbstractHashedMap<MultiKey<K>,V> map)
Constructor that decorates the specified map and is called from decorate(AbstractHashedMap) . |
Method Summary | ||
---|---|---|
protected void |
checkKey(Object key)
Check to ensure that input keys are valid MultiKey objects. |
|
void |
clear()
|
|
Object |
clone()
Clones the map without cloning the keys or values. |
|
boolean |
containsKey(K... keys)
Checks whether the map contains the specified multi-key. |
|
boolean |
containsKey(Object key)
|
|
boolean |
containsValue(Object value)
|
|
static
|
decorate(AbstractHashedMap<MultiKey<K>,V> map)
Decorates the specified map to add the MultiKeyMap API and fast query. |
|
Set |
entrySet()
|
|
boolean |
equals(Object obj)
|
|
V |
get(K... keys)
Gets the value mapped to the specified multi-key. |
|
V |
get(Object key)
|
|
protected int |
hash(K... keys)
Gets the hash code for the specified multi-key. |
|
int |
hashCode()
|
|
boolean |
isEmpty()
|
|
protected boolean |
isEqualKey(AbstractHashedMap.HashEntry<MultiKey<K>,V> entry,
K... keys)
Is the key equal to the combined key. |
|
Set |
keySet()
|
|
MapIterator |
mapIterator()
Obtains a MapIterator over the map. |
|
V |
put(K key1,
K key2,
K key3,
K key4,
K key5,
V value)
For backwards compatibility, makes a call to the new varargs putMultiKey(V, K...) |
|
V |
put(K key1,
K key2,
K key3,
K key4,
V value)
For backwards compatibility, makes a call to the new varargs putMultiKey(V, K...) |
|
V |
put(K key1,
K key2,
K key3,
V value)
For backwards compatibility, makes a call to the new varargs putMultiKey(V, K...) |
|
V |
put(K key1,
K key2,
V value)
For backwards compatibility, makes a call to the new varargs putMultiKey(V, K...) |
|
V |
put(MultiKey<K> key,
V value)
Puts the key and value into the map, where the key must be a non-null MultiKey object. |
|
void |
putAll(Map<? extends MultiKey<K>,? extends V> mapToCopy)
Puts all the keys and values into this map. |
|
V |
putMultiKey(V value,
K... keys)
Stores the value against the specified multi-key. |
|
Object |
remove(K... keys)
Removes the specified multi-key from this map. |
|
V |
remove(Object key)
|
|
boolean |
removeAll(Object key1)
Removes all mappings where the first key is that specified. |
|
boolean |
removeAll(Object key1,
Object key2)
Removes all mappings where the first two keys are those specified. |
|
boolean |
removeAll(Object key1,
Object key2,
Object key3)
Removes all mappings where the first three keys are those specified. |
|
boolean |
removeAll(Object key1,
Object key2,
Object key3,
Object key4)
Removes all mappings where the first four keys are those specified. |
|
int |
size()
|
|
String |
toString()
|
|
Collection |
values()
|
Methods inherited from class java.lang.Object |
---|
finalize, getClass, notify, notifyAll, wait, wait, wait |
Field Detail |
---|
protected final AbstractHashedMap<MultiKey<K>,V> map
Constructor Detail |
---|
public MultiKeyMap()
HashedMap
.
protected MultiKeyMap(AbstractHashedMap<MultiKey<K>,V> map)
decorate(AbstractHashedMap)
.
The map must not be null and should be empty or only contain valid keys.
This constructor performs no validation.
map
- the map to decorateMethod Detail |
---|
public static <K,V> MultiKeyMap<K,V> decorate(AbstractHashedMap<MultiKey<K>,V> map)
map
- the map to decorate, not null
IllegalArgumentException
- if the map is null or not emptypublic V get(K... keys)
keys
- the keys
public boolean containsKey(K... keys)
keys
- the keys
public V put(K key1, K key2, V value)
putMultiKey(V, K...)
public V put(K key1, K key2, K key3, V value)
putMultiKey(V, K...)
public V put(K key1, K key2, K key3, K key4, V value)
putMultiKey(V, K...)
public V put(K key1, K key2, K key3, K key4, K key5, V value)
putMultiKey(V, K...)
public V putMultiKey(V value, K... keys)
value
- the value to storekeys
- the keys
public Object remove(K... keys)
keys
- the keys
protected int hash(K... keys)
keys
- the keys
protected boolean isEqualKey(AbstractHashedMap.HashEntry<MultiKey<K>,V> entry, K... keys)
entry
- the entry to compare tokeys
- the keys
public boolean removeAll(Object key1)
MultiKey
has one or more keys, and the first matches that specified.
key1
- the first key
public boolean removeAll(Object key1, Object key2)
MultiKey
has two or more keys, and the first two match those specified.
key1
- the first keykey2
- the second key
public boolean removeAll(Object key1, Object key2, Object key3)
MultiKey
has three or more keys, and the first three match those specified.
key1
- the first keykey2
- the second keykey3
- the third key
public boolean removeAll(Object key1, Object key2, Object key3, Object key4)
MultiKey
has four or more keys, and the first four match those specified.
key1
- the first keykey2
- the second keykey3
- the third keykey4
- the fourth key
protected void checkKey(Object key)
key
- the key to checkpublic Object clone()
clone
in class Object
public V put(MultiKey<K> key, V value)
put
in interface Map<MultiKey<K>,V>
key
- the non-null MultiKey objectvalue
- the value to store
NullPointerException
- if the key is null
ClassCastException
- if the key is not a MultiKeypublic void putAll(Map<? extends MultiKey<K>,? extends V> mapToCopy)
putAll
in interface Map<MultiKey<K>,V>
mapToCopy
- the map to copy in.
NullPointerException
- if the mapToCopy or any key within is null
ClassCastException
- if any key is not a MultiKeypublic MapIterator mapIterator()
IterableMap
MapIterator
over the map.
A map iterator is an efficient way of iterating over maps.
There is no need to access the entry set or cast to Map Entry objects.
IterableMap map = new HashedMap(); MapIterator it = map.mapIterator(); while (it.hasNext()) { Object key = it.next(); Object value = it.getValue(); it.setValue("newValue"); }
mapIterator
in interface IterableMap<MultiKey<K>,V>
public int size()
size
in interface Map<MultiKey<K>,V>
public boolean isEmpty()
isEmpty
in interface Map<MultiKey<K>,V>
public boolean containsKey(Object key)
containsKey
in interface Map<MultiKey<K>,V>
public boolean containsValue(Object value)
containsValue
in interface Map<MultiKey<K>,V>
public V get(Object key)
get
in interface Map<MultiKey<K>,V>
public V remove(Object key)
remove
in interface Map<MultiKey<K>,V>
public void clear()
clear
in interface Map<MultiKey<K>,V>
public Set keySet()
keySet
in interface Map<MultiKey<K>,V>
public Collection values()
values
in interface Map<MultiKey<K>,V>
public Set entrySet()
entrySet
in interface Map<MultiKey<K>,V>
public boolean equals(Object obj)
equals
in interface Map<MultiKey<K>,V>
equals
in class Object
public int hashCode()
hashCode
in interface Map<MultiKey<K>,V>
hashCode
in class Object
public String toString()
toString
in class Object
|
||||||||||
PREV CLASS NEXT CLASS | FRAMES NO FRAMES | |||||||||
SUMMARY: NESTED | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD |