137 * final Function<? super S, ? extends T> function;
138 * Flow.Subscription subscription;
139 * TransformProcessor(Executor executor, int maxBufferCapacity,
140 * Function<? super S, ? extends T> function) {
141 * super(executor, maxBufferCapacity);
142 * this.function = function;
143 * }
144 * public void onSubscribe(Flow.Subscription subscription) {
145 * (this.subscription = subscription).request(1);
146 * }
147 * public void onNext(S item) {
148 * subscription.request(1);
149 * submit(function.apply(item));
150 * }
151 * public void onError(Throwable ex) { closeExceptionally(ex); }
152 * public void onComplete() { close(); }
153 * }}</pre>
154 *
155 * @param <T> the published item type
156 * @author Doug Lea
157 * @since 1.9
158 */
159 public class SubmissionPublisher<T> implements Flow.Publisher<T>,
160 AutoCloseable {
161 /*
162 * Most mechanics are handled by BufferedSubscription. This class
163 * mainly tracks subscribers and ensures sequentiality, by using
164 * built-in synchronization locks across public methods. (Using
165 * built-in locks works well in the most typical case in which
166 * only one thread submits items).
167 */
168
169 /** The largest possible power of two array size. */
170 static final int BUFFER_CAPACITY_LIMIT = 1 << 30;
171
172 /** Round capacity to power of 2, at most limit. */
173 static final int roundCapacity(int cap) {
174 int n = cap - 1;
175 n |= n >>> 1;
176 n |= n >>> 2;
177 n |= n >>> 4;
|
137 * final Function<? super S, ? extends T> function;
138 * Flow.Subscription subscription;
139 * TransformProcessor(Executor executor, int maxBufferCapacity,
140 * Function<? super S, ? extends T> function) {
141 * super(executor, maxBufferCapacity);
142 * this.function = function;
143 * }
144 * public void onSubscribe(Flow.Subscription subscription) {
145 * (this.subscription = subscription).request(1);
146 * }
147 * public void onNext(S item) {
148 * subscription.request(1);
149 * submit(function.apply(item));
150 * }
151 * public void onError(Throwable ex) { closeExceptionally(ex); }
152 * public void onComplete() { close(); }
153 * }}</pre>
154 *
155 * @param <T> the published item type
156 * @author Doug Lea
157 * @since 9
158 */
159 public class SubmissionPublisher<T> implements Flow.Publisher<T>,
160 AutoCloseable {
161 /*
162 * Most mechanics are handled by BufferedSubscription. This class
163 * mainly tracks subscribers and ensures sequentiality, by using
164 * built-in synchronization locks across public methods. (Using
165 * built-in locks works well in the most typical case in which
166 * only one thread submits items).
167 */
168
169 /** The largest possible power of two array size. */
170 static final int BUFFER_CAPACITY_LIMIT = 1 << 30;
171
172 /** Round capacity to power of 2, at most limit. */
173 static final int roundCapacity(int cap) {
174 int n = cap - 1;
175 n |= n >>> 1;
176 n |= n >>> 2;
177 n |= n >>> 4;
|