1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21 package org.apache.hadoop.hbase.util;
22
23 import java.util.Iterator;
24
25 import org.apache.commons.lang.NotImplementedException;
26
27
28
29
30
31
32 public class PairOfSameType<T> implements Iterable<T> {
33 private final T first;
34 private final T second;
35
36
37
38
39
40
41 public PairOfSameType(T a, T b) {
42 this.first = a;
43 this.second = b;
44 }
45
46
47
48
49
50 public T getFirst() {
51 return first;
52 }
53
54
55
56
57
58 public T getSecond() {
59 return second;
60 }
61
62 private static boolean equals(Object x, Object y) {
63 return (x == null && y == null) || (x != null && x.equals(y));
64 }
65
66 @Override
67 @SuppressWarnings("unchecked")
68 public boolean equals(Object other) {
69 return other instanceof PairOfSameType &&
70 equals(first, ((PairOfSameType)other).first) &&
71 equals(second, ((PairOfSameType)other).second);
72 }
73
74 @Override
75 public int hashCode() {
76 if (first == null)
77 return (second == null) ? 0 : second.hashCode() + 1;
78 else if (second == null)
79 return first.hashCode() + 2;
80 else
81 return first.hashCode() * 17 + second.hashCode();
82 }
83
84 @Override
85 public String toString() {
86 return "{" + getFirst() + "," + getSecond() + "}";
87 }
88
89 @Override
90 public Iterator<T> iterator() {
91 return new Iterator<T>() {
92 private int returned = 0;
93
94 @Override
95 public boolean hasNext() {
96 return this.returned < 2;
97 }
98
99 @Override
100 public T next() {
101 if (++this.returned == 1) return getFirst();
102 else if (this.returned == 2) return getSecond();
103 else throw new IllegalAccessError("this.returned=" + this.returned);
104 }
105
106 @Override
107 public void remove() {
108 throw new NotImplementedException();
109 }
110 };
111 }
112 }