public class MultiKey<K>
extends java.lang.Object
implements java.io.Serializable
MultiKey
allows multiple map keys to be merged together.
The purpose of this class is to avoid the need to write code to handle maps of maps. An example might be the need to look up a file name by key and locale. The typical solution might be nested maps. This class can be used instead by creating an instance passing in the key and locale.
Example usage:
// populate map with data mapping key+locale to localizedText Map map = new HashMap(); MultiKey multiKey = new MultiKey(key, locale); map.put(multiKey, localizedText); // later retrieve the localized text MultiKey multiKey = new MultiKey(key, locale); String localizedText = (String) map.get(multiKey);
Constructor and Description |
---|
MultiKey(K[] keys)
Constructor taking an array of keys which is cloned.
|
MultiKey(K[] keys,
boolean makeClone)
Constructor taking an array of keys, optionally choosing whether to clone.
|
MultiKey(K key1,
K key2)
Constructor taking two keys.
|
MultiKey(K key1,
K key2,
K key3)
Constructor taking three keys.
|
MultiKey(K key1,
K key2,
K key3,
K key4)
Constructor taking four keys.
|
MultiKey(K key1,
K key2,
K key3,
K key4,
K key5)
Constructor taking five keys.
|
Modifier and Type | Method and Description |
---|---|
boolean |
equals(java.lang.Object other)
Compares this object to another.
|
K |
getKey(int index)
Gets the key at the specified index.
|
K[] |
getKeys()
Gets a clone of the array of keys.
|
int |
hashCode()
Gets the combined hash code that is computed from all the keys.
|
int |
size()
Gets the size of the list of keys.
|
java.lang.String |
toString()
Gets a debugging string version of the key.
|
public MultiKey(K key1, K key2)
The keys should be immutable If they are not then they must not be changed after adding to the MultiKey.
key1
- the first keykey2
- the second keypublic MultiKey(K key1, K key2, K key3)
The keys should be immutable If they are not then they must not be changed after adding to the MultiKey.
key1
- the first keykey2
- the second keykey3
- the third keypublic MultiKey(K key1, K key2, K key3, K key4)
The keys should be immutable If they are not then they must not be changed after adding to the MultiKey.
key1
- the first keykey2
- the second keykey3
- the third keykey4
- the fourth keypublic MultiKey(K key1, K key2, K key3, K key4, K key5)
The keys should be immutable If they are not then they must not be changed after adding to the MultiKey.
key1
- the first keykey2
- the second keykey3
- the third keykey4
- the fourth keykey5
- the fifth keypublic MultiKey(K[] keys)
The keys should be immutable If they are not then they must not be changed after adding to the MultiKey.
This is equivalent to new MultiKey(keys, true)
.
keys
- the array of keys, not nulljava.lang.IllegalArgumentException
- if the key array is nullpublic MultiKey(K[] keys, boolean makeClone)
If the array is not cloned, then it must not be modified.
This method is public for performance reasons only, to avoid a clone. The hashcode is calculated once here in this method. Therefore, changing the array passed in would not change the hashcode but would change the equals method, which is a bug.
This is the only fully safe usage of this constructor, as the object array is never made available in a variable:
new MultiKey(new Object[] {...}, false);
The keys should be immutable If they are not then they must not be changed after adding to the MultiKey.
keys
- the array of keys, not nullmakeClone
- true to clone the array, false to assign itjava.lang.IllegalArgumentException
- if the key array is nullpublic K[] getKeys()
The keys should be immutable If they are not then they must not be changed.
public K getKey(int index)
The key should be immutable. If it is not then it must not be changed.
index
- the index to retrievejava.lang.IndexOutOfBoundsException
- if the index is invalidpublic int size()
public boolean equals(java.lang.Object other)
To be equal, the other object must be a MultiKey
with the
same number of keys which are also equal.
equals
in class java.lang.Object
other
- the other object to compare topublic int hashCode()
This value is computed once and then cached, so elements should not
change their hash codes once created (note that this is the same
constraint that would be used if the individual keys elements were
themselves Map
keys.
hashCode
in class java.lang.Object
public java.lang.String toString()
toString
in class java.lang.Object
Copyright © 2010 - 2020 Adobe. All Rights Reserved