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.io.Serializable;
23  
24  import org.apache.hadoop.classification.InterfaceAudience;
25  import org.apache.hadoop.classification.InterfaceStability;
26  
27  /**
28   * A generic class for pairs.
29   * @param <T1>
30   * @param <T2>
31   */
32  @InterfaceAudience.Public
33  @InterfaceStability.Stable
34  public class Pair<T1, T2> implements Serializable
35  {
36    private static final long serialVersionUID = -3986244606585552569L;
37    protected T1 first = null;
38    protected T2 second = null;
39  
40    /**
41     * Default constructor.
42     */
43    public Pair()
44    {
45    }
46  
47    /**
48     * Constructor
49     * @param a operand
50     * @param b operand
51     */
52    public Pair(T1 a, T2 b)
53    {
54      this.first = a;
55      this.second = b;
56    }
57    
58    /**
59     * Constructs a new pair, inferring the type via the passed arguments
60     * @param <T1> type for first
61     * @param <T2> type for second
62     * @param a first element
63     * @param b second element
64     * @return a new pair containing the passed arguments
65     */
66    public static <T1,T2> Pair<T1,T2> newPair(T1 a, T2 b) {
67      return new Pair<T1,T2>(a, b);
68    }
69    
70    /**
71     * Replace the first element of the pair.
72     * @param a operand
73     */
74    public void setFirst(T1 a)
75    {
76      this.first = a;
77    }
78  
79    /**
80     * Replace the second element of the pair.
81     * @param b operand
82     */
83    public void setSecond(T2 b)
84    {
85      this.second = b;
86    }
87  
88    /**
89     * Return the first element stored in the pair.
90     * @return T1
91     */
92    public T1 getFirst()
93    {
94      return first;
95    }
96  
97    /**
98     * Return the second element stored in the pair.
99     * @return T2
100    */
101   public T2 getSecond()
102   {
103     return second;
104   }
105 
106   private static boolean equals(Object x, Object y)
107   {
108     return (x == null && y == null) || (x != null && x.equals(y));
109   }
110 
111   @Override
112   @SuppressWarnings("unchecked")
113   public boolean equals(Object other)
114   {
115     return other instanceof Pair && equals(first, ((Pair)other).first) &&
116       equals(second, ((Pair)other).second);
117   }
118 
119   @Override
120   public int hashCode()
121   {
122     if (first == null)
123       return (second == null) ? 0 : second.hashCode() + 1;
124     else if (second == null)
125       return first.hashCode() + 2;
126     else
127       return first.hashCode() * 17 + second.hashCode();
128   }
129 
130   @Override
131   public String toString()
132   {
133     return "{" + getFirst() + "," + getSecond() + "}";
134   }
135 }