Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

8317993: Add capturing factories to classes in java.util.function package #16213

Closed
wants to merge 8 commits into from
Closed
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -101,8 +101,9 @@ default BiConsumer<T, U> andThen(BiConsumer<? super T, ? super U> after) {
* @param <T> the type of the first argument to the operation
* @param <U> the type of the second argument to the operation
*/
static <T, U> BiConsumer<T, U> of(BiConsumer<T, U> uncaptured) {
return Objects.requireNonNull(uncaptured);
@SuppressWarnings("unchecked")
static <T, U> BiConsumer<T, U> of(BiConsumer<? super T, ? super U> uncaptured) {
return (BiConsumer<T, U>) Objects.requireNonNull(uncaptured);
}

}
Original file line number Diff line number Diff line change
@@ -91,8 +91,9 @@ default <V> BiFunction<T, U, V> andThen(Function<? super R, ? extends V> after)
* @param <U> the type of the second argument to the function
* @param <R> the type of the result of the function
*/
static <T, U, R> BiFunction<T, U, R> of(BiFunction<T, U, R> uncaptured) {
return Objects.requireNonNull(uncaptured);
@SuppressWarnings("unchecked")
static <T, U, R> BiFunction<T, U, R> of(BiFunction<? super T, ? super U, ? extends R> uncaptured) {
return (BiFunction<T, U, R>) Objects.requireNonNull(uncaptured);
}

}
Original file line number Diff line number Diff line change
@@ -125,8 +125,9 @@ default BiPredicate<T, U> or(BiPredicate<? super T, ? super U> other) {
* @param <T> the type of the first argument to the predicate
* @param <U> the type of the second argument the predicate
*/
static <T, U> BiPredicate<T, U> of(BiPredicate<T, U> uncaptured) {
return Objects.requireNonNull(uncaptured);
@SuppressWarnings("unchecked")
static <T, U> BiPredicate<T, U> of(BiPredicate<? super T, ? super U> uncaptured) {
return (BiPredicate<T, U>) Objects.requireNonNull(uncaptured);
}

}
5 changes: 3 additions & 2 deletions src/java.base/share/classes/java/util/function/Consumer.java
Original file line number Diff line number Diff line change
@@ -84,8 +84,9 @@ default Consumer<T> andThen(Consumer<? super T> after) {
* @param uncaptured to capture
* @param <T> the type of the input to the operation
*/
static <T> Consumer<T> of(Consumer<T> uncaptured) {
return Objects.requireNonNull(uncaptured);
@SuppressWarnings("unchecked")
static <T> Consumer<T> of(Consumer<? super T> uncaptured) {
return (Consumer<T>) Objects.requireNonNull(uncaptured);
}

}
5 changes: 3 additions & 2 deletions src/java.base/share/classes/java/util/function/Function.java
Original file line number Diff line number Diff line change
@@ -118,8 +118,9 @@ static <T> Function<T, T> identity() {
* @param <T> the type of the input to the function
* @param <R> the type of the result of the function
*/
static <T, R> Function<T, R> of(Function<T, R> uncaptured) {
return Objects.requireNonNull(uncaptured);
@SuppressWarnings("unchecked")
static <T, R> Function<T, R> of(Function<? super T, ? extends R> uncaptured) {
return (Function<T, R>) Objects.requireNonNull(uncaptured);
}

}
5 changes: 3 additions & 2 deletions src/java.base/share/classes/java/util/function/Predicate.java
Original file line number Diff line number Diff line change
@@ -157,8 +157,9 @@ static <T> Predicate<T> not(Predicate<? super T> target) {
* @param uncaptured to capture
* @param <T> the type of the input to the predicate
*/
static <T> Predicate<T> of(Predicate<T> uncaptured) {
return Objects.requireNonNull(uncaptured);
@SuppressWarnings("unchecked")
static <T> Predicate<T> of(Predicate<? super T> uncaptured) {
return (Predicate<T>) Objects.requireNonNull(uncaptured);
}

}
16 changes: 15 additions & 1 deletion test/jdk/java/util/function/CapturingFactoriesTest.java
Original file line number Diff line number Diff line change
@@ -39,7 +39,6 @@
import java.util.function.Function;
import java.util.function.Predicate;
import java.util.function.UnaryOperator;
import java.util.logging.Logger;

import static org.junit.jupiter.api.Assertions.*;

@@ -165,6 +164,21 @@ void unaryOperator() {
assertEquals("a", composed.apply(" a "));
}


@Test
void coVariants() {
var function = Function.of(Number::toString); // Function<Number, String>
Function<Integer, String> sFunction = Function.of(function); // Function<String, String>
var sFunction2 = Function.of(function); // Function<Number, String>
}

@Test
void contraVariants() {
var function = Function.of(Object::toString); // Function<Object, String>
Function<String, String> sFunction = Function.of(function); // Function<String, String>
var sFunction2 = Function.of(function); // Function<Object, String>
}

// Methods

static boolean not(boolean original) {