From 75110e936dcc50a75df10ffab260d30a073bf00e Mon Sep 17 00:00:00 2001 From: cpovirk Date: Fri, 14 May 2021 03:46:11 -0700 Subject: [PATCH] Generalize `Function` and `Predicate` factories to let callers specify the desired input type. RELNOTES=`base`: Changed `Functions.forSupplier` and `Predicates.instanceOf` to accept an additional type argument to specify the input type for the returned `Function`/`Predicate`. The flexibility we're adding should typically not be necessary if users follow the [PECS](https://stackoverflow.com/a/2723538/28465) principle, but it can be useful in some cases, particularly around nullness analysis. Note that this change may require updates to callers' source code (to specify an additional type argument). Still, it maintains _binary_ compatibility. PiperOrigin-RevId: 373757473 --- .../guava/src/com/google/common/base/Functions.java | 10 +++++----- .../guava/src/com/google/common/base/Predicates.java | 10 +++++----- guava/src/com/google/common/base/Functions.java | 10 +++++----- guava/src/com/google/common/base/Predicates.java | 10 +++++----- 4 files changed, 20 insertions(+), 20 deletions(-) diff --git a/android/guava/src/com/google/common/base/Functions.java b/android/guava/src/com/google/common/base/Functions.java index 805f15c73727..8c8e1d5cb3a6 100644 --- a/android/guava/src/com/google/common/base/Functions.java +++ b/android/guava/src/com/google/common/base/Functions.java @@ -361,12 +361,12 @@ public String toString() { * * @since 10.0 */ - public static Function forSupplier(Supplier supplier) { - return new SupplierFunction(supplier); + public static Function forSupplier(Supplier supplier) { + return new SupplierFunction<>(supplier); } /** @see Functions#forSupplier */ - private static class SupplierFunction implements Function, Serializable { + private static class SupplierFunction implements Function, Serializable { private final Supplier supplier; @@ -375,14 +375,14 @@ private SupplierFunction(Supplier supplier) { } @Override - public T apply(@NullableDecl Object input) { + public T apply(@NullableDecl F input) { return supplier.get(); } @Override public boolean equals(@NullableDecl Object obj) { if (obj instanceof SupplierFunction) { - SupplierFunction that = (SupplierFunction) obj; + SupplierFunction that = (SupplierFunction) obj; return this.supplier.equals(that.supplier); } return false; diff --git a/android/guava/src/com/google/common/base/Predicates.java b/android/guava/src/com/google/common/base/Predicates.java index 9c8f1e5ea256..75d95fdfedec 100644 --- a/android/guava/src/com/google/common/base/Predicates.java +++ b/android/guava/src/com/google/common/base/Predicates.java @@ -169,8 +169,8 @@ public static Predicate equalTo(@NullableDecl T target) { * instances {@code Lists.newArrayList(1)} and {@code Arrays.asList(1)}. */ @GwtIncompatible // Class.isInstance - public static Predicate instanceOf(Class clazz) { - return new InstanceOfPredicate(clazz); + public static Predicate instanceOf(Class clazz) { + return new InstanceOfPredicate(clazz); } /** @@ -472,7 +472,7 @@ public String toString() { /** @see Predicates#instanceOf(Class) */ @GwtIncompatible // Class.isInstance - private static class InstanceOfPredicate implements Predicate, Serializable { + private static class InstanceOfPredicate implements Predicate, Serializable { private final Class clazz; private InstanceOfPredicate(Class clazz) { @@ -480,7 +480,7 @@ private InstanceOfPredicate(Class clazz) { } @Override - public boolean apply(@NullableDecl Object o) { + public boolean apply(@NullableDecl T o) { return clazz.isInstance(o); } @@ -492,7 +492,7 @@ public int hashCode() { @Override public boolean equals(@NullableDecl Object obj) { if (obj instanceof InstanceOfPredicate) { - InstanceOfPredicate that = (InstanceOfPredicate) obj; + InstanceOfPredicate that = (InstanceOfPredicate) obj; return clazz == that.clazz; } return false; diff --git a/guava/src/com/google/common/base/Functions.java b/guava/src/com/google/common/base/Functions.java index be845c6a45ff..351813bba885 100644 --- a/guava/src/com/google/common/base/Functions.java +++ b/guava/src/com/google/common/base/Functions.java @@ -359,12 +359,12 @@ public String toString() { * * @since 10.0 */ - public static Function forSupplier(Supplier supplier) { - return new SupplierFunction(supplier); + public static Function forSupplier(Supplier supplier) { + return new SupplierFunction<>(supplier); } /** @see Functions#forSupplier */ - private static class SupplierFunction implements Function, Serializable { + private static class SupplierFunction implements Function, Serializable { private final Supplier supplier; @@ -373,14 +373,14 @@ private SupplierFunction(Supplier supplier) { } @Override - public T apply(@Nullable Object input) { + public T apply(@Nullable F input) { return supplier.get(); } @Override public boolean equals(@Nullable Object obj) { if (obj instanceof SupplierFunction) { - SupplierFunction that = (SupplierFunction) obj; + SupplierFunction that = (SupplierFunction) obj; return this.supplier.equals(that.supplier); } return false; diff --git a/guava/src/com/google/common/base/Predicates.java b/guava/src/com/google/common/base/Predicates.java index 033ab6c26c37..3fd950bc20ee 100644 --- a/guava/src/com/google/common/base/Predicates.java +++ b/guava/src/com/google/common/base/Predicates.java @@ -169,8 +169,8 @@ public static Predicate equalTo(@Nullable T target) { * instances {@code Lists.newArrayList(1)} and {@code Arrays.asList(1)}. */ @GwtIncompatible // Class.isInstance - public static Predicate instanceOf(Class clazz) { - return new InstanceOfPredicate(clazz); + public static Predicate instanceOf(Class clazz) { + return new InstanceOfPredicate(clazz); } /** @@ -472,7 +472,7 @@ public String toString() { /** @see Predicates#instanceOf(Class) */ @GwtIncompatible // Class.isInstance - private static class InstanceOfPredicate implements Predicate, Serializable { + private static class InstanceOfPredicate implements Predicate, Serializable { private final Class clazz; private InstanceOfPredicate(Class clazz) { @@ -480,7 +480,7 @@ private InstanceOfPredicate(Class clazz) { } @Override - public boolean apply(@Nullable Object o) { + public boolean apply(@Nullable T o) { return clazz.isInstance(o); } @@ -492,7 +492,7 @@ public int hashCode() { @Override public boolean equals(@Nullable Object obj) { if (obj instanceof InstanceOfPredicate) { - InstanceOfPredicate that = (InstanceOfPredicate) obj; + InstanceOfPredicate that = (InstanceOfPredicate) obj; return clazz == that.clazz; } return false;