|
| 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 | +import java.util.concurrent.SynchronousQueue; |
| 22 | + |
| 23 | +/** |
| 24 | + * A producer/consumer implementation of {@link AsyncStream} used by |
| 25 | + * {@code async} methods that contain {@code yield return} statements. |
| 26 | + * <p> |
| 27 | + * The producer (method body) runs on a separate thread and calls |
| 28 | + * {@link #yield(Object)} for each emitted element. The consumer |
| 29 | + * calls {@link #moveNext()}/{@link #getCurrent()} — typically via |
| 30 | + * a {@code for await} loop. |
| 31 | + * <p> |
| 32 | + * Uses a {@link SynchronousQueue} to provide natural back-pressure: |
| 33 | + * the producer thread blocks at each {@code yield return} until the |
| 34 | + * consumer has consumed the previous element (mirroring C#'s async |
| 35 | + * iterator suspension semantics). |
| 36 | + * |
| 37 | + * @param <T> the element type |
| 38 | + * @since 6.0.0 |
| 39 | + */ |
| 40 | +public class AsyncStreamGenerator<T> implements AsyncStream<T> { |
| 41 | + |
| 42 | + private static final Object DONE = new Object(); |
| 43 | + |
| 44 | + private final SynchronousQueue<Object> queue = new SynchronousQueue<>(); |
| 45 | + private T current; |
| 46 | + |
| 47 | + /** |
| 48 | + * Produces the next element. Called from the generator body when |
| 49 | + * a {@code yield return expr} statement is executed. Blocks until |
| 50 | + * the consumer is ready. |
| 51 | + */ |
| 52 | + public void yield(Object value) { |
| 53 | + try { |
| 54 | + queue.put(new Item(value)); |
| 55 | + } catch (InterruptedException e) { |
| 56 | + Thread.currentThread().interrupt(); |
| 57 | + throw new java.util.concurrent.CancellationException("Interrupted during yield"); |
| 58 | + } |
| 59 | + } |
| 60 | + |
| 61 | + /** |
| 62 | + * Signals that the generator has completed (no more elements). |
| 63 | + */ |
| 64 | + public void complete() { |
| 65 | + try { |
| 66 | + queue.put(DONE); |
| 67 | + } catch (InterruptedException e) { |
| 68 | + Thread.currentThread().interrupt(); |
| 69 | + } |
| 70 | + } |
| 71 | + |
| 72 | + /** |
| 73 | + * Signals that the generator failed with an exception. |
| 74 | + */ |
| 75 | + public void error(Throwable t) { |
| 76 | + try { |
| 77 | + queue.put(new ErrorItem(t)); |
| 78 | + } catch (InterruptedException e) { |
| 79 | + Thread.currentThread().interrupt(); |
| 80 | + } |
| 81 | + } |
| 82 | + |
| 83 | + @Override |
| 84 | + @SuppressWarnings("unchecked") |
| 85 | + public Awaitable<Boolean> moveNext() { |
| 86 | + try { |
| 87 | + Object next = queue.take(); |
| 88 | + if (next == DONE) { |
| 89 | + return Awaitable.of(false); |
| 90 | + } |
| 91 | + if (next instanceof ErrorItem ei) { |
| 92 | + Throwable cause = ei.error; |
| 93 | + if (cause instanceof Error err) throw err; |
| 94 | + throw sneakyThrow(cause); |
| 95 | + } |
| 96 | + current = (T) ((Item) next).value; |
| 97 | + return Awaitable.of(true); |
| 98 | + } catch (InterruptedException e) { |
| 99 | + Thread.currentThread().interrupt(); |
| 100 | + throw new java.util.concurrent.CancellationException("Interrupted during moveNext"); |
| 101 | + } |
| 102 | + } |
| 103 | + |
| 104 | + @Override |
| 105 | + public T getCurrent() { |
| 106 | + return current; |
| 107 | + } |
| 108 | + |
| 109 | + // Wrapper to handle null values in the queue |
| 110 | + private record Item(Object value) { } |
| 111 | + private record ErrorItem(Throwable error) { } |
| 112 | + |
| 113 | + @SuppressWarnings("unchecked") |
| 114 | + private static <T extends Throwable> RuntimeException sneakyThrow(Throwable t) throws T { |
| 115 | + throw (T) t; |
| 116 | + } |
| 117 | +} |
0 commit comments