1 | package agents.anac.y2011.IAMhaggler2011;
|
---|
2 | import java.io.Serializable;
|
---|
3 |
|
---|
4 | public class Pair<A, B> implements Serializable {
|
---|
5 |
|
---|
6 | private static final long serialVersionUID = -3160841691791187933L;
|
---|
7 | public final A fst;
|
---|
8 | public final B snd;
|
---|
9 |
|
---|
10 | /**
|
---|
11 | * @param fst
|
---|
12 | * First value.
|
---|
13 | * @param snd
|
---|
14 | * Second value.
|
---|
15 | */
|
---|
16 | public Pair(A fst, B snd) {
|
---|
17 | this.fst = fst;
|
---|
18 | this.snd = snd;
|
---|
19 | }
|
---|
20 |
|
---|
21 | /*
|
---|
22 | * (non-Javadoc)
|
---|
23 | *
|
---|
24 | * @see java.lang.Object#toString()
|
---|
25 | */
|
---|
26 | public String toString() {
|
---|
27 | return "Pair[" + fst + "," + snd + "]";
|
---|
28 | }
|
---|
29 |
|
---|
30 | /*
|
---|
31 | * (non-Javadoc)
|
---|
32 | *
|
---|
33 | * @see java.lang.Object#equals(java.lang.Object)
|
---|
34 | */
|
---|
35 | public boolean equals(Object other) {
|
---|
36 | return other instanceof Pair<?, ?> && equals(fst, ((Pair<?, ?>) other).fst) && equals(snd, ((Pair<?, ?>) other).snd);
|
---|
37 | }
|
---|
38 |
|
---|
39 | private static boolean equals(Object x, Object y) {
|
---|
40 | return (x == null && y == null) || (x != null && x.equals(y));
|
---|
41 | }
|
---|
42 |
|
---|
43 | /*
|
---|
44 | * (non-Javadoc)
|
---|
45 | *
|
---|
46 | * @see java.lang.Object#hashCode()
|
---|
47 | */
|
---|
48 | public int hashCode() {
|
---|
49 | if (fst == null)
|
---|
50 | return (snd == null) ? 0 : snd.hashCode() + 1;
|
---|
51 | else if (snd == null)
|
---|
52 | return fst.hashCode() + 2;
|
---|
53 | else
|
---|
54 | return fst.hashCode() * 17 + snd.hashCode();
|
---|
55 | }
|
---|
56 |
|
---|
57 | public static <A, B> Pair<A, B> of(A a, B b) {
|
---|
58 | return new Pair<A, B>(a, b);
|
---|
59 | }
|
---|
60 | } |
---|