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