1/* 2 * Copyright (C) 2009 The Android Open Source Project 3 * 4 * Licensed under the Apache License, Version 2.0 (the "License"); 5 * you may not use this file except in compliance with the License. 6 * You may obtain a copy of the License at 7 * 8 * http://www.apache.org/licenses/LICENSE-2.0 9 * 10 * Unless required by applicable law or agreed to in writing, software 11 * distributed under the License is distributed on an "AS IS" BASIS, 12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 * See the License for the specific language governing permissions and 14 * limitations under the License. 15 */ 16 17package android.support.v4.util; 18 19/** 20 * Container to ease passing around a tuple of two objects. This object provides a sensible 21 * implementation of equals(), returning true if equals() is true on each of the contained 22 * objects. 23 */ 24public class Pair<F, S> { 25 public final F first; 26 public final S second; 27 28 /** 29 * Constructor for a Pair. 30 * 31 * @param first the first object in the Pair 32 * @param second the second object in the pair 33 */ 34 public Pair(F first, S second) { 35 this.first = first; 36 this.second = second; 37 } 38 39 /** 40 * Checks the two objects for equality by delegating to their respective 41 * {@link Object#equals(Object)} methods. 42 * 43 * @param o the {@link Pair} to which this one is to be checked for equality 44 * @return true if the underlying objects of the Pair are both considered 45 * equal 46 */ 47 @Override 48 public boolean equals(Object o) { 49 if (!(o instanceof Pair)) { 50 return false; 51 } 52 Pair<?, ?> p = (Pair<?, ?>) o; 53 return objectsEqual(p.first, first) && objectsEqual(p.second, second); 54 } 55 56 private static boolean objectsEqual(Object a, Object b) { 57 return a == b || (a != null && a.equals(b)); 58 } 59 60 /** 61 * Compute a hash code using the hash codes of the underlying objects 62 * 63 * @return a hashcode of the Pair 64 */ 65 @Override 66 public int hashCode() { 67 return (first == null ? 0 : first.hashCode()) ^ (second == null ? 0 : second.hashCode()); 68 } 69 70 /** 71 * Convenience method for creating an appropriately typed pair. 72 * @param a the first object in the Pair 73 * @param b the second object in the pair 74 * @return a Pair that is templatized with the types of a and b 75 */ 76 public static <A, B> Pair <A, B> create(A a, B b) { 77 return new Pair<A, B>(a, b); 78 } 79} 80