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

Per Minborg pminborg at openjdk.org
Tue Oct 17 08:12:39 UTC 2023


This PR proposes to add a number of "capturing factories" in classes in the `java.util.function` package.

The PR additionally (an optionally) proposes to add a new function `UnaryOperator::andThenUnary` to allow composition while retaining the `UnaryOperator` type.

With the new change, it is possible to write code like this (example from `java.util.function.Function`):


// Resolve ambiguity
var function = Function.of(String::isEmpty); // Function<String, Boolean>
var predicate = Predicate.of(String::isEmpty); // Predicate<String>

// Fluent composition
var chained = Function.of(String::length)       // Function<String, Integer>
                   .andThen(Integer::byteValue);      // Function<String, Byte>


Please see the original bug report for a comprehensive description of these proposed changes.

Note: It is not the objective to promote `var` declaration or to prevent previous ways of capturing lambdas and method references. The comments in the code above is for explaining the binding and once that becomes obvious, such comments are likely to not appear in real code. Users that prefers having a normal type declaration can still do that.

-------------

Commit messages:
 - Add test and improve
 - Add factories to additional functional interfaces
 - Merge branch 'master' into capturing-factories
 - Initial commit

Changes: https://git.openjdk.org/jdk/pull/16213/files
 Webrev: https://webrevs.openjdk.org/?repo=jdk&pr=16213&range=00
  Issue: https://bugs.openjdk.org/browse/JDK-8317993
  Stats: 421 lines in 9 files changed: 413 ins; 0 del; 8 mod
  Patch: https://git.openjdk.org/jdk/pull/16213.diff
  Fetch: git fetch https://git.openjdk.org/jdk.git pull/16213/head:pull/16213

PR: https://git.openjdk.org/jdk/pull/16213


More information about the core-libs-dev mailing list