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 * @param <T1> 026 * @param <T2> 027 */ 028@InterfaceAudience.Public 029public class Pair<T1, T2> implements Serializable { 030 private static final long serialVersionUID = -3986244606585552569L; 031 protected T1 first = null; 032 protected T2 second = null; 033 034 /** 035 * Default constructor. 036 */ 037 public Pair() { 038 } 039 040 /** 041 * Constructor 042 * @param a operand 043 * @param b operand 044 */ 045 public Pair(T1 a, T2 b) { 046 this.first = a; 047 this.second = b; 048 } 049 050 /** 051 * Constructs a new pair, inferring the type via the passed arguments 052 * @param <T1> type for first 053 * @param <T2> type for second 054 * @param a first element 055 * @param b second element 056 * @return a new pair containing the passed arguments 057 */ 058 public static <T1, T2> Pair<T1, T2> newPair(T1 a, T2 b) { 059 return new Pair<>(a, b); 060 } 061 062 /** 063 * Replace the first element of the pair. 064 * @param a operand 065 */ 066 public void setFirst(T1 a) { 067 this.first = a; 068 } 069 070 /** 071 * Replace the second element of the pair. 072 * @param b operand 073 */ 074 public void setSecond(T2 b) { 075 this.second = b; 076 } 077 078 /** 079 * Return the first element stored in the pair. 080 */ 081 public T1 getFirst() { 082 return first; 083 } 084 085 /** 086 * Return the second element stored in the pair. 087 */ 088 public T2 getSecond() { 089 return second; 090 } 091 092 private static boolean equals(Object x, Object y) { 093 return (x == null && y == null) || (x != null && x.equals(y)); 094 } 095 096 @Override 097 @SuppressWarnings("unchecked") 098 public boolean equals(Object other) { 099 return other instanceof Pair && equals(first, ((Pair) other).first) 100 && equals(second, ((Pair) other).second); 101 } 102 103 @Override 104 public int hashCode() { 105 if (first == null) return (second == null) ? 0 : second.hashCode() + 1; 106 else if (second == null) return first.hashCode() + 2; 107 else return first.hashCode() * 17 + second.hashCode(); 108 } 109 110 @Override 111 public String toString() { 112 return "{" + getFirst() + "," + getSecond() + "}"; 113 } 114}