public class MultiKey extends Object implements Comparable<MultiKey>, Serializable
Map collections. If an Order object is
associated with a tuple
(String customer, Date timestamp, OrderId id), Java cannot
handle multi-object keys but only a single object key. The
solutions to this mismatch is:
Map objects:
Map<String, Map<Date, Map<OrderId, Order>>>.
This solution is both cumbersome to implement, uses much
memory and is not recommended.
MultiKey's hash code is
insufficient.
MultiMap stores multiple key values in a general
class obviating the need to write a proprietary key class.
The hash code algorithm is:
Object[] keys;
int i;
int hashCode;
for (i = 0, hashCode = 0; i < keys.length; ++i)
{
if (keys[i] != null)
{
hashCode ^= keys[i].hashCode();
}
}
Unlike a proprietary key class, MultiKey cannot
enforce key value number and type requirements. It is possible
to use keys of differing lengths and type within the same map.
If MultiKey's size and type ordering enforcement is
required, use the subclasses MultiKey2,
MultiKey3 or MultiKey4.
It is recommended that only immutable objects be used in the
key but if mutable objects are used and are modified
after creating the MultiKey, the
MultiKey hash code is unaffected because the hash code
is calculated once in the constructor. This results in a
discrepency between the key's values and hash code.
| Constructor and Description |
|---|
MultiKey(Object... keys)
Creates a multiple key container for the given objects.
|
| Modifier and Type | Method and Description |
|---|---|
int |
compareTo(MultiKey key)
Returns a negative integer, zero or a positive integer if
this object is less than, equal to or greater than the
argument, respectively.
|
boolean |
equals(Object o)
Returns
true if key is a non-null
MultiKey instance with the same number of key values and
the key values are equal and in the same order. |
int |
hashCode()
Returns a hash code value for this multi-valued key.
|
Object |
key(int index)
Returns the key value at the specified index.
|
Object[] |
keys()
Returns a copy of the key array.
|
int |
size()
Returns the number of key values.
|
String |
toString()
Returns a textual representation of this multi-valued
key.
|
public MultiKey(Object... keys) throws IndexOutOfBoundsException
keys - One or more key objects. May contain
null references.IndexOutOfBoundsException - if no keys are specified.public int compareTo(MultiKey key) throws ClassCastException
The comparison is done as follows:
this key's size does not equal
key's size, then returns
this.size() - key.size().
compareTo in interface Comparable<MultiKey>key - compare against this multi-valued key.ClassCastException - if the keys contain incompatible or incomparable types.public int size()
public Object key(int index) throws IndexOutOfBoundsException
If the returned object is mutable, modifying it will result in unspecified behavior by the map.
index - Index to desired key value.IndexOutOfBoundsException - if index is either < zero or >
key size.public Object[] keys()
If any of the returned key objects is mutable, modifying a key will result in unspecified behavior by the map.
public boolean equals(Object o)
true if key is a non-null
MultiKey instance with the same number of key values and
the key values are equal and in the same order. Returns
false otherwise.public int hashCode()
The hash code is calculated once in the constructor and
cached. If any key objects is modified after construction,
its new hash code will not be reflected in this
MultiKey object's hash code.
The hash code calculation is:
Object[] keys;
int i;
int hashCode;
for (i = 0, hashCode = 0; i < keys.length; ++i)
{
if (keys[i] != null)
{
hashCode ^= keys[i].hashCode();
}
}
Copyright © 2020. All rights reserved.