blob: 6027d081510de2dab74853bf477c713bb9397976 [file] [log] [blame]
Fred Quintanad4a1d2e2009-07-16 16:36:38 -07001/*
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.util;
18
Jeff Sharkey162fabb2012-09-06 20:32:16 -070019import libcore.util.Objects;
20
Fred Quintanad4a1d2e2009-07-16 16:36:38 -070021/**
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 */
26public class Pair<F, S> {
27 public final F first;
28 public final S second;
29
30 /**
Jeff Sharkey162fabb2012-09-06 20:32:16 -070031 * Constructor for a Pair.
32 *
Fred Quintanad4a1d2e2009-07-16 16:36:38 -070033 * @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 Sharkey162fabb2012-09-06 20:32:16 -070042 * 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 Quintanad4a1d2e2009-07-16 16:36:38 -070048 */
Jeff Sharkey162fabb2012-09-06 20:32:16 -070049 @Override
Fred Quintanad4a1d2e2009-07-16 16:36:38 -070050 public boolean equals(Object o) {
Jeff Sharkey162fabb2012-09-06 20:32:16 -070051 if (!(o instanceof Pair)) {
Fred Quintanad4a1d2e2009-07-16 16:36:38 -070052 return false;
53 }
Jeff Sharkey162fabb2012-09-06 20:32:16 -070054 Pair<?, ?> p = (Pair<?, ?>) o;
55 return Objects.equal(p.first, first) && Objects.equal(p.second, second);
Fred Quintanad4a1d2e2009-07-16 16:36:38 -070056 }
57
58 /**
59 * Compute a hash code using the hash codes of the underlying objects
Jeff Sharkey162fabb2012-09-06 20:32:16 -070060 *
Fred Quintanad4a1d2e2009-07-16 16:36:38 -070061 * @return a hashcode of the Pair
62 */
Jeff Sharkey162fabb2012-09-06 20:32:16 -070063 @Override
Fred Quintanad4a1d2e2009-07-16 16:36:38 -070064 public int hashCode() {
Jeff Sharkey162fabb2012-09-06 20:32:16 -070065 return (first == null ? 0 : first.hashCode()) ^ (second == null ? 0 : second.hashCode());
Fred Quintanad4a1d2e2009-07-16 16:36:38 -070066 }
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}