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.util.Iterator; 021import org.apache.commons.lang3.NotImplementedException; 022import org.apache.hadoop.hbase.HConstants; 023import org.apache.yetus.audience.InterfaceAudience; 024 025/** 026 * A generic, immutable class for pairs of objects both of type <code>T</code>. 027 * @see Pair if Types differ. 028 */ 029@InterfaceAudience.Public 030public class PairOfSameType<T> implements Iterable<T> { 031 private final T first; 032 private final T second; 033 034 /** 035 * Constructor 036 * @param a operand 037 * @param b operand 038 */ 039 public PairOfSameType(T a, T b) { 040 this.first = a; 041 this.second = b; 042 } 043 044 /** 045 * Return the first element stored in the pair. n 046 */ 047 public T getFirst() { 048 return first; 049 } 050 051 /** 052 * Return the second element stored in the pair. n 053 */ 054 public T getSecond() { 055 return second; 056 } 057 058 private static boolean equals(Object x, Object y) { 059 return (x == null && y == null) || (x != null && x.equals(y)); 060 } 061 062 @Override 063 @SuppressWarnings("unchecked") 064 public boolean equals(Object other) { 065 return other instanceof PairOfSameType && equals(first, ((PairOfSameType) other).first) 066 && equals(second, ((PairOfSameType) other).second); 067 } 068 069 @Override 070 public int hashCode() { 071 if (first == null) return (second == null) ? 0 : second.hashCode() + 1; 072 else if (second == null) return first.hashCode() + 2; 073 else return first.hashCode() * 17 + second.hashCode(); 074 } 075 076 @Override 077 public String toString() { 078 return "{" + getFirst() + "," + getSecond() + "}"; 079 } 080 081 @Override 082 public Iterator<T> iterator() { 083 return new Iterator<T>() { 084 private int returned = 0; 085 086 @Override 087 public boolean hasNext() { 088 return this.returned < 2; 089 } 090 091 @Override 092 public T next() { 093 if (++this.returned == 1) return getFirst(); 094 else if (this.returned == 2) return getSecond(); 095 else throw new IllegalAccessError("this.returned=" + this.returned); 096 } 097 098 @Override 099 public void remove() { 100 throw new NotImplementedException(HConstants.NOT_IMPLEMENTED); 101 } 102 }; 103 } 104}