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