Fred Quintana | d4a1d2e | 2009-07-16 16:36:38 -0700 | [diff] [blame] | 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 | |
Jeff Sharkey | 162fabb | 2012-09-06 20:32:16 -0700 | [diff] [blame] | 19 | import libcore.util.Objects; |
| 20 | |
Fred Quintana | d4a1d2e | 2009-07-16 16:36:38 -0700 | [diff] [blame] | 21 | /** |
| 22 | * Container to ease passing around a tuple of two objects. This object provides a sensible |
| 23 | * implementation of equals(), returning true if equals() is true on each of the contained |
| 24 | * objects. |
| 25 | */ |
| 26 | public class Pair<F, S> { |
| 27 | public final F first; |
| 28 | public final S second; |
| 29 | |
| 30 | /** |
Jeff Sharkey | 162fabb | 2012-09-06 20:32:16 -0700 | [diff] [blame] | 31 | * Constructor for a Pair. |
| 32 | * |
Fred Quintana | d4a1d2e | 2009-07-16 16:36:38 -0700 | [diff] [blame] | 33 | * @param first the first object in the Pair |
| 34 | * @param second the second object in the pair |
| 35 | */ |
| 36 | public Pair(F first, S second) { |
| 37 | this.first = first; |
| 38 | this.second = second; |
| 39 | } |
| 40 | |
| 41 | /** |
Jeff Sharkey | 162fabb | 2012-09-06 20:32:16 -0700 | [diff] [blame] | 42 | * Checks the two objects for equality by delegating to their respective |
| 43 | * {@link Object#equals(Object)} methods. |
| 44 | * |
| 45 | * @param o the {@link Pair} to which this one is to be checked for equality |
| 46 | * @return true if the underlying objects of the Pair are both considered |
| 47 | * equal |
Fred Quintana | d4a1d2e | 2009-07-16 16:36:38 -0700 | [diff] [blame] | 48 | */ |
Jeff Sharkey | 162fabb | 2012-09-06 20:32:16 -0700 | [diff] [blame] | 49 | @Override |
Fred Quintana | d4a1d2e | 2009-07-16 16:36:38 -0700 | [diff] [blame] | 50 | public boolean equals(Object o) { |
Jeff Sharkey | 162fabb | 2012-09-06 20:32:16 -0700 | [diff] [blame] | 51 | if (!(o instanceof Pair)) { |
Fred Quintana | d4a1d2e | 2009-07-16 16:36:38 -0700 | [diff] [blame] | 52 | return false; |
| 53 | } |
Jeff Sharkey | 162fabb | 2012-09-06 20:32:16 -0700 | [diff] [blame] | 54 | Pair<?, ?> p = (Pair<?, ?>) o; |
| 55 | return Objects.equal(p.first, first) && Objects.equal(p.second, second); |
Fred Quintana | d4a1d2e | 2009-07-16 16:36:38 -0700 | [diff] [blame] | 56 | } |
| 57 | |
| 58 | /** |
| 59 | * Compute a hash code using the hash codes of the underlying objects |
Jeff Sharkey | 162fabb | 2012-09-06 20:32:16 -0700 | [diff] [blame] | 60 | * |
Fred Quintana | d4a1d2e | 2009-07-16 16:36:38 -0700 | [diff] [blame] | 61 | * @return a hashcode of the Pair |
| 62 | */ |
Jeff Sharkey | 162fabb | 2012-09-06 20:32:16 -0700 | [diff] [blame] | 63 | @Override |
Fred Quintana | d4a1d2e | 2009-07-16 16:36:38 -0700 | [diff] [blame] | 64 | public int hashCode() { |
Jeff Sharkey | 162fabb | 2012-09-06 20:32:16 -0700 | [diff] [blame] | 65 | return (first == null ? 0 : first.hashCode()) ^ (second == null ? 0 : second.hashCode()); |
Fred Quintana | d4a1d2e | 2009-07-16 16:36:38 -0700 | [diff] [blame] | 66 | } |
| 67 | |
| 68 | /** |
| 69 | * Convenience method for creating an appropriately typed pair. |
| 70 | * @param a the first object in the Pair |
| 71 | * @param b the second object in the pair |
| 72 | * @return a Pair that is templatized with the types of a and b |
| 73 | */ |
| 74 | public static <A, B> Pair <A, B> create(A a, B b) { |
| 75 | return new Pair<A, B>(a, b); |
| 76 | } |
| 77 | } |