1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21 package org.onemind.commons.java.datastructure;
22
23 /***
24 * Represent a map key that consists of two value
25 * @author TiongHiang Lee (thlee@onemindsoft.org)
26 */
27 public class DuoMapKey
28 {
29
30 private final int _hashCode;
31
32 /*** key1 **/
33 private Object key1;
34
35 /*** key2 **/
36 private Object key2;
37
38 /***
39 * Constructor
40 * @param key1
41 * @param key2
42 */
43 public DuoMapKey(Object key1, Object key2)
44 {
45 _hashCode = ((key1 == null) ? 0 : key1.hashCode()) + ((key2 == null) ? 0 : key2.hashCode() >> 4);
46 }
47
48 /***
49 * {@inheritDoc}
50 */
51 public int hashCode()
52 {
53 return _hashCode;
54 }
55
56 /***
57 * {@inheritDoc}
58 */
59 public boolean equals(Object o)
60 {
61 if (o instanceof DuoMapKey)
62 {
63 DuoMapKey other = (DuoMapKey) o;
64 return _keyEquals(key1, other.key1) && _keyEquals(key2, other.key2);
65 } else
66 {
67 return false;
68 }
69 }
70
71 /***
72 * Return whether key and other is equals
73 * @param key the key
74 * @param other the other key
75 * @return true if both null or equals
76 */
77 private boolean _keyEquals(Object key, Object other)
78 {
79 if (key == null)
80 {
81 return other == null;
82 } else
83 {
84 return key.equals(other);
85 }
86 }
87 }