Skip to content

Commit 162fabb

Browse files
committed
Let Pair represent null values.
Bug: 7121262 Change-Id: I067ea0a4af40a0e8935a9408404df7a2e851e22c
1 parent 1a9c0df commit 162fabb

File tree

1 file changed

+17
-16
lines changed

1 file changed

+17
-16
lines changed

core/java/android/util/Pair.java

Lines changed: 17 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,8 @@
1616

1717
package android.util;
1818

19+
import libcore.util.Objects;
20+
1921
/**
2022
* Container to ease passing around a tuple of two objects. This object provides a sensible
2123
* implementation of equals(), returning true if equals() is true on each of the contained
@@ -26,8 +28,8 @@ public class Pair<F, S> {
2628
public final S second;
2729

2830
/**
29-
* Constructor for a Pair. If either are null then equals() and hashCode() will throw
30-
* a NullPointerException.
31+
* Constructor for a Pair.
32+
*
3133
* @param first the first object in the Pair
3234
* @param second the second object in the pair
3335
*/
@@ -37,31 +39,30 @@ public Pair(F first, S second) {
3739
}
3840

3941
/**
40-
* Checks the two objects for equality by delegating to their respective equals() methods.
41-
* @param o the Pair to which this one is to be checked for equality
42-
* @return true if the underlying objects of the Pair are both considered equals()
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
4348
*/
49+
@Override
4450
public boolean equals(Object o) {
45-
if (o == this) return true;
46-
if (!(o instanceof Pair)) return false;
47-
final Pair<F, S> other;
48-
try {
49-
other = (Pair<F, S>) o;
50-
} catch (ClassCastException e) {
51+
if (!(o instanceof Pair)) {
5152
return false;
5253
}
53-
return first.equals(other.first) && second.equals(other.second);
54+
Pair<?, ?> p = (Pair<?, ?>) o;
55+
return Objects.equal(p.first, first) && Objects.equal(p.second, second);
5456
}
5557

5658
/**
5759
* Compute a hash code using the hash codes of the underlying objects
60+
*
5861
* @return a hashcode of the Pair
5962
*/
63+
@Override
6064
public int hashCode() {
61-
int result = 17;
62-
result = 31 * result + first.hashCode();
63-
result = 31 * result + second.hashCode();
64-
return result;
65+
return (first == null ? 0 : first.hashCode()) ^ (second == null ? 0 : second.hashCode());
6566
}
6667

6768
/**

0 commit comments

Comments
 (0)