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 // Exported to Mainline modules; cannot use annotations
29 // @android.ravenwood.annotation.RavenwoodKeepWholeClass
30 public class Pair<F, S> {
31     public final F first;
32     public final S second;
33 
34     /**
35      * Constructor for a Pair.
36      *
37      * @param first the first object in the Pair
38      * @param second the second object in the pair
39      */
Pair(F first, S second)40     public Pair(F first, S second) {
41         this.first = first;
42         this.second = second;
43     }
44 
45     /**
46      * Checks the two objects for equality by delegating to their respective
47      * {@link Object#equals(Object)} methods.
48      *
49      * @param o the {@link Pair} to which this one is to be checked for equality
50      * @return true if the underlying objects of the Pair are both considered
51      *         equal
52      */
53     @Override
equals(@ullable Object o)54     public boolean equals(@Nullable Object o) {
55         if (!(o instanceof Pair)) {
56             return false;
57         }
58         Pair<?, ?> p = (Pair<?, ?>) o;
59         return Objects.equals(p.first, first) && Objects.equals(p.second, second);
60     }
61 
62     /**
63      * Compute a hash code using the hash codes of the underlying objects
64      *
65      * @return a hashcode of the Pair
66      */
67     @Override
hashCode()68     public int hashCode() {
69         return (first == null ? 0 : first.hashCode()) ^ (second == null ? 0 : second.hashCode());
70     }
71 
72     @Override
toString()73     public String toString() {
74         return "Pair{" + String.valueOf(first) + " " + String.valueOf(second) + "}";
75     }
76 
77     /**
78      * Convenience method for creating an appropriately typed pair.
79      * @param a the first object in the Pair
80      * @param b the second object in the pair
81      * @return a Pair that is templatized with the types of a and b
82      */
create(A a, B b)83     public static <A, B> Pair <A, B> create(A a, B b) {
84         return new Pair<A, B>(a, b);
85     }
86 }
87