blob: f96da729b8314edd1257f7b2a642c96bba742d35 [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 Sharkey7732e1e2016-03-30 17:14:23 -060019import java.util.Objects;
Jeff Sharkey162fabb2012-09-06 20:32:16 -070020
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;
Jeff Sharkey7732e1e2016-03-30 17:14:23 -060055 return Objects.equals(p.first, first) && Objects.equals(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
Jeff Sharkey7732e1e2016-03-30 17:14:23 -060068 @Override
69 public String toString() {
70 return "Pair{" + String.valueOf(first) + " " + String.valueOf(second) + "}";
71 }
72
Fred Quintanad4a1d2e2009-07-16 16:36:38 -070073 /**
74 * Convenience method for creating an appropriately typed pair.
75 * @param a the first object in the Pair
76 * @param b the second object in the pair
77 * @return a Pair that is templatized with the types of a and b
78 */
79 public static <A, B> Pair <A, B> create(A a, B b) {
80 return new Pair<A, B>(a, b);
81 }
82}