public class MultiKey extends java.lang.Object implements java.lang.Comparable<MultiKey>
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 Arrays.hashCode(Object[])
.
Note: that Arrays.hashCode(Object[])
allows Object[]
to contain null
references.
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
discrepancy between the key's values and hash code.
Constructor and Description |
---|
MultiKey(java.lang.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(java.lang.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.
|
java.lang.Object |
key(int index)
Returns the key value at the specified index.
|
java.lang.Object[] |
keys()
Returns a copy of the key array.
|
int |
size()
Returns the number of key values.
|
java.lang.String |
toString()
Returns a textual representation of this multi-valued
key.
|
public MultiKey(java.lang.Object... keys)
keys
- One or more key objects. May contain
null
references.java.lang.IndexOutOfBoundsException
- if no keys are specified.public int compareTo(MultiKey key)
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 java.lang.Comparable<MultiKey>
key
- compare against this multi-valued key.java.lang.ClassCastException
- if the keys contain incompatible or incomparable types.public boolean equals(java.lang.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.equals
in class java.lang.Object
o
- Test equality with this object.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;
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();
}
}
hashCode
in class java.lang.Object
public java.lang.String toString()
toString
in class java.lang.Object
public int size()
public java.lang.Object key(int index)
If the returned object is mutable, modifying it will result in unspecified behavior by the map.
index
- Index to desired key value.java.lang.IndexOutOfBoundsException
- if index
is either < zero or >
key size.public java.lang.Object[] keys()
If any of the returned key objects is mutable, modifying a key will result in unspecified behavior by the map.
Copyright © 2001 - 2024. Charles W. Rapp. All rights reserved.