|
| 1 | +/* |
| 2 | + * Licensed to the Apache Software Foundation (ASF) under one |
| 3 | + * or more contributor license agreements. See the NOTICE file |
| 4 | + * distributed with this work for additional information |
| 5 | + * regarding copyright ownership. The ASF licenses this file |
| 6 | + * to you under the Apache License, Version 2.0 (the |
| 7 | + * "License"); you may not use this file except in compliance |
| 8 | + * with the License. You may obtain a copy of the License at |
| 9 | + * |
| 10 | + * http://www.apache.org/licenses/LICENSE-2.0 |
| 11 | + * |
| 12 | + * Unless required by applicable law or agreed to in writing, |
| 13 | + * software distributed under the License is distributed on an |
| 14 | + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY |
| 15 | + * KIND, either express or implied. See the License for the |
| 16 | + * specific language governing permissions and limitations |
| 17 | + * under the License. |
| 18 | + */ |
| 19 | +package groovy.concurrent; |
| 20 | + |
| 21 | +/** |
| 22 | + * Asynchronous iteration abstraction, analogous to C#'s |
| 23 | + * {@code IAsyncEnumerable<T>} or JavaScript's async iterables. |
| 24 | + * <p> |
| 25 | + * Used with the {@code for await} syntax: |
| 26 | + * <pre> |
| 27 | + * for await (item in asyncStream) { |
| 28 | + * process(item) |
| 29 | + * } |
| 30 | + * </pre> |
| 31 | + * <p> |
| 32 | + * An {@code AsyncStream} can be produced in several ways: |
| 33 | + * <ul> |
| 34 | + * <li>Using {@code yield return} inside an {@code async} method or closure |
| 35 | + * to create a generator-style stream</li> |
| 36 | + * <li>Adapting JDK {@link java.util.concurrent.Flow.Publisher} instances |
| 37 | + * (supported out of the box by the built-in adapter)</li> |
| 38 | + * <li>Adapting third-party reactive types (Reactor {@code Flux}, RxJava |
| 39 | + * {@code Observable}) via {@link AwaitableAdapter}</li> |
| 40 | + * </ul> |
| 41 | + * |
| 42 | + * @param <T> the element type |
| 43 | + * @see AwaitableAdapter |
| 44 | + * @see AwaitableAdapterRegistry |
| 45 | + * @since 6.0.0 |
| 46 | + */ |
| 47 | +public interface AsyncStream<T> extends AutoCloseable { |
| 48 | + |
| 49 | + /** |
| 50 | + * Asynchronously advances to the next element. Returns an {@link Awaitable} |
| 51 | + * that completes with {@code true} if an element is available, or |
| 52 | + * {@code false} if the stream is exhausted. |
| 53 | + */ |
| 54 | + Awaitable<Boolean> moveNext(); |
| 55 | + |
| 56 | + /** |
| 57 | + * Returns the current element. Must only be called after {@link #moveNext()} |
| 58 | + * has completed with {@code true}. |
| 59 | + */ |
| 60 | + T getCurrent(); |
| 61 | + |
| 62 | + /** |
| 63 | + * Closes the stream and releases any associated resources. |
| 64 | + * <p> |
| 65 | + * The default implementation is a no-op. Implementations that bridge to |
| 66 | + * generators, publishers, or other resource-owning sources may override |
| 67 | + * this to propagate cancellation upstream. Compiler-generated |
| 68 | + * {@code for await} loops invoke {@code close()} automatically from a |
| 69 | + * {@code finally} block, including on early {@code break}, {@code return}, |
| 70 | + * and exceptional exit. |
| 71 | + * |
| 72 | + * @since 6.0.0 |
| 73 | + */ |
| 74 | + @Override |
| 75 | + default void close() { |
| 76 | + } |
| 77 | + |
| 78 | + /** |
| 79 | + * Converts the given source to an {@code AsyncStream}. |
| 80 | + * <p> |
| 81 | + * If the source is already an {@code AsyncStream}, it is returned as-is. |
| 82 | + * Otherwise, the {@link AwaitableAdapterRegistry} is consulted to find a |
| 83 | + * suitable adapter. Built-in adapters handle {@link Iterable} and |
| 84 | + * {@link java.util.Iterator}; the auto-discovered {@code FlowPublisherAdapter} |
| 85 | + * handles {@link java.util.concurrent.Flow.Publisher}; third-party frameworks |
| 86 | + * can register additional adapters via the registry. |
| 87 | + * <p> |
| 88 | + * This is the recommended entry point for converting external collection or |
| 89 | + * reactive types to {@code AsyncStream}: |
| 90 | + * <pre> |
| 91 | + * AsyncStream<String> stream = AsyncStream.from(myList) |
| 92 | + * AsyncStream<Integer> stream2 = AsyncStream.from(myFlowPublisher) |
| 93 | + * </pre> |
| 94 | + * |
| 95 | + * @param source the source object; must not be {@code null} |
| 96 | + * @param <T> the element type |
| 97 | + * @return an async stream backed by the source |
| 98 | + * @throws IllegalArgumentException if {@code source} is {@code null} |
| 99 | + * or no adapter supports the source type |
| 100 | + * @see AwaitableAdapterRegistry#toAsyncStream(Object) |
| 101 | + * @since 6.0.0 |
| 102 | + */ |
| 103 | + @SuppressWarnings("unchecked") |
| 104 | + static <T> AsyncStream<T> from(Object source) { |
| 105 | + return AwaitableAdapterRegistry.toAsyncStream(source); |
| 106 | + } |
| 107 | + |
| 108 | + /** |
| 109 | + * Returns an empty {@code AsyncStream} that completes immediately. |
| 110 | + */ |
| 111 | + @SuppressWarnings("unchecked") |
| 112 | + static <T> AsyncStream<T> empty() { |
| 113 | + return (AsyncStream<T>) EMPTY; |
| 114 | + } |
| 115 | + |
| 116 | + /** |
| 117 | + * Cached awaitable for {@code moveNext()} returning {@code true}. |
| 118 | + * Eliminates per-call allocation on the hot path. Shared by all |
| 119 | + * {@code AsyncStream} implementations (e.g. via |
| 120 | + * {@link org.apache.groovy.runtime.async.AbstractAsyncStream AbstractAsyncStream}). |
| 121 | + */ |
| 122 | + Awaitable<Boolean> MOVE_NEXT_TRUE = Awaitable.of(Boolean.TRUE); |
| 123 | + |
| 124 | + /** |
| 125 | + * Cached awaitable for {@code moveNext()} returning {@code false}. |
| 126 | + * Eliminates per-call allocation on the stream-end path. |
| 127 | + */ |
| 128 | + Awaitable<Boolean> MOVE_NEXT_FALSE = Awaitable.of(Boolean.FALSE); |
| 129 | + |
| 130 | + /** |
| 131 | + * Singleton empty stream instance. |
| 132 | + * <p> |
| 133 | + * This is an implementation detail backing {@link #empty()}. |
| 134 | + * User code should call {@code AsyncStream.empty()} rather than |
| 135 | + * referencing this field directly. |
| 136 | + */ |
| 137 | + AsyncStream<Object> EMPTY = new AsyncStream<>() { |
| 138 | + @Override public Awaitable<Boolean> moveNext() { return MOVE_NEXT_FALSE; } |
| 139 | + @Override public Object getCurrent() { return null; } |
| 140 | + }; |
| 141 | +} |
0 commit comments