View Javadoc

1   /**
2    *
3    * Licensed to the Apache Software Foundation (ASF) under one
4    * or more contributor license agreements.  See the NOTICE file
5    * distributed with this work for additional information
6    * regarding copyright ownership.  The ASF licenses this file
7    * to you under the Apache License, Version 2.0 (the
8    * "License"); you may not use this file except in compliance
9    * with the License.  You may obtain a copy of the License at
10   *
11   *     http://www.apache.org/licenses/LICENSE-2.0
12   *
13   * Unless required by applicable law or agreed to in writing, software
14   * distributed under the License is distributed on an "AS IS" BASIS,
15   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16   * See the License for the specific language governing permissions and
17   * limitations under the License.
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.classification.InterfaceAudience;
26  import org.apache.hadoop.classification.InterfaceStability;
27  
28  /**
29   * A generic, immutable class for pairs of objects both of type <code>T</code>.
30   * @param <T>
31   * @see Pair if Types differ.
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     * Constructor
41     * @param a operand
42     * @param b operand
43     */
44    public PairOfSameType(T a, T b) {
45      this.first = a;
46      this.second = b;
47    }
48  
49    /**
50     * Return the first element stored in the pair.
51     * @return T
52     */
53    public T getFirst() {
54      return first;
55    }
56  
57    /**
58     * Return the second element stored in the pair.
59     * @return T
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 }