Skip to content

Commit

Permalink
Generalize Function and Predicate factories to let callers specif…
Browse files Browse the repository at this point in the history
…y 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
  • Loading branch information
cpovirk authored and Google Java Core Libraries committed May 14, 2021
1 parent 9ccb937 commit 75110e9
Show file tree
Hide file tree
Showing 4 changed files with 20 additions and 20 deletions.
10 changes: 5 additions & 5 deletions android/guava/src/com/google/common/base/Functions.java
Original file line number Diff line number Diff line change
Expand Up @@ -361,12 +361,12 @@ public String toString() {
*
* @since 10.0
*/
public static <T> Function<Object, T> forSupplier(Supplier<T> supplier) {
return new SupplierFunction<T>(supplier);
public static <F, T> Function<F, T> forSupplier(Supplier<T> supplier) {
return new SupplierFunction<>(supplier);
}

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

private final Supplier<T> supplier;

Expand All @@ -375,14 +375,14 @@ private SupplierFunction(Supplier<T> 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;
Expand Down
10 changes: 5 additions & 5 deletions android/guava/src/com/google/common/base/Predicates.java
Original file line number Diff line number Diff line change
Expand Up @@ -169,8 +169,8 @@ public static <T> Predicate<T> equalTo(@NullableDecl T target) {
* instances {@code Lists.newArrayList(1)} and {@code Arrays.asList(1)}.
*/
@GwtIncompatible // Class.isInstance
public static Predicate<Object> instanceOf(Class<?> clazz) {
return new InstanceOfPredicate(clazz);
public static <T> Predicate<T> instanceOf(Class<?> clazz) {
return new InstanceOfPredicate<T>(clazz);
}

/**
Expand Down Expand Up @@ -472,15 +472,15 @@ public String toString() {

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

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

@Override
public boolean apply(@NullableDecl Object o) {
public boolean apply(@NullableDecl T o) {
return clazz.isInstance(o);
}

Expand All @@ -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;
Expand Down
10 changes: 5 additions & 5 deletions guava/src/com/google/common/base/Functions.java
Original file line number Diff line number Diff line change
Expand Up @@ -359,12 +359,12 @@ public String toString() {
*
* @since 10.0
*/
public static <T> Function<Object, T> forSupplier(Supplier<T> supplier) {
return new SupplierFunction<T>(supplier);
public static <F, T> Function<F, T> forSupplier(Supplier<T> supplier) {
return new SupplierFunction<>(supplier);
}

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

private final Supplier<T> supplier;

Expand All @@ -373,14 +373,14 @@ private SupplierFunction(Supplier<T> 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;
Expand Down
10 changes: 5 additions & 5 deletions guava/src/com/google/common/base/Predicates.java
Original file line number Diff line number Diff line change
Expand Up @@ -169,8 +169,8 @@ public static <T> Predicate<T> equalTo(@Nullable T target) {
* instances {@code Lists.newArrayList(1)} and {@code Arrays.asList(1)}.
*/
@GwtIncompatible // Class.isInstance
public static Predicate<Object> instanceOf(Class<?> clazz) {
return new InstanceOfPredicate(clazz);
public static <T> Predicate<T> instanceOf(Class<?> clazz) {
return new InstanceOfPredicate<T>(clazz);
}

/**
Expand Down Expand Up @@ -472,15 +472,15 @@ public String toString() {

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

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

@Override
public boolean apply(@Nullable Object o) {
public boolean apply(@Nullable T o) {
return clazz.isInstance(o);
}

Expand All @@ -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;
Expand Down

0 comments on commit 75110e9

Please sign in to comment.