diff --git a/src/share/classes/java/util/stream/Collectors.java b/src/share/classes/java/util/stream/Collectors.java index 29a5464eee0b781e258a7810b849460a891dd163..27c7c83ead400e9f0a13a31ef44503a4ea39db97 100644 --- a/src/share/classes/java/util/stream/Collectors.java +++ b/src/share/classes/java/util/stream/Collectors.java @@ -353,6 +353,43 @@ public final class Collectors { downstream.characteristics()); } + /** + * Adapts a {@code Collector} to perform an additional finishing + * transformation. For example, one could adapt the {@link #toList()} + * collector to always produce an immutable list with: + *
{@code
+     *     List people
+     *         = people.stream().collect(collectingAndThen(toList(), Collections::unmodifiableList));
+     * }
+ * + * @param the type of the input elements + * @param intermediate accumulation type of the downstream collector + * @param result type of the downstream collector + * @param result type of the resulting collector + * @param downstream a collector + * @param finisher a function to be applied to the final result of the downstream collector + * @return a collector which performs the action of the downstream collector, + * followed by an additional finishing step + */ + public static Collector collectingAndThen(Collector downstream, + Function finisher) { + Set characteristics = downstream.characteristics(); + if (characteristics.contains(Collector.Characteristics.IDENTITY_FINISH)) { + if (characteristics.size() == 1) + characteristics = Collectors.CH_NOID; + else { + characteristics = EnumSet.copyOf(characteristics); + characteristics.remove(Collector.Characteristics.IDENTITY_FINISH); + characteristics = Collections.unmodifiableSet(characteristics); + } + } + return new CollectorImpl<>(downstream.supplier(), + downstream.accumulator(), + downstream.combiner(), + downstream.finisher().andThen(finisher), + characteristics); + } + /** * Returns a {@code Collector} accepting elements of type {@code T} that * counts the number of input elements. If no elements are present, the diff --git a/test/java/util/stream/test/org/openjdk/tests/java/util/stream/TabulatorsTest.java b/test/java/util/stream/test/org/openjdk/tests/java/util/stream/TabulatorsTest.java index 7505cb13f6e548b3a7cba64487b5b87d954526b8..f539df5d825e24685ae62499d683d6f1ae7c7efa 100644 --- a/test/java/util/stream/test/org/openjdk/tests/java/util/stream/TabulatorsTest.java +++ b/test/java/util/stream/test/org/openjdk/tests/java/util/stream/TabulatorsTest.java @@ -25,6 +25,7 @@ package org.openjdk.tests.java.util.stream; import java.util.ArrayList; import java.util.Arrays; import java.util.Collection; +import java.util.Collections; import java.util.Comparator; import java.util.HashMap; import java.util.HashSet; @@ -52,6 +53,7 @@ import java.util.stream.TestData; import org.testng.annotations.Test; +import static java.util.stream.Collectors.collectingAndThen; import static java.util.stream.Collectors.groupingBy; import static java.util.stream.Collectors.groupingByConcurrent; import static java.util.stream.Collectors.partitioningBy; @@ -603,4 +605,17 @@ public class TabulatorsTest extends OpTestCase { new PartitionAssertion<>(classifier, new ReduceAssertion<>(0, LambdaTestHelpers.identity(), Integer::sum))); } + + @Test(dataProvider = "StreamTestData", dataProviderClass = StreamTestDataProvider.class) + public void testComposeFinisher(String name, TestData.OfRef data) throws ReflectiveOperationException { + List asList = exerciseTerminalOps(data, s -> s.collect(toList())); + List asImmutableList = exerciseTerminalOps(data, s -> s.collect(collectingAndThen(toList(), Collections::unmodifiableList))); + assertEquals(asList, asImmutableList); + try { + asImmutableList.add(0); + fail("Expecting immutable result"); + } + catch (UnsupportedOperationException ignored) { } + } + }