diff --git a/src/java.base/share/classes/java/util/stream/Gatherer.java b/src/java.base/share/classes/java/util/stream/Gatherer.java index 267ec62b931..0578ac15d40 100644 --- a/src/java.base/share/classes/java/util/stream/Gatherer.java +++ b/src/java.base/share/classes/java/util/stream/Gatherer.java @@ -24,6 +24,9 @@ */ package java.util.stream; +import org.jspecify.annotations.NullMarked; +import org.jspecify.annotations.Nullable; + import jdk.internal.vm.annotation.ForceInline; import java.util.*; @@ -196,7 +199,8 @@ * @param the type of output elements from the gatherer operation * @since 24 */ -public interface Gatherer { +@NullMarked +public interface Gatherer { /** * A function that produces an instance of the intermediate state used for * this gathering operation. @@ -265,7 +269,7 @@ default BiConsumer> finisher() { * @return returns a composed Gatherer which connects the output of this * Gatherer as input that Gatherer */ - default Gatherer andThen(Gatherer that) { + default Gatherer andThen(Gatherer that) { Objects.requireNonNull(that); return Gatherers.Composite.of(this, that); } @@ -280,7 +284,7 @@ default BiConsumer> finisher() { * @return the instance of the default initializer * @param the type of the state of the returned initializer */ - static Supplier defaultInitializer() { + static Supplier<@Nullable A> defaultInitializer() { return Gatherers.Value.DEFAULT.initializer(); } @@ -295,7 +299,7 @@ static Supplier defaultInitializer() { * @return the instance of the default combiner * @param the type of the state of the returned combiner */ - static BinaryOperator defaultCombiner() { + static BinaryOperator defaultCombiner() { return Gatherers.Value.DEFAULT.combiner(); } @@ -312,7 +316,7 @@ static BinaryOperator defaultCombiner() { * @param the type of the state of the returned finisher * @param the type of the Downstream of the returned finisher */ - static BiConsumer> defaultFinisher() { + static BiConsumer> defaultFinisher() { return Gatherers.Value.DEFAULT.finisher(); } @@ -326,8 +330,8 @@ static BiConsumer> defaultFinisher() { * @throws NullPointerException if the argument is {@code null} * @return the new {@code Gatherer} */ - static Gatherer ofSequential( - Integrator integrator) { + static Gatherer ofSequential( + Integrator<@Nullable Void, T, R> integrator) { return of( defaultInitializer(), integrator, @@ -347,9 +351,9 @@ static Gatherer ofSequential( * @throws NullPointerException if any argument is {@code null} * @return the new {@code Gatherer} */ - static Gatherer ofSequential( - Integrator integrator, - BiConsumer> finisher) { + static Gatherer ofSequential( + Integrator<@Nullable Void, T, R> integrator, + BiConsumer<@Nullable Void, Downstream> finisher) { return of( defaultInitializer(), integrator, @@ -370,7 +374,7 @@ static Gatherer ofSequential( * @throws NullPointerException if any argument is {@code null} * @return the new {@code Gatherer} */ - static Gatherer ofSequential( + static Gatherer ofSequential( Supplier initializer, Integrator integrator) { return of( @@ -394,7 +398,7 @@ static Gatherer ofSequential( * @throws NullPointerException if any argument is {@code null} * @return the new {@code Gatherer} */ - static Gatherer ofSequential( + static Gatherer ofSequential( Supplier initializer, Integrator integrator, BiConsumer> finisher) { @@ -416,7 +420,7 @@ static Gatherer ofSequential( * @throws NullPointerException if any argument is {@code null} * @return the new {@code Gatherer} */ - static Gatherer of(Integrator integrator) { + static Gatherer of(Integrator<@Nullable Void, T, R> integrator) { return of( defaultInitializer(), integrator, @@ -436,9 +440,9 @@ static Gatherer of(Integrator integrator) { * @throws NullPointerException if any argument is {@code null} * @return the new {@code Gatherer} */ - static Gatherer of( - Integrator integrator, - BiConsumer> finisher) { + static Gatherer of( + Integrator<@Nullable Void, T, R> integrator, + BiConsumer<@Nullable Void, Downstream> finisher) { return of( defaultInitializer(), integrator, @@ -462,7 +466,7 @@ static Gatherer of( * @throws NullPointerException if any argument is {@code null} * @return the new {@code Gatherer} */ - static Gatherer of( + static Gatherer of( Supplier initializer, Integrator integrator, BinaryOperator combiner, @@ -482,7 +486,7 @@ static Gatherer of( * @since 24 */ @FunctionalInterface - interface Downstream { + interface Downstream { /** * Pushes, if possible, the provided element downstream -- to the next @@ -524,7 +528,7 @@ interface Downstream { * @since 24 */ @FunctionalInterface - interface Integrator { + interface Integrator { /** * Performs an action given: the current state, the next element, and * a downstream object; potentially inspecting and/or updating @@ -550,7 +554,7 @@ interface Integrator { * @param the type of results this integrator can produce */ @ForceInline - static Integrator of(Integrator integrator) { + static Integrator of(Integrator integrator) { return integrator; } @@ -565,7 +569,7 @@ static Integrator of(Integrator integrator) { * @param the type of results this integrator can produce */ @ForceInline - static Greedy ofGreedy(Greedy greedy) { + static Greedy ofGreedy(Greedy greedy) { return greedy; } @@ -583,6 +587,6 @@ static Greedy ofGreedy(Greedy greedy) { * @since 24 */ @FunctionalInterface - interface Greedy extends Integrator { } + interface Greedy extends Integrator { } } } diff --git a/src/java.base/share/classes/java/util/stream/Gatherers.java b/src/java.base/share/classes/java/util/stream/Gatherers.java index b394f6fc7d8..6434c762972 100644 --- a/src/java.base/share/classes/java/util/stream/Gatherers.java +++ b/src/java.base/share/classes/java/util/stream/Gatherers.java @@ -24,6 +24,9 @@ */ package java.util.stream; +import org.jspecify.annotations.NullMarked; +import org.jspecify.annotations.Nullable; + import jdk.internal.access.SharedSecrets; import jdk.internal.vm.annotation.ForceInline; @@ -49,6 +52,7 @@ * * @since 24 */ +@NullMarked public final class Gatherers { private Gatherers() { } // This class is not intended to be instantiated @@ -83,7 +87,7 @@ private Gatherers() { } // This class is not intended to be instantiated * @return a new gatherer which groups elements into fixed-size windows * @throws IllegalArgumentException when {@code windowSize} is less than 1 */ - public static Gatherer> windowFixed(int windowSize) { + public static Gatherer> windowFixed(int windowSize) { if (windowSize < 1) throw new IllegalArgumentException("'windowSize' must be greater than zero"); @@ -172,7 +176,7 @@ void finish(Downstream> downstream) { * @return a new gatherer which groups elements into sliding windows * @throws IllegalArgumentException when windowSize is less than 1 */ - public static Gatherer> windowSliding(int windowSize) { + public static Gatherer> windowSliding(int windowSize) { if (windowSize < 1) throw new IllegalArgumentException("'windowSize' must be greater than zero"); @@ -259,7 +263,7 @@ void finish(Downstream> downstream) { * @return a new Gatherer * @throws NullPointerException if any of the parameters are {@code null} */ - public static Gatherer fold( + public static Gatherer fold( Supplier initial, BiFunction folder) { Objects.requireNonNull(initial, "'initial' must not be null"); @@ -306,7 +310,7 @@ class State { * @return a new Gatherer which performs a prefix scan * @throws NullPointerException if any of the parameters are {@code null} */ - public static Gatherer scan( + public static Gatherer scan( Supplier initial, BiFunction scanner) { Objects.requireNonNull(initial, "'initial' must not be null"); @@ -346,7 +350,7 @@ boolean integrate(T element, Downstream downstream) { * @throws IllegalArgumentException if {@code maxConcurrency} is less than 1 * @throws NullPointerException if {@code mapper} is {@code null} */ - public static Gatherer mapConcurrent( + public static Gatherer mapConcurrent( final int maxConcurrency, final Function mapper) { if (maxConcurrency < 1) diff --git a/src/java.base/share/classes/java/util/stream/Stream.java b/src/java.base/share/classes/java/util/stream/Stream.java index ef3b24077c9..fd3c0c4311c 100644 --- a/src/java.base/share/classes/java/util/stream/Stream.java +++ b/src/java.base/share/classes/java/util/stream/Stream.java @@ -1103,7 +1103,7 @@ default Stream dropWhile(Predicate predicate) { * @return the new stream * @since 24 */ - default Stream gather(Gatherer gatherer) { + default Stream gather(Gatherer gatherer) { return StreamSupport.stream(spliterator(), isParallel()) .gather(gatherer) .onClose(this::close);