Class MultiKeyMap
- java.lang.Object
-
- org.apache.commons.collections.map.MultiKeyMap
-
- All Implemented Interfaces:
Serializable
,Map
,IterableMap
public class MultiKeyMap extends Object implements IterableMap, Serializable
AMap
implementation that uses multiple keys to map the value.This class is the most efficient way to uses multiple keys to map to a value. The best way to use this class is via the additional map-style methods. These provide
get
,containsKey
,put
andremove
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 useput()
with any other object type aClassCastException
is thrown. If you try to usenull
as the key input()
aNullPointerException
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
andReferenceIdentityMap
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; }
Note that MultiKeyMap is not synchronized and is not thread-safe. If you wish to use this map from multiple threads concurrently, you must use appropriate synchronization. This class may throw exceptions when accessed by concurrent threads without synchronization.
- Since:
- Commons Collections 3.1
- Version:
- $Revision: 646777 $ $Date: 2008-04-10 14:33:15 +0200 (Thu, 10 Apr 2008) $
- Author:
- Stephen Colebourne
- See Also:
- Serialized Form
-
-
Field Summary
Fields Modifier and Type Field Description protected AbstractHashedMap
map
The decorated map
-
Constructor Summary
Constructors Modifier Constructor Description MultiKeyMap()
Constructs a new MultiKeyMap that decorates aHashedMap
.protected
MultiKeyMap(AbstractHashedMap map)
Constructor that decorates the specified map and is called fromdecorate(AbstractHashedMap)
.
-
Method Summary
All Methods Static Methods Instance Methods Concrete Methods Modifier and Type Method Description 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(Object key)
boolean
containsKey(Object key1, Object key2)
Checks whether the map contains the specified multi-key.boolean
containsKey(Object key1, Object key2, Object key3)
Checks whether the map contains the specified multi-key.boolean
containsKey(Object key1, Object key2, Object key3, Object key4)
Checks whether the map contains the specified multi-key.boolean
containsKey(Object key1, Object key2, Object key3, Object key4, Object key5)
Checks whether the map contains the specified multi-key.boolean
containsValue(Object value)
static MultiKeyMap
decorate(AbstractHashedMap map)
Decorates the specified map to add the MultiKeyMap API and fast query.Set
entrySet()
boolean
equals(Object obj)
Object
get(Object key)
Object
get(Object key1, Object key2)
Gets the value mapped to the specified multi-key.Object
get(Object key1, Object key2, Object key3)
Gets the value mapped to the specified multi-key.Object
get(Object key1, Object key2, Object key3, Object key4)
Gets the value mapped to the specified multi-key.Object
get(Object key1, Object key2, Object key3, Object key4, Object key5)
Gets the value mapped to the specified multi-key.protected int
hash(Object key1, Object key2)
Gets the hash code for the specified multi-key.protected int
hash(Object key1, Object key2, Object key3)
Gets the hash code for the specified multi-key.protected int
hash(Object key1, Object key2, Object key3, Object key4)
Gets the hash code for the specified multi-key.protected int
hash(Object key1, Object key2, Object key3, Object key4, Object key5)
Gets the hash code for the specified multi-key.int
hashCode()
boolean
isEmpty()
protected boolean
isEqualKey(AbstractHashedMap.HashEntry entry, Object key1, Object key2)
Is the key equal to the combined key.protected boolean
isEqualKey(AbstractHashedMap.HashEntry entry, Object key1, Object key2, Object key3)
Is the key equal to the combined key.protected boolean
isEqualKey(AbstractHashedMap.HashEntry entry, Object key1, Object key2, Object key3, Object key4)
Is the key equal to the combined key.protected boolean
isEqualKey(AbstractHashedMap.HashEntry entry, Object key1, Object key2, Object key3, Object key4, Object key5)
Is the key equal to the combined key.Set
keySet()
MapIterator
mapIterator()
Obtains aMapIterator
over the map.Object
put(Object key, Object value)
Puts the key and value into the map, where the key must be a non-null MultiKey object.Object
put(Object key1, Object key2, Object value)
Stores the value against the specified multi-key.Object
put(Object key1, Object key2, Object key3, Object value)
Stores the value against the specified multi-key.Object
put(Object key1, Object key2, Object key3, Object key4, Object value)
Stores the value against the specified multi-key.Object
put(Object key1, Object key2, Object key3, Object key4, Object key5, Object value)
Stores the value against the specified multi-key.void
putAll(Map mapToCopy)
Copies all of the keys and values from the specified map to this map.Object
remove(Object key)
Object
remove(Object key1, Object key2, Object key3)
Removes the specified multi-key from this map.Object
remove(Object key1, Object key2, Object key3, Object key4)
Removes the specified multi-key from this map.Object
remove(Object key1, Object key2, Object key3, Object key4, Object key5)
Removes the specified multi-key from this map.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.Object
removeMultiKey(Object key1, Object key2)
Removes the specified multi-key from this map.int
size()
String
toString()
Collection
values()
-
Methods inherited from class java.lang.Object
finalize, getClass, notify, notifyAll, wait, wait, wait
-
Methods inherited from interface java.util.Map
compute, computeIfAbsent, computeIfPresent, forEach, getOrDefault, merge, putIfAbsent, remove, replace, replace, replaceAll
-
-
-
-
Field Detail
-
map
protected final AbstractHashedMap map
The decorated map
-
-
Constructor Detail
-
MultiKeyMap
public MultiKeyMap()
Constructs a new MultiKeyMap that decorates aHashedMap
.
-
MultiKeyMap
protected MultiKeyMap(AbstractHashedMap map)
Constructor that decorates the specified map and is called fromdecorate(AbstractHashedMap)
. The map must not be null and should be empty or only contain valid keys. This constructor performs no validation.- Parameters:
map
- the map to decorate
-
-
Method Detail
-
decorate
public static MultiKeyMap decorate(AbstractHashedMap map)
Decorates the specified map to add the MultiKeyMap API and fast query. The map must not be null and must be empty.- Parameters:
map
- the map to decorate, not null- Throws:
IllegalArgumentException
- if the map is null or not empty
-
get
public Object get(Object key1, Object key2)
Gets the value mapped to the specified multi-key.- Parameters:
key1
- the first keykey2
- the second key- Returns:
- the mapped value, null if no match
-
containsKey
public boolean containsKey(Object key1, Object key2)
Checks whether the map contains the specified multi-key.- Parameters:
key1
- the first keykey2
- the second key- Returns:
- true if the map contains the key
-
put
public Object put(Object key1, Object key2, Object value)
Stores the value against the specified multi-key.- Parameters:
key1
- the first keykey2
- the second keyvalue
- the value to store- Returns:
- the value previously mapped to this combined key, null if none
-
removeMultiKey
public Object removeMultiKey(Object key1, Object key2)
Removes the specified multi-key from this map.- Parameters:
key1
- the first keykey2
- the second key- Returns:
- the value mapped to the removed key, null if key not in map
-
hash
protected int hash(Object key1, Object key2)
Gets the hash code for the specified multi-key.- Parameters:
key1
- the first keykey2
- the second key- Returns:
- the hash code
-
isEqualKey
protected boolean isEqualKey(AbstractHashedMap.HashEntry entry, Object key1, Object key2)
Is the key equal to the combined key.- Parameters:
entry
- the entry to compare tokey1
- the first keykey2
- the second key- Returns:
- true if the key matches
-
get
public Object get(Object key1, Object key2, Object key3)
Gets the value mapped to the specified multi-key.- Parameters:
key1
- the first keykey2
- the second keykey3
- the third key- Returns:
- the mapped value, null if no match
-
containsKey
public boolean containsKey(Object key1, Object key2, Object key3)
Checks whether the map contains the specified multi-key.- Parameters:
key1
- the first keykey2
- the second keykey3
- the third key- Returns:
- true if the map contains the key
-
put
public Object put(Object key1, Object key2, Object key3, Object value)
Stores the value against the specified multi-key.- Parameters:
key1
- the first keykey2
- the second keykey3
- the third keyvalue
- the value to store- Returns:
- the value previously mapped to this combined key, null if none
-
remove
public Object remove(Object key1, Object key2, Object key3)
Removes the specified multi-key from this map.- Parameters:
key1
- the first keykey2
- the second keykey3
- the third key- Returns:
- the value mapped to the removed key, null if key not in map
-
hash
protected int hash(Object key1, Object key2, Object key3)
Gets the hash code for the specified multi-key.- Parameters:
key1
- the first keykey2
- the second keykey3
- the third key- Returns:
- the hash code
-
isEqualKey
protected boolean isEqualKey(AbstractHashedMap.HashEntry entry, Object key1, Object key2, Object key3)
Is the key equal to the combined key.- Parameters:
entry
- the entry to compare tokey1
- the first keykey2
- the second keykey3
- the third key- Returns:
- true if the key matches
-
get
public Object get(Object key1, Object key2, Object key3, Object key4)
Gets the value mapped to the specified multi-key.- Parameters:
key1
- the first keykey2
- the second keykey3
- the third keykey4
- the fourth key- Returns:
- the mapped value, null if no match
-
containsKey
public boolean containsKey(Object key1, Object key2, Object key3, Object key4)
Checks whether the map contains the specified multi-key.- Parameters:
key1
- the first keykey2
- the second keykey3
- the third keykey4
- the fourth key- Returns:
- true if the map contains the key
-
put
public Object put(Object key1, Object key2, Object key3, Object key4, Object value)
Stores the value against the specified multi-key.- Parameters:
key1
- the first keykey2
- the second keykey3
- the third keykey4
- the fourth keyvalue
- the value to store- Returns:
- the value previously mapped to this combined key, null if none
-
remove
public Object remove(Object key1, Object key2, Object key3, Object key4)
Removes the specified multi-key from this map.- Parameters:
key1
- the first keykey2
- the second keykey3
- the third keykey4
- the fourth key- Returns:
- the value mapped to the removed key, null if key not in map
-
hash
protected int hash(Object key1, Object key2, Object key3, Object key4)
Gets the hash code for the specified multi-key.- Parameters:
key1
- the first keykey2
- the second keykey3
- the third keykey4
- the fourth key- Returns:
- the hash code
-
isEqualKey
protected boolean isEqualKey(AbstractHashedMap.HashEntry entry, Object key1, Object key2, Object key3, Object key4)
Is the key equal to the combined key.- Parameters:
entry
- the entry to compare tokey1
- the first keykey2
- the second keykey3
- the third keykey4
- the fourth key- Returns:
- true if the key matches
-
get
public Object get(Object key1, Object key2, Object key3, Object key4, Object key5)
Gets the value mapped to the specified multi-key.- Parameters:
key1
- the first keykey2
- the second keykey3
- the third keykey4
- the fourth keykey5
- the fifth key- Returns:
- the mapped value, null if no match
-
containsKey
public boolean containsKey(Object key1, Object key2, Object key3, Object key4, Object key5)
Checks whether the map contains the specified multi-key.- Parameters:
key1
- the first keykey2
- the second keykey3
- the third keykey4
- the fourth keykey5
- the fifth key- Returns:
- true if the map contains the key
-
put
public Object put(Object key1, Object key2, Object key3, Object key4, Object key5, Object value)
Stores the value against the specified multi-key.- Parameters:
key1
- the first keykey2
- the second keykey3
- the third keykey4
- the fourth keykey5
- the fifth keyvalue
- the value to store- Returns:
- the value previously mapped to this combined key, null if none
-
remove
public Object remove(Object key1, Object key2, Object key3, Object key4, Object key5)
Removes the specified multi-key from this map.- Parameters:
key1
- the first keykey2
- the second keykey3
- the third keykey4
- the fourth keykey5
- the fifth key- Returns:
- the value mapped to the removed key, null if key not in map
-
hash
protected int hash(Object key1, Object key2, Object key3, Object key4, Object key5)
Gets the hash code for the specified multi-key.- Parameters:
key1
- the first keykey2
- the second keykey3
- the third keykey4
- the fourth keykey5
- the fifth key- Returns:
- the hash code
-
isEqualKey
protected boolean isEqualKey(AbstractHashedMap.HashEntry entry, Object key1, Object key2, Object key3, Object key4, Object key5)
Is the key equal to the combined key.- Parameters:
entry
- the entry to compare tokey1
- the first keykey2
- the second keykey3
- the third keykey4
- the fourth keykey5
- the fifth key- Returns:
- true if the key matches
-
removeAll
public boolean removeAll(Object key1)
Removes all mappings where the first key is that specified.This method removes all the mappings where the
MultiKey
has one or more keys, and the first matches that specified.- Parameters:
key1
- the first key- Returns:
- true if any elements were removed
-
removeAll
public boolean removeAll(Object key1, Object key2)
Removes all mappings where the first two keys are those specified.This method removes all the mappings where the
MultiKey
has two or more keys, and the first two match those specified.- Parameters:
key1
- the first keykey2
- the second key- Returns:
- true if any elements were removed
-
removeAll
public boolean removeAll(Object key1, Object key2, Object key3)
Removes all mappings where the first three keys are those specified.This method removes all the mappings where the
MultiKey
has three or more keys, and the first three match those specified.- Parameters:
key1
- the first keykey2
- the second keykey3
- the third key- Returns:
- true if any elements were removed
-
removeAll
public boolean removeAll(Object key1, Object key2, Object key3, Object key4)
Removes all mappings where the first four keys are those specified.This method removes all the mappings where the
MultiKey
has four or more keys, and the first four match those specified.- Parameters:
key1
- the first keykey2
- the second keykey3
- the third keykey4
- the fourth key- Returns:
- true if any elements were removed
-
checkKey
protected void checkKey(Object key)
Check to ensure that input keys are valid MultiKey objects.- Parameters:
key
- the key to check
-
clone
public Object clone()
Clones the map without cloning the keys or values.
-
put
public Object put(Object key, Object value)
Puts the key and value into the map, where the key must be a non-null MultiKey object.- Specified by:
put
in interfaceMap
- Parameters:
key
- the non-null MultiKey objectvalue
- the value to store- Returns:
- the previous value for the key
- Throws:
NullPointerException
- if the key is nullClassCastException
- if the key is not a MultiKey
-
putAll
public void putAll(Map mapToCopy)
Copies all of the keys and values from the specified map to this map. Each key must be non-null and a MultiKey object.- Specified by:
putAll
in interfaceMap
- Parameters:
mapToCopy
- to this map- Throws:
NullPointerException
- if the mapToCopy or any key within is nullClassCastException
- if any key in mapToCopy is not a MultiKey
-
mapIterator
public MapIterator mapIterator()
Description copied from interface:IterableMap
Obtains aMapIterator
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"); }
- Specified by:
mapIterator
in interfaceIterableMap
- Returns:
- a map iterator
-
containsKey
public boolean containsKey(Object key)
- Specified by:
containsKey
in interfaceMap
-
containsValue
public boolean containsValue(Object value)
- Specified by:
containsValue
in interfaceMap
-
values
public Collection values()
-
equals
public boolean equals(Object obj)
-
hashCode
public int hashCode()
-
-