|
||||||||||
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
public class MultiKeyMap
A Map
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
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; }
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.
Nested Class Summary |
---|
Nested classes/interfaces inherited from interface java.util.Map |
---|
java.util.Map.Entry |
Field Summary | |
---|---|
protected AbstractHashedMap |
map
The decorated map |
Constructor Summary | |
---|---|
|
MultiKeyMap()
Constructs a new MultiKeyMap that decorates a HashedMap . |
protected |
MultiKeyMap(AbstractHashedMap map)
Constructor that decorates the specified map and is called from decorate(AbstractHashedMap) . |
Method Summary | |
---|---|
protected void |
checkKey(java.lang.Object key)
Check to ensure that input keys are valid MultiKey objects. |
void |
clear()
|
java.lang.Object |
clone()
Clones the map without cloning the keys or values. |
boolean |
containsKey(java.lang.Object key)
|
boolean |
containsKey(java.lang.Object key1,
java.lang.Object key2)
Checks whether the map contains the specified multi-key. |
boolean |
containsKey(java.lang.Object key1,
java.lang.Object key2,
java.lang.Object key3)
Checks whether the map contains the specified multi-key. |
boolean |
containsKey(java.lang.Object key1,
java.lang.Object key2,
java.lang.Object key3,
java.lang.Object key4)
Checks whether the map contains the specified multi-key. |
boolean |
containsKey(java.lang.Object key1,
java.lang.Object key2,
java.lang.Object key3,
java.lang.Object key4,
java.lang.Object key5)
Checks whether the map contains the specified multi-key. |
boolean |
containsValue(java.lang.Object value)
|
static MultiKeyMap |
decorate(AbstractHashedMap map)
Decorates the specified map to add the MultiKeyMap API and fast query. |
java.util.Set |
entrySet()
|
boolean |
equals(java.lang.Object obj)
|
java.lang.Object |
get(java.lang.Object key)
|
java.lang.Object |
get(java.lang.Object key1,
java.lang.Object key2)
Gets the value mapped to the specified multi-key. |
java.lang.Object |
get(java.lang.Object key1,
java.lang.Object key2,
java.lang.Object key3)
Gets the value mapped to the specified multi-key. |
java.lang.Object |
get(java.lang.Object key1,
java.lang.Object key2,
java.lang.Object key3,
java.lang.Object key4)
Gets the value mapped to the specified multi-key. |
java.lang.Object |
get(java.lang.Object key1,
java.lang.Object key2,
java.lang.Object key3,
java.lang.Object key4,
java.lang.Object key5)
Gets the value mapped to the specified multi-key. |
protected int |
hash(java.lang.Object key1,
java.lang.Object key2)
Gets the hash code for the specified multi-key. |
protected int |
hash(java.lang.Object key1,
java.lang.Object key2,
java.lang.Object key3)
Gets the hash code for the specified multi-key. |
protected int |
hash(java.lang.Object key1,
java.lang.Object key2,
java.lang.Object key3,
java.lang.Object key4)
Gets the hash code for the specified multi-key. |
protected int |
hash(java.lang.Object key1,
java.lang.Object key2,
java.lang.Object key3,
java.lang.Object key4,
java.lang.Object key5)
Gets the hash code for the specified multi-key. |
int |
hashCode()
|
boolean |
isEmpty()
|
protected boolean |
isEqualKey(AbstractHashedMap.HashEntry entry,
java.lang.Object key1,
java.lang.Object key2)
Is the key equal to the combined key. |
protected boolean |
isEqualKey(AbstractHashedMap.HashEntry entry,
java.lang.Object key1,
java.lang.Object key2,
java.lang.Object key3)
Is the key equal to the combined key. |
protected boolean |
isEqualKey(AbstractHashedMap.HashEntry entry,
java.lang.Object key1,
java.lang.Object key2,
java.lang.Object key3,
java.lang.Object key4)
Is the key equal to the combined key. |
protected boolean |
isEqualKey(AbstractHashedMap.HashEntry entry,
java.lang.Object key1,
java.lang.Object key2,
java.lang.Object key3,
java.lang.Object key4,
java.lang.Object key5)
Is the key equal to the combined key. |
java.util.Set |
keySet()
|
MapIterator |
mapIterator()
Obtains a MapIterator over the map. |
java.lang.Object |
put(java.lang.Object key,
java.lang.Object value)
Puts the key and value into the map, where the key must be a non-null MultiKey object. |
java.lang.Object |
put(java.lang.Object key1,
java.lang.Object key2,
java.lang.Object value)
Stores the value against the specified multi-key. |
java.lang.Object |
put(java.lang.Object key1,
java.lang.Object key2,
java.lang.Object key3,
java.lang.Object value)
Stores the value against the specified multi-key. |
java.lang.Object |
put(java.lang.Object key1,
java.lang.Object key2,
java.lang.Object key3,
java.lang.Object key4,
java.lang.Object value)
Stores the value against the specified multi-key. |
java.lang.Object |
put(java.lang.Object key1,
java.lang.Object key2,
java.lang.Object key3,
java.lang.Object key4,
java.lang.Object key5,
java.lang.Object value)
Stores the value against the specified multi-key. |
void |
putAll(java.util.Map mapToCopy)
Copies all of the keys and values from the specified map to this map. |
java.lang.Object |
remove(java.lang.Object key)
|
java.lang.Object |
remove(java.lang.Object key1,
java.lang.Object key2)
Removes the specified multi-key from this map. |
java.lang.Object |
remove(java.lang.Object key1,
java.lang.Object key2,
java.lang.Object key3)
Removes the specified multi-key from this map. |
java.lang.Object |
remove(java.lang.Object key1,
java.lang.Object key2,
java.lang.Object key3,
java.lang.Object key4)
Removes the specified multi-key from this map. |
java.lang.Object |
remove(java.lang.Object key1,
java.lang.Object key2,
java.lang.Object key3,
java.lang.Object key4,
java.lang.Object key5)
Removes the specified multi-key from this map. |
boolean |
removeAll(java.lang.Object key1)
Removes all mappings where the first key is that specified. |
boolean |
removeAll(java.lang.Object key1,
java.lang.Object key2)
Removes all mappings where the first two keys are those specified. |
boolean |
removeAll(java.lang.Object key1,
java.lang.Object key2,
java.lang.Object key3)
Removes all mappings where the first three keys are those specified. |
boolean |
removeAll(java.lang.Object key1,
java.lang.Object key2,
java.lang.Object key3,
java.lang.Object key4)
Removes all mappings where the first four keys are those specified. |
int |
size()
|
java.lang.String |
toString()
|
java.util.Collection |
values()
|
Methods inherited from class java.lang.Object |
---|
finalize, getClass, notify, notifyAll, wait, wait, wait |
Field Detail |
---|
protected final AbstractHashedMap map
Constructor Detail |
---|
public MultiKeyMap()
HashedMap
.
protected MultiKeyMap(AbstractHashedMap 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 MultiKeyMap decorate(AbstractHashedMap map)
map
- the map to decorate, not null
java.lang.IllegalArgumentException
- if the map is null or not emptypublic java.lang.Object get(java.lang.Object key1, java.lang.Object key2)
key1
- the first keykey2
- the second key
public boolean containsKey(java.lang.Object key1, java.lang.Object key2)
key1
- the first keykey2
- the second key
public java.lang.Object put(java.lang.Object key1, java.lang.Object key2, java.lang.Object value)
key1
- the first keykey2
- the second keyvalue
- the value to store
public java.lang.Object remove(java.lang.Object key1, java.lang.Object key2)
key1
- the first keykey2
- the second key
protected int hash(java.lang.Object key1, java.lang.Object key2)
key1
- the first keykey2
- the second key
protected boolean isEqualKey(AbstractHashedMap.HashEntry entry, java.lang.Object key1, java.lang.Object key2)
entry
- the entry to compare tokey1
- the first keykey2
- the second key
public java.lang.Object get(java.lang.Object key1, java.lang.Object key2, java.lang.Object key3)
key1
- the first keykey2
- the second keykey3
- the third key
public boolean containsKey(java.lang.Object key1, java.lang.Object key2, java.lang.Object key3)
key1
- the first keykey2
- the second keykey3
- the third key
public java.lang.Object put(java.lang.Object key1, java.lang.Object key2, java.lang.Object key3, java.lang.Object value)
key1
- the first keykey2
- the second keykey3
- the third keyvalue
- the value to store
public java.lang.Object remove(java.lang.Object key1, java.lang.Object key2, java.lang.Object key3)
key1
- the first keykey2
- the second keykey3
- the third key
protected int hash(java.lang.Object key1, java.lang.Object key2, java.lang.Object key3)
key1
- the first keykey2
- the second keykey3
- the third key
protected boolean isEqualKey(AbstractHashedMap.HashEntry entry, java.lang.Object key1, java.lang.Object key2, java.lang.Object key3)
entry
- the entry to compare tokey1
- the first keykey2
- the second keykey3
- the third key
public java.lang.Object get(java.lang.Object key1, java.lang.Object key2, java.lang.Object key3, java.lang.Object key4)
key1
- the first keykey2
- the second keykey3
- the third keykey4
- the fourth key
public boolean containsKey(java.lang.Object key1, java.lang.Object key2, java.lang.Object key3, java.lang.Object key4)
key1
- the first keykey2
- the second keykey3
- the third keykey4
- the fourth key
public java.lang.Object put(java.lang.Object key1, java.lang.Object key2, java.lang.Object key3, java.lang.Object key4, java.lang.Object value)
key1
- the first keykey2
- the second keykey3
- the third keykey4
- the fourth keyvalue
- the value to store
public java.lang.Object remove(java.lang.Object key1, java.lang.Object key2, java.lang.Object key3, java.lang.Object key4)
key1
- the first keykey2
- the second keykey3
- the third keykey4
- the fourth key
protected int hash(java.lang.Object key1, java.lang.Object key2, java.lang.Object key3, java.lang.Object key4)
key1
- the first keykey2
- the second keykey3
- the third keykey4
- the fourth key
protected boolean isEqualKey(AbstractHashedMap.HashEntry entry, java.lang.Object key1, java.lang.Object key2, java.lang.Object key3, java.lang.Object key4)
entry
- the entry to compare tokey1
- the first keykey2
- the second keykey3
- the third keykey4
- the fourth key
public java.lang.Object get(java.lang.Object key1, java.lang.Object key2, java.lang.Object key3, java.lang.Object key4, java.lang.Object key5)
key1
- the first keykey2
- the second keykey3
- the third keykey4
- the fourth keykey5
- the fifth key
public boolean containsKey(java.lang.Object key1, java.lang.Object key2, java.lang.Object key3, java.lang.Object key4, java.lang.Object key5)
key1
- the first keykey2
- the second keykey3
- the third keykey4
- the fourth keykey5
- the fifth key
public java.lang.Object put(java.lang.Object key1, java.lang.Object key2, java.lang.Object key3, java.lang.Object key4, java.lang.Object key5, java.lang.Object value)
key1
- the first keykey2
- the second keykey3
- the third keykey4
- the fourth keykey5
- the fifth keyvalue
- the value to store
public java.lang.Object remove(java.lang.Object key1, java.lang.Object key2, java.lang.Object key3, java.lang.Object key4, java.lang.Object key5)
key1
- the first keykey2
- the second keykey3
- the third keykey4
- the fourth keykey5
- the fifth key
protected int hash(java.lang.Object key1, java.lang.Object key2, java.lang.Object key3, java.lang.Object key4, java.lang.Object key5)
key1
- the first keykey2
- the second keykey3
- the third keykey4
- the fourth keykey5
- the fifth key
protected boolean isEqualKey(AbstractHashedMap.HashEntry entry, java.lang.Object key1, java.lang.Object key2, java.lang.Object key3, java.lang.Object key4, java.lang.Object key5)
entry
- the entry to compare tokey1
- the first keykey2
- the second keykey3
- the third keykey4
- the fourth keykey5
- the fifth key
public boolean removeAll(java.lang.Object key1)
This method removes all the mappings where the MultiKey
has one or more keys, and the first matches that specified.
key1
- the first key
public boolean removeAll(java.lang.Object key1, java.lang.Object key2)
This method removes all the mappings where the MultiKey
has two or more keys, and the first two match those specified.
key1
- the first keykey2
- the second key
public boolean removeAll(java.lang.Object key1, java.lang.Object key2, java.lang.Object key3)
This method removes all the mappings where the 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(java.lang.Object key1, java.lang.Object key2, java.lang.Object key3, java.lang.Object key4)
This method removes all the mappings where the 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(java.lang.Object key)
key
- the key to checkpublic java.lang.Object clone()
clone
in class java.lang.Object
public java.lang.Object put(java.lang.Object key, java.lang.Object value)
put
in interface java.util.Map
key
- the non-null MultiKey objectvalue
- the value to store
java.lang.NullPointerException
- if the key is null
java.lang.ClassCastException
- if the key is not a MultiKeypublic void putAll(java.util.Map mapToCopy)
putAll
in interface java.util.Map
mapToCopy
- to this map
java.lang.NullPointerException
- if the mapToCopy or any key within is null
java.lang.ClassCastException
- if any key in mapToCopy 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
public int size()
size
in interface java.util.Map
public boolean isEmpty()
isEmpty
in interface java.util.Map
public boolean containsKey(java.lang.Object key)
containsKey
in interface java.util.Map
public boolean containsValue(java.lang.Object value)
containsValue
in interface java.util.Map
public java.lang.Object get(java.lang.Object key)
get
in interface java.util.Map
public java.lang.Object remove(java.lang.Object key)
remove
in interface java.util.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 obj)
equals
in interface java.util.Map
equals
in class java.lang.Object
public int hashCode()
hashCode
in interface java.util.Map
hashCode
in class java.lang.Object
public java.lang.String toString()
toString
in class java.lang.Object
|
||||||||||
PREV CLASS NEXT CLASS | FRAMES NO FRAMES | |||||||||
SUMMARY: NESTED | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD |