Class MultiKey
- java.lang.Object
-
- net.sf.eBus.util.MultiKey
-
- All Implemented Interfaces:
Serializable,Comparable<MultiKey>
public class MultiKey extends Object implements Comparable<MultiKey>, Serializable
Allows multi-valued keys to be used in Java's single keyMapcollections. If anOrderobject 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:-
Use nested
Mapobjects:Map<String, Map<Date, Map<OrderId, Order>>>.This solution is both cumbersome to implement, uses much memory and is not recommended. -
Write a class to store the tuple objects and provides
the appropriate hash code for the key values. This is
the only solution if
MultiKey's hash code is insufficient.
MultiMapstores multiple key values in a general class obviating the need to write a proprietary key class. The hash code algorithm isArrays.hashCode(Object[]). Note: thatArrays.hashCode(Object[])allowsObject[]to containnullreferences.Unlike a proprietary key class,
MultiKeycannot enforce key value number and type requirements. It is possible to use keys of differing lengths and type within the same map. IfMultiKey's size and type ordering enforcement is required, use the subclassesMultiKey2,MultiKey3orMultiKey4.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, theMultiKeyhash 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.- Since:
- Commons Collections 3.0
- Version:
- $Revision: 1.1 $ $Date: 2005/10/28 14:37:52 $
- Author:
- Howard Lewis Ship, Stephen Colebourne, Charles W. Rapp
- See Also:
- Serialized Form
-
-
Method Summary
All Methods Instance Methods Concrete Methods Modifier and Type Method Description intcompareTo(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.booleanequals(Object o)Returnstrueifkeyis a non-null MultiKey instance with the same number of key values and the key values are equal and in the same order.inthashCode()Returns a hash code value for this multi-valued key.Objectkey(int index)Returns the key value at the specified index.Object[]keys()Returns a copy of the key array.intsize()Returns the number of key values.StringtoString()Returns a textual representation of this multi-valued key.
-
-
-
Constructor Detail
-
MultiKey
public MultiKey(Object... keys)
Creates a multiple key container for the given objects.- Parameters:
keys- One or more key objects. May containnullreferences.- Throws:
IndexOutOfBoundsException- if no keys are specified.
-
-
Method Detail
-
compareTo
public 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.The comparison is done as follows:
-
If
thiskey's size does not equalkey's size, then returnsthis.size() - key.size(). - Otherwise iterates over the keys, comparing each key in turn until a non-zero value is found or all the keys are compared.
- Specified by:
compareToin interfaceComparable<MultiKey>- Parameters:
key- compare against this multi-valued key.- Returns:
- a negative integer, zero or a positive integer if this object is less than, equal to or greater than the argument, respectively.
- Throws:
ClassCastException- if the keys contain incompatible or incomparable types.
-
If
-
equals
public boolean equals(Object o)
Returnstrueifkeyis a non-null MultiKey instance with the same number of key values and the key values are equal and in the same order. Returnsfalseotherwise.
-
hashCode
public int hashCode()
Returns a hash code value for this multi-valued key.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
MultiKeyobject'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(); } }
-
toString
public String toString()
Returns a textual representation of this multi-valued key.
-
size
public int size()
Returns the number of key values.- Returns:
- the number of key values.
-
key
public Object key(int index)
Returns the key value at the specified index.If the returned object is mutable, modifying it will result in unspecified behavior by the map.
- Parameters:
index- Index to desired key value.- Returns:
- the key value at the specified index.
- Throws:
IndexOutOfBoundsException- ifindexis either < zero or > key size.
-
keys
public Object[] keys()
Returns a copy of the key array.If any of the returned key objects is mutable, modifying a key will result in unspecified behavior by the map.
- Returns:
- the key array copy.
-
-