001/**
002 *
003 * Licensed to the Apache Software Foundation (ASF) under one
004 * or more contributor license agreements.  See the NOTICE file
005 * distributed with this work for additional information
006 * regarding copyright ownership.  The ASF licenses this file
007 * to you under the Apache License, Version 2.0 (the
008 * "License"); you may not use this file except in compliance
009 * with the License.  You may obtain a copy of the License at
010 *
011 *     http://www.apache.org/licenses/LICENSE-2.0
012 *
013 * Unless required by applicable law or agreed to in writing, software
014 * distributed under the License is distributed on an "AS IS" BASIS,
015 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
016 * See the License for the specific language governing permissions and
017 * limitations under the License.
018 */
019
020package org.apache.hadoop.hbase.util;
021
022import java.io.Serializable;
023
024import org.apache.yetus.audience.InterfaceAudience;
025
026/**
027 * A generic class for pairs.
028 * @param <T1>
029 * @param <T2>
030 */
031@InterfaceAudience.Public
032public class Pair<T1, T2> implements Serializable
033{
034  private static final long serialVersionUID = -3986244606585552569L;
035  protected T1 first = null;
036  protected T2 second = null;
037
038  /**
039   * Default constructor.
040   */
041  public Pair()
042  {
043  }
044
045  /**
046   * Constructor
047   * @param a operand
048   * @param b operand
049   */
050  public Pair(T1 a, T2 b)
051  {
052    this.first = a;
053    this.second = b;
054  }
055  
056  /**
057   * Constructs a new pair, inferring the type via the passed arguments
058   * @param <T1> type for first
059   * @param <T2> type for second
060   * @param a first element
061   * @param b second element
062   * @return a new pair containing the passed arguments
063   */
064  public static <T1,T2> Pair<T1,T2> newPair(T1 a, T2 b) {
065    return new Pair<>(a, b);
066  }
067  
068  /**
069   * Replace the first element of the pair.
070   * @param a operand
071   */
072  public void setFirst(T1 a)
073  {
074    this.first = a;
075  }
076
077  /**
078   * Replace the second element of the pair.
079   * @param b operand
080   */
081  public void setSecond(T2 b)
082  {
083    this.second = b;
084  }
085
086  /**
087   * Return the first element stored in the pair.
088   * @return T1
089   */
090  public T1 getFirst()
091  {
092    return first;
093  }
094
095  /**
096   * Return the second element stored in the pair.
097   * @return T2
098   */
099  public T2 getSecond()
100  {
101    return second;
102  }
103
104  private static boolean equals(Object x, Object y)
105  {
106    return (x == null && y == null) || (x != null && x.equals(y));
107  }
108
109  @Override
110  @SuppressWarnings("unchecked")
111  public boolean equals(Object other)
112  {
113    return other instanceof Pair && equals(first, ((Pair)other).first) &&
114      equals(second, ((Pair)other).second);
115  }
116
117  @Override
118  public int hashCode()
119  {
120    if (first == null)
121      return (second == null) ? 0 : second.hashCode() + 1;
122    else if (second == null)
123      return first.hashCode() + 2;
124    else
125      return first.hashCode() * 17 + second.hashCode();
126  }
127
128  @Override
129  public String toString()
130  {
131    return "{" + getFirst() + "," + getSecond() + "}";
132  }
133}