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