| 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 | |
| 17 | package android.util; |
| 18 | |
| 19 | import android.annotation.Nullable; |
| 20 | |
| 21 | import java.util.Objects; |
| 22 | |
| 23 | /** |
| 24 | * Container to ease passing around a tuple of two objects. This object provides a sensible |
| 25 | * implementation of equals(), returning true if equals() is true on each of the contained |
| 26 | * objects. |
| 27 | */ |
| 28 | public class Pair<F, S> { |
| 29 | public final F first; |
| 30 | public final S second; |
| 31 | |
| 32 | /** |
| 33 | * Constructor for a Pair. |
| 34 | * |
| 35 | * @param first the first object in the Pair |
| 36 | * @param second the second object in the pair |
| 37 | */ |
| 38 | public Pair(F first, S second) { |
| 39 | this.first = first; |
| 40 | this.second = second; |
| 41 | } |
| 42 | |
| 43 | /** |
| 44 | * Checks the two objects for equality by delegating to their respective |
| 45 | * {@link Object#equals(Object)} methods. |
| 46 | * |
| 47 | * @param o the {@link Pair} to which this one is to be checked for equality |
| 48 | * @return true if the underlying objects of the Pair are both considered |
| 49 | * equal |
| 50 | */ |
| 51 | @Override |
| 52 | public boolean equals(@Nullable Object o) { |
| 53 | if (!(o instanceof Pair)) { |
| 54 | return false; |
| 55 | } |
| 56 | Pair<?, ?> p = (Pair<?, ?>) o; |
| 57 | return Objects.equals(p.first, first) && Objects.equals(p.second, second); |
| 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 | @Override |
| 71 | public String toString() { |
| 72 | return "Pair{" + String.valueOf(first) + " " + String.valueOf(second) + "}"; |
| 73 | } |
| 74 | |
| 75 | /** |
| 76 | * Convenience method for creating an appropriately typed pair. |
| 77 | * @param a the first object in the Pair |
| 78 | * @param b the second object in the pair |
| 79 | * @return a Pair that is templatized with the types of a and b |
| 80 | */ |
| 81 | public static <A, B> Pair <A, B> create(A a, B b) { |
| 82 | return new Pair<A, B>(a, b); |
| 83 | } |
| 84 | } |