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