Stuart McCulloch | bb01437 | 2012-06-07 21:57:32 +0000 | [diff] [blame] | 1 | package aQute.libg.tuple; |
| 2 | |
Stuart McCulloch | d482610 | 2012-06-26 16:34:24 +0000 | [diff] [blame] | 3 | import java.io.Serializable; |
Stuart McCulloch | 2286f23 | 2012-06-15 13:27:53 +0000 | [diff] [blame] | 4 | |
Stuart McCulloch | d482610 | 2012-06-26 16:34:24 +0000 | [diff] [blame] | 5 | public class Pair<A, B> implements Serializable { |
| 6 | |
| 7 | private static final long serialVersionUID = 1L; |
| 8 | |
| 9 | private final A first; |
| 10 | private final B second; |
| 11 | |
| 12 | public Pair(A first, B second) { |
| 13 | assert first != null && second != null : "both parameters must be non-null"; |
| 14 | this.first = first; |
| 15 | this.second = second; |
Stuart McCulloch | bb01437 | 2012-06-07 21:57:32 +0000 | [diff] [blame] | 16 | } |
Stuart McCulloch | d482610 | 2012-06-26 16:34:24 +0000 | [diff] [blame] | 17 | |
| 18 | public static <A, B> Pair<A,B> newInstance(A first, B second) { |
| 19 | return new Pair<A,B>(first, second); |
| 20 | } |
| 21 | |
| 22 | public A getFirst() { |
| 23 | return first; |
| 24 | } |
| 25 | |
| 26 | public B getSecond() { |
| 27 | return second; |
| 28 | } |
| 29 | |
| 30 | @Override |
| 31 | public String toString() { |
| 32 | return "Pair [" + first + ", " + second + "]"; |
| 33 | } |
| 34 | |
| 35 | @Override |
| 36 | public int hashCode() { |
| 37 | final int prime = 31; |
| 38 | int result = 1; |
| 39 | result = prime * result + ((first == null) ? 0 : first.hashCode()); |
| 40 | result = prime * result + ((second == null) ? 0 : second.hashCode()); |
| 41 | return result; |
| 42 | } |
| 43 | |
| 44 | @Override |
| 45 | public boolean equals(Object obj) { |
| 46 | if (this == obj) |
| 47 | return true; |
| 48 | if (obj == null) |
| 49 | return false; |
| 50 | if (getClass() != obj.getClass()) |
| 51 | return false; |
| 52 | @SuppressWarnings("unchecked") |
| 53 | Pair<A,B> other = (Pair<A,B>) obj; |
| 54 | if (first == null) { |
| 55 | if (other.first != null) |
| 56 | return false; |
| 57 | } else if (!first.equals(other.first)) |
| 58 | return false; |
| 59 | if (second == null) { |
| 60 | if (other.second != null) |
| 61 | return false; |
| 62 | } else if (!second.equals(other.second)) |
| 63 | return false; |
| 64 | return true; |
| 65 | } |
| 66 | |
| 67 | @Override |
| 68 | public Pair<A,B> clone() { |
| 69 | return new Pair<A,B>(first, second); |
| 70 | } |
| 71 | } |