1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16 package net.sf.composite.util;
17
18 /***
19 * A pair of objects. This object is used for creating a key value in a Maps
20 * used for caching purposes. Note that the order of the two objects in the pair
21 * <em>is</em> important. Also note that object1 and object2 may not be null.
22 *
23 * @author Matt Sgarlata
24 * @since Mar 14, 2005
25 */
26 public class ObjectPair {
27
28 private Object object1;
29 private Object object2;
30
31 public ObjectPair() { }
32
33 public ObjectPair(Object object1, Object object2) {
34 this.object1 = object1;
35 this.object2 = object2;
36 }
37
38 public Object getObject1() {
39 return object1;
40 }
41 public void setObject1(Object object1) {
42 this.object1 = object1;
43 }
44 public Object getObject2() {
45 return object2;
46 }
47 public void setObject2(Object object2) {
48 this.object2 = object2;
49 }
50
51 public boolean equals(Object object) {
52 if (object == this) {
53 return true;
54 }
55 if (!(object instanceof ObjectPair)) {
56 return false;
57 }
58 ObjectPair pair = (ObjectPair) object;
59 return nullSafeEquals(object1, pair.object1)
60 && nullSafeEquals(object2, pair.object2);
61 }
62
63 private boolean nullSafeEquals(Object o1, Object o2) {
64 return o1 == o2 || o1 != null && o1.equals(o2);
65 }
66
67 public int hashCode() {
68 return (object1 == null ? 0 : object1.hashCode())
69 + (object2 == null ? 0 : object2.hashCode());
70 }
71
72 public String toString() {
73 return "(" + getObject1() + "," + getObject2() + ")";
74 }
75
76 }