Skip to content

Commit 75110e9

Browse files
cpovirkGoogle Java Core Libraries
authored and
Google Java Core Libraries
committed
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
1 parent 9ccb937 commit 75110e9

File tree

4 files changed

+20
-20
lines changed

4 files changed

+20
-20
lines changed

android/guava/src/com/google/common/base/Functions.java

+5-5
Original file line numberDiff line numberDiff line change
@@ -361,12 +361,12 @@ public String toString() {
361361
*
362362
* @since 10.0
363363
*/
364-
public static <T> Function<Object, T> forSupplier(Supplier<T> supplier) {
365-
return new SupplierFunction<T>(supplier);
364+
public static <F, T> Function<F, T> forSupplier(Supplier<T> supplier) {
365+
return new SupplierFunction<>(supplier);
366366
}
367367

368368
/** @see Functions#forSupplier */
369-
private static class SupplierFunction<T> implements Function<Object, T>, Serializable {
369+
private static class SupplierFunction<F, T> implements Function<F, T>, Serializable {
370370

371371
private final Supplier<T> supplier;
372372

@@ -375,14 +375,14 @@ private SupplierFunction(Supplier<T> supplier) {
375375
}
376376

377377
@Override
378-
public T apply(@NullableDecl Object input) {
378+
public T apply(@NullableDecl F input) {
379379
return supplier.get();
380380
}
381381

382382
@Override
383383
public boolean equals(@NullableDecl Object obj) {
384384
if (obj instanceof SupplierFunction) {
385-
SupplierFunction<?> that = (SupplierFunction<?>) obj;
385+
SupplierFunction<?, ?> that = (SupplierFunction<?, ?>) obj;
386386
return this.supplier.equals(that.supplier);
387387
}
388388
return false;

android/guava/src/com/google/common/base/Predicates.java

+5-5
Original file line numberDiff line numberDiff line change
@@ -169,8 +169,8 @@ public static <T> Predicate<T> equalTo(@NullableDecl T target) {
169169
* instances {@code Lists.newArrayList(1)} and {@code Arrays.asList(1)}.
170170
*/
171171
@GwtIncompatible // Class.isInstance
172-
public static Predicate<Object> instanceOf(Class<?> clazz) {
173-
return new InstanceOfPredicate(clazz);
172+
public static <T> Predicate<T> instanceOf(Class<?> clazz) {
173+
return new InstanceOfPredicate<T>(clazz);
174174
}
175175

176176
/**
@@ -472,15 +472,15 @@ public String toString() {
472472

473473
/** @see Predicates#instanceOf(Class) */
474474
@GwtIncompatible // Class.isInstance
475-
private static class InstanceOfPredicate implements Predicate<Object>, Serializable {
475+
private static class InstanceOfPredicate<T> implements Predicate<T>, Serializable {
476476
private final Class<?> clazz;
477477

478478
private InstanceOfPredicate(Class<?> clazz) {
479479
this.clazz = checkNotNull(clazz);
480480
}
481481

482482
@Override
483-
public boolean apply(@NullableDecl Object o) {
483+
public boolean apply(@NullableDecl T o) {
484484
return clazz.isInstance(o);
485485
}
486486

@@ -492,7 +492,7 @@ public int hashCode() {
492492
@Override
493493
public boolean equals(@NullableDecl Object obj) {
494494
if (obj instanceof InstanceOfPredicate) {
495-
InstanceOfPredicate that = (InstanceOfPredicate) obj;
495+
InstanceOfPredicate<?> that = (InstanceOfPredicate<?>) obj;
496496
return clazz == that.clazz;
497497
}
498498
return false;

guava/src/com/google/common/base/Functions.java

+5-5
Original file line numberDiff line numberDiff line change
@@ -359,12 +359,12 @@ public String toString() {
359359
*
360360
* @since 10.0
361361
*/
362-
public static <T> Function<Object, T> forSupplier(Supplier<T> supplier) {
363-
return new SupplierFunction<T>(supplier);
362+
public static <F, T> Function<F, T> forSupplier(Supplier<T> supplier) {
363+
return new SupplierFunction<>(supplier);
364364
}
365365

366366
/** @see Functions#forSupplier */
367-
private static class SupplierFunction<T> implements Function<Object, T>, Serializable {
367+
private static class SupplierFunction<F, T> implements Function<F, T>, Serializable {
368368

369369
private final Supplier<T> supplier;
370370

@@ -373,14 +373,14 @@ private SupplierFunction(Supplier<T> supplier) {
373373
}
374374

375375
@Override
376-
public T apply(@Nullable Object input) {
376+
public T apply(@Nullable F input) {
377377
return supplier.get();
378378
}
379379

380380
@Override
381381
public boolean equals(@Nullable Object obj) {
382382
if (obj instanceof SupplierFunction) {
383-
SupplierFunction<?> that = (SupplierFunction<?>) obj;
383+
SupplierFunction<?, ?> that = (SupplierFunction<?, ?>) obj;
384384
return this.supplier.equals(that.supplier);
385385
}
386386
return false;

guava/src/com/google/common/base/Predicates.java

+5-5
Original file line numberDiff line numberDiff line change
@@ -169,8 +169,8 @@ public static <T> Predicate<T> equalTo(@Nullable T target) {
169169
* instances {@code Lists.newArrayList(1)} and {@code Arrays.asList(1)}.
170170
*/
171171
@GwtIncompatible // Class.isInstance
172-
public static Predicate<Object> instanceOf(Class<?> clazz) {
173-
return new InstanceOfPredicate(clazz);
172+
public static <T> Predicate<T> instanceOf(Class<?> clazz) {
173+
return new InstanceOfPredicate<T>(clazz);
174174
}
175175

176176
/**
@@ -472,15 +472,15 @@ public String toString() {
472472

473473
/** @see Predicates#instanceOf(Class) */
474474
@GwtIncompatible // Class.isInstance
475-
private static class InstanceOfPredicate implements Predicate<Object>, Serializable {
475+
private static class InstanceOfPredicate<T> implements Predicate<T>, Serializable {
476476
private final Class<?> clazz;
477477

478478
private InstanceOfPredicate(Class<?> clazz) {
479479
this.clazz = checkNotNull(clazz);
480480
}
481481

482482
@Override
483-
public boolean apply(@Nullable Object o) {
483+
public boolean apply(@Nullable T o) {
484484
return clazz.isInstance(o);
485485
}
486486

@@ -492,7 +492,7 @@ public int hashCode() {
492492
@Override
493493
public boolean equals(@Nullable Object obj) {
494494
if (obj instanceof InstanceOfPredicate) {
495-
InstanceOfPredicate that = (InstanceOfPredicate) obj;
495+
InstanceOfPredicate<?> that = (InstanceOfPredicate<?>) obj;
496496
return clazz == that.clazz;
497497
}
498498
return false;

0 commit comments

Comments
 (0)