View Javadoc

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