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