01: package org.gui4j.util;
02:
03: import java.io.Serializable;
04:
05: /**
06: * A Pair is a container for two objects, the "first" and the "second" one.
07: * Instances of <code>Pair</code> are immutable.
08: */
09: public class Pair implements Serializable {
10: private final Object first;
11: private final Object second;
12:
13: /**
14: * Constructor for Pair.
15: * @param first must be different from null
16: * @param second must be different from null
17: */
18: public Pair(Object first, Object second) {
19: assert first != null;
20: assert second != null;
21: this .first = first;
22: this .second = second;
23: }
24:
25: /**
26: * Returns the first.
27: * @return Object
28: */
29: public Object getFirst() {
30: return first;
31: }
32:
33: /**
34: * Returns the second.
35: * @return Object
36: */
37: public Object getSecond() {
38: return second;
39: }
40:
41: /**
42: * @see java.lang.Object#equals(Object)
43: */
44: public boolean equals(Object obj) {
45: if (obj == this ) {
46: return true;
47: }
48: if (!(obj instanceof Pair)) {
49: return false;
50: }
51: Pair pair = (Pair) obj;
52: return first.equals(pair.first) && second.equals(pair.second);
53: }
54:
55: /*
56: * @see java.lang.Object#hashCode()
57: * KKB, 17.3.04: hashCode calculated after "Effective Java", Item 8
58: */
59: public int hashCode() {
60: int result = 17;
61: result = 37 * result + first.hashCode();
62: result = 37 * result + second.hashCode();
63:
64: return result;
65: }
66:
67: /**
68: * @see java.lang.Object#toString()
69: */
70: public String toString() {
71: return "(" + first + ", " + second + ")";
72: }
73:
74: }
|