1559b5e8554651ffc9f9cc639f8e363b9494fc98aGeorge Mount/*
2559b5e8554651ffc9f9cc639f8e363b9494fc98aGeorge Mount * Copyright (C) 2009 The Android Open Source Project
3559b5e8554651ffc9f9cc639f8e363b9494fc98aGeorge Mount *
4559b5e8554651ffc9f9cc639f8e363b9494fc98aGeorge Mount * Licensed under the Apache License, Version 2.0 (the "License");
5559b5e8554651ffc9f9cc639f8e363b9494fc98aGeorge Mount * you may not use this file except in compliance with the License.
6559b5e8554651ffc9f9cc639f8e363b9494fc98aGeorge Mount * You may obtain a copy of the License at
7559b5e8554651ffc9f9cc639f8e363b9494fc98aGeorge Mount *
8559b5e8554651ffc9f9cc639f8e363b9494fc98aGeorge Mount *      http://www.apache.org/licenses/LICENSE-2.0
9559b5e8554651ffc9f9cc639f8e363b9494fc98aGeorge Mount *
10559b5e8554651ffc9f9cc639f8e363b9494fc98aGeorge Mount * Unless required by applicable law or agreed to in writing, software
11559b5e8554651ffc9f9cc639f8e363b9494fc98aGeorge Mount * distributed under the License is distributed on an "AS IS" BASIS,
12559b5e8554651ffc9f9cc639f8e363b9494fc98aGeorge Mount * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13559b5e8554651ffc9f9cc639f8e363b9494fc98aGeorge Mount * See the License for the specific language governing permissions and
14559b5e8554651ffc9f9cc639f8e363b9494fc98aGeorge Mount * limitations under the License.
15559b5e8554651ffc9f9cc639f8e363b9494fc98aGeorge Mount */
16559b5e8554651ffc9f9cc639f8e363b9494fc98aGeorge Mount
17559b5e8554651ffc9f9cc639f8e363b9494fc98aGeorge Mountpackage android.support.v4.util;
18559b5e8554651ffc9f9cc639f8e363b9494fc98aGeorge Mount
19559b5e8554651ffc9f9cc639f8e363b9494fc98aGeorge Mount/**
20559b5e8554651ffc9f9cc639f8e363b9494fc98aGeorge Mount * Container to ease passing around a tuple of two objects. This object provides a sensible
21559b5e8554651ffc9f9cc639f8e363b9494fc98aGeorge Mount * implementation of equals(), returning true if equals() is true on each of the contained
22559b5e8554651ffc9f9cc639f8e363b9494fc98aGeorge Mount * objects.
23559b5e8554651ffc9f9cc639f8e363b9494fc98aGeorge Mount */
24559b5e8554651ffc9f9cc639f8e363b9494fc98aGeorge Mountpublic class Pair<F, S> {
25559b5e8554651ffc9f9cc639f8e363b9494fc98aGeorge Mount    public final F first;
26559b5e8554651ffc9f9cc639f8e363b9494fc98aGeorge Mount    public final S second;
27559b5e8554651ffc9f9cc639f8e363b9494fc98aGeorge Mount
28559b5e8554651ffc9f9cc639f8e363b9494fc98aGeorge Mount    /**
29559b5e8554651ffc9f9cc639f8e363b9494fc98aGeorge Mount     * Constructor for a Pair.
30559b5e8554651ffc9f9cc639f8e363b9494fc98aGeorge Mount     *
31559b5e8554651ffc9f9cc639f8e363b9494fc98aGeorge Mount     * @param first the first object in the Pair
32559b5e8554651ffc9f9cc639f8e363b9494fc98aGeorge Mount     * @param second the second object in the pair
33559b5e8554651ffc9f9cc639f8e363b9494fc98aGeorge Mount     */
34559b5e8554651ffc9f9cc639f8e363b9494fc98aGeorge Mount    public Pair(F first, S second) {
35559b5e8554651ffc9f9cc639f8e363b9494fc98aGeorge Mount        this.first = first;
36559b5e8554651ffc9f9cc639f8e363b9494fc98aGeorge Mount        this.second = second;
37559b5e8554651ffc9f9cc639f8e363b9494fc98aGeorge Mount    }
38559b5e8554651ffc9f9cc639f8e363b9494fc98aGeorge Mount
39559b5e8554651ffc9f9cc639f8e363b9494fc98aGeorge Mount    /**
40559b5e8554651ffc9f9cc639f8e363b9494fc98aGeorge Mount     * Checks the two objects for equality by delegating to their respective
41559b5e8554651ffc9f9cc639f8e363b9494fc98aGeorge Mount     * {@link Object#equals(Object)} methods.
42559b5e8554651ffc9f9cc639f8e363b9494fc98aGeorge Mount     *
43559b5e8554651ffc9f9cc639f8e363b9494fc98aGeorge Mount     * @param o the {@link Pair} to which this one is to be checked for equality
44559b5e8554651ffc9f9cc639f8e363b9494fc98aGeorge Mount     * @return true if the underlying objects of the Pair are both considered
45559b5e8554651ffc9f9cc639f8e363b9494fc98aGeorge Mount     *         equal
46559b5e8554651ffc9f9cc639f8e363b9494fc98aGeorge Mount     */
47559b5e8554651ffc9f9cc639f8e363b9494fc98aGeorge Mount    @Override
48559b5e8554651ffc9f9cc639f8e363b9494fc98aGeorge Mount    public boolean equals(Object o) {
49559b5e8554651ffc9f9cc639f8e363b9494fc98aGeorge Mount        if (!(o instanceof Pair)) {
50559b5e8554651ffc9f9cc639f8e363b9494fc98aGeorge Mount            return false;
51559b5e8554651ffc9f9cc639f8e363b9494fc98aGeorge Mount        }
52559b5e8554651ffc9f9cc639f8e363b9494fc98aGeorge Mount        Pair<?, ?> p = (Pair<?, ?>) o;
53559b5e8554651ffc9f9cc639f8e363b9494fc98aGeorge Mount        return objectsEqual(p.first, first) && objectsEqual(p.second, second);
54559b5e8554651ffc9f9cc639f8e363b9494fc98aGeorge Mount    }
55559b5e8554651ffc9f9cc639f8e363b9494fc98aGeorge Mount
56559b5e8554651ffc9f9cc639f8e363b9494fc98aGeorge Mount    private static boolean objectsEqual(Object a, Object b) {
57559b5e8554651ffc9f9cc639f8e363b9494fc98aGeorge Mount        return a == b || (a != null && a.equals(b));
58559b5e8554651ffc9f9cc639f8e363b9494fc98aGeorge Mount    }
59559b5e8554651ffc9f9cc639f8e363b9494fc98aGeorge Mount
60559b5e8554651ffc9f9cc639f8e363b9494fc98aGeorge Mount    /**
61559b5e8554651ffc9f9cc639f8e363b9494fc98aGeorge Mount     * Compute a hash code using the hash codes of the underlying objects
62559b5e8554651ffc9f9cc639f8e363b9494fc98aGeorge Mount     *
63559b5e8554651ffc9f9cc639f8e363b9494fc98aGeorge Mount     * @return a hashcode of the Pair
64559b5e8554651ffc9f9cc639f8e363b9494fc98aGeorge Mount     */
65559b5e8554651ffc9f9cc639f8e363b9494fc98aGeorge Mount    @Override
66559b5e8554651ffc9f9cc639f8e363b9494fc98aGeorge Mount    public int hashCode() {
67559b5e8554651ffc9f9cc639f8e363b9494fc98aGeorge Mount        return (first == null ? 0 : first.hashCode()) ^ (second == null ? 0 : second.hashCode());
68559b5e8554651ffc9f9cc639f8e363b9494fc98aGeorge Mount    }
69559b5e8554651ffc9f9cc639f8e363b9494fc98aGeorge Mount
70559b5e8554651ffc9f9cc639f8e363b9494fc98aGeorge Mount    /**
71559b5e8554651ffc9f9cc639f8e363b9494fc98aGeorge Mount     * Convenience method for creating an appropriately typed pair.
72559b5e8554651ffc9f9cc639f8e363b9494fc98aGeorge Mount     * @param a the first object in the Pair
73559b5e8554651ffc9f9cc639f8e363b9494fc98aGeorge Mount     * @param b the second object in the pair
74559b5e8554651ffc9f9cc639f8e363b9494fc98aGeorge Mount     * @return a Pair that is templatized with the types of a and b
75559b5e8554651ffc9f9cc639f8e363b9494fc98aGeorge Mount     */
76559b5e8554651ffc9f9cc639f8e363b9494fc98aGeorge Mount    public static <A, B> Pair <A, B> create(A a, B b) {
77559b5e8554651ffc9f9cc639f8e363b9494fc98aGeorge Mount        return new Pair<A, B>(a, b);
78559b5e8554651ffc9f9cc639f8e363b9494fc98aGeorge Mount    }
79559b5e8554651ffc9f9cc639f8e363b9494fc98aGeorge Mount}
80