1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389
|
/*
* Copyright (c) 2019, Oracle and/or its affiliates. All rights reserved.
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
*
* This code is free software; you can redistribute it and/or modify it
* under the terms of the GNU General Public License version 2 only, as
* published by the Free Software Foundation.
*
* This code is distributed in the hope that it will be useful, but WITHOUT
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
* version 2 for more details (a copy is included in the LICENSE file that
* accompanied this code).
*
* You should have received a copy of the GNU General Public License version
* 2 along with this work; if not, write to the Free Software Foundation,
* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
*
* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
* or visit www.oracle.com if you need additional information or have any
* questions.
*/
package org.reactivestreams;
import java.util.concurrent.Flow;
import static java.util.Objects.requireNonNull;
/**
* Bridge between Reactive Streams API and the Java 9 {@link java.util.concurrent.Flow} API.
*/
public final class FlowAdapters {
/** Utility class. */
private FlowAdapters() {
throw new IllegalStateException("No instances!");
}
/**
* Converts a Flow Publisher into a Reactive Streams Publisher.
* @param <T> the element type
* @param flowPublisher the source Flow Publisher to convert
* @return the equivalent Reactive Streams Publisher
*/
@SuppressWarnings("unchecked")
public static <T> org.reactivestreams.Publisher<T> toPublisher(
Flow.Publisher<? extends T> flowPublisher) {
requireNonNull(flowPublisher, "flowPublisher");
final org.reactivestreams.Publisher<T> publisher;
if (flowPublisher instanceof FlowPublisherFromReactive) {
publisher = (org.reactivestreams.Publisher<T>)(((FlowPublisherFromReactive<T>)flowPublisher).reactiveStreams);
} else if (flowPublisher instanceof org.reactivestreams.Publisher) {
publisher = (org.reactivestreams.Publisher<T>)flowPublisher;
} else {
publisher = new ReactivePublisherFromFlow<T>(flowPublisher);
}
return publisher;
}
/**
* Converts a Reactive Streams Publisher into a Flow Publisher.
* @param <T> the element type
* @param reactiveStreamsPublisher the source Reactive Streams Publisher to convert
* @return the equivalent Flow Publisher
*/
@SuppressWarnings("unchecked")
public static <T> Flow.Publisher<T> toFlowPublisher(
org.reactivestreams.Publisher<? extends T> reactiveStreamsPublisher
) {
requireNonNull(reactiveStreamsPublisher, "reactiveStreamsPublisher");
final Flow.Publisher<T> flowPublisher;
if (reactiveStreamsPublisher instanceof ReactivePublisherFromFlow) {
flowPublisher = (Flow.Publisher<T>)(((ReactivePublisherFromFlow<T>)reactiveStreamsPublisher).flow);
} else if (reactiveStreamsPublisher instanceof Flow.Publisher) {
flowPublisher = (Flow.Publisher<T>)reactiveStreamsPublisher;
} else {
flowPublisher = new FlowPublisherFromReactive<T>(reactiveStreamsPublisher);
}
return flowPublisher;
}
/**
* Converts a Flow Processor into a Reactive Streams Processor.
* @param <T> the input value type
* @param <U> the output value type
* @param flowProcessor the source Flow Processor to convert
* @return the equivalent Reactive Streams Processor
*/
@SuppressWarnings("unchecked")
public static <T, U> org.reactivestreams.Processor<T, U> toProcessor(
Flow.Processor<? super T, ? extends U> flowProcessor
) {
requireNonNull(flowProcessor, "flowProcessor");
final org.reactivestreams.Processor<T, U> processor;
if (flowProcessor instanceof FlowToReactiveProcessor) {
processor = (org.reactivestreams.Processor<T, U>)(((FlowToReactiveProcessor<T, U>)flowProcessor).reactiveStreams);
} else if (flowProcessor instanceof org.reactivestreams.Processor) {
processor = (org.reactivestreams.Processor<T, U>)flowProcessor;
} else {
processor = new ReactiveToFlowProcessor<T, U>(flowProcessor);
}
return processor;
}
/**
* Converts a Reactive Streams Processor into a Flow Processor.
* @param <T> the input value type
* @param <U> the output value type
* @param reactiveStreamsProcessor the source Reactive Streams Processor to convert
* @return the equivalent Flow Processor
*/
@SuppressWarnings("unchecked")
public static <T, U> Flow.Processor<T, U> toFlowProcessor(
org.reactivestreams.Processor<? super T, ? extends U> reactiveStreamsProcessor
) {
requireNonNull(reactiveStreamsProcessor, "reactiveStreamsProcessor");
final Flow.Processor<T, U> flowProcessor;
if (reactiveStreamsProcessor instanceof ReactiveToFlowProcessor) {
flowProcessor = (Flow.Processor<T, U>)(((ReactiveToFlowProcessor<T, U>)reactiveStreamsProcessor).flow);
} else if (reactiveStreamsProcessor instanceof Flow.Processor) {
flowProcessor = (Flow.Processor<T, U>)reactiveStreamsProcessor;
} else {
flowProcessor = new FlowToReactiveProcessor<T, U>(reactiveStreamsProcessor);
}
return flowProcessor;
}
/**
* Converts a Reactive Streams Subscriber into a Flow Subscriber.
* @param <T> the input and output value type
* @param reactiveStreamsSubscriber the Reactive Streams Subscriber instance to convert
* @return the equivalent Flow Subscriber
*/
@SuppressWarnings("unchecked")
public static <T> Flow.Subscriber<T> toFlowSubscriber(org.reactivestreams.Subscriber<T> reactiveStreamsSubscriber) {
requireNonNull(reactiveStreamsSubscriber, "reactiveStreamsSubscriber");
final Flow.Subscriber<T> flowSubscriber;
if (reactiveStreamsSubscriber instanceof ReactiveToFlowSubscriber) {
flowSubscriber = (Flow.Subscriber<T>)((ReactiveToFlowSubscriber<T>)reactiveStreamsSubscriber).flow;
} else if (reactiveStreamsSubscriber instanceof Flow.Subscriber) {
flowSubscriber = (Flow.Subscriber<T>)reactiveStreamsSubscriber;
} else {
flowSubscriber = new FlowToReactiveSubscriber<T>(reactiveStreamsSubscriber);
}
return flowSubscriber;
}
/**
* Converts a Flow Subscriber into a Reactive Streams Subscriber.
* @param <T> the input and output value type
* @param flowSubscriber the Flow Subscriber instance to convert
* @return the equivalent Reactive Streams Subscriber
*/
@SuppressWarnings("unchecked")
public static <T> org.reactivestreams.Subscriber<T> toSubscriber(Flow.Subscriber<T> flowSubscriber) {
requireNonNull(flowSubscriber, "flowSubscriber");
final org.reactivestreams.Subscriber<T> subscriber;
if (flowSubscriber instanceof FlowToReactiveSubscriber) {
subscriber = (org.reactivestreams.Subscriber<T>)((FlowToReactiveSubscriber<T>)flowSubscriber).reactiveStreams;
} else if (flowSubscriber instanceof org.reactivestreams.Subscriber) {
subscriber = (org.reactivestreams.Subscriber<T>)flowSubscriber;
} else {
subscriber = new ReactiveToFlowSubscriber<T>(flowSubscriber);
}
return subscriber;
}
/**
* Wraps a Reactive Streams Subscription and converts the calls to a Flow Subscription.
*/
static final class FlowToReactiveSubscription implements Flow.Subscription {
final org.reactivestreams.Subscription reactiveStreams;
public FlowToReactiveSubscription(org.reactivestreams.Subscription reactive) {
this.reactiveStreams = reactive;
}
@Override
public void request(long n) {
reactiveStreams.request(n);
}
@Override
public void cancel() {
reactiveStreams.cancel();
}
}
/**
* Wraps a Flow Subscription and converts the calls to a Reactive Streams Subscription.
*/
static final class ReactiveToFlowSubscription implements org.reactivestreams.Subscription {
final Flow.Subscription flow;
public ReactiveToFlowSubscription(Flow.Subscription flow) {
this.flow = flow;
}
@Override
public void request(long n) {
flow.request(n);
}
@Override
public void cancel() {
flow.cancel();
}
}
/**
* Wraps a Reactive Streams Subscriber and forwards methods of the Flow Subscriber to it.
* @param <T> the element type
*/
static final class FlowToReactiveSubscriber<T> implements Flow.Subscriber<T> {
final org.reactivestreams.Subscriber<? super T> reactiveStreams;
public FlowToReactiveSubscriber(org.reactivestreams.Subscriber<? super T> reactive) {
this.reactiveStreams = reactive;
}
@Override
public void onSubscribe(Flow.Subscription subscription) {
reactiveStreams.onSubscribe((subscription == null) ? null : new ReactiveToFlowSubscription(subscription));
}
@Override
public void onNext(T item) {
reactiveStreams.onNext(item);
}
@Override
public void onError(Throwable throwable) {
reactiveStreams.onError(throwable);
}
@Override
public void onComplete() {
reactiveStreams.onComplete();
}
}
/**
* Wraps a Flow Subscriber and forwards methods of the Reactive Streams Subscriber to it.
* @param <T> the element type
*/
static final class ReactiveToFlowSubscriber<T> implements org.reactivestreams.Subscriber<T> {
final Flow.Subscriber<? super T> flow;
public ReactiveToFlowSubscriber(Flow.Subscriber<? super T> flow) {
this.flow = flow;
}
@Override
public void onSubscribe(org.reactivestreams.Subscription subscription) {
flow.onSubscribe((subscription == null) ? null : new FlowToReactiveSubscription(subscription));
}
@Override
public void onNext(T item) {
flow.onNext(item);
}
@Override
public void onError(Throwable throwable) {
flow.onError(throwable);
}
@Override
public void onComplete() {
flow.onComplete();
}
}
/**
* Wraps a Flow Processor and forwards methods of the Reactive Streams Processor to it.
* @param <T> the input type
* @param <U> the output type
*/
static final class ReactiveToFlowProcessor<T, U> implements org.reactivestreams.Processor<T, U> {
final Flow.Processor<? super T, ? extends U> flow;
public ReactiveToFlowProcessor(Flow.Processor<? super T, ? extends U> flow) {
this.flow = flow;
}
@Override
public void onSubscribe(org.reactivestreams.Subscription subscription) {
flow.onSubscribe((subscription == null) ? null : new FlowToReactiveSubscription(subscription));
}
@Override
public void onNext(T t) {
flow.onNext(t);
}
@Override
public void onError(Throwable t) {
flow.onError(t);
}
@Override
public void onComplete() {
flow.onComplete();
}
@Override
public void subscribe(org.reactivestreams.Subscriber<? super U> s) {
flow.subscribe((s == null) ? null : new FlowToReactiveSubscriber<U>(s));
}
}
/**
* Wraps a Reactive Streams Processor and forwards methods of the Flow Processor to it.
* @param <T> the input type
* @param <U> the output type
*/
static final class FlowToReactiveProcessor<T, U> implements Flow.Processor<T, U> {
final org.reactivestreams.Processor<? super T, ? extends U> reactiveStreams;
public FlowToReactiveProcessor(org.reactivestreams.Processor<? super T, ? extends U> reactive) {
this.reactiveStreams = reactive;
}
@Override
public void onSubscribe(Flow.Subscription subscription) {
reactiveStreams.onSubscribe((subscription == null) ? null : new ReactiveToFlowSubscription(subscription));
}
@Override
public void onNext(T t) {
reactiveStreams.onNext(t);
}
@Override
public void onError(Throwable t) {
reactiveStreams.onError(t);
}
@Override
public void onComplete() {
reactiveStreams.onComplete();
}
@Override
public void subscribe(Flow.Subscriber<? super U> s) {
reactiveStreams.subscribe((s == null) ? null : new ReactiveToFlowSubscriber<U>(s));
}
}
/**
* Reactive Streams Publisher that wraps a Flow Publisher.
* @param <T> the element type
*/
static final class ReactivePublisherFromFlow<T> implements org.reactivestreams.Publisher<T> {
final Flow.Publisher<? extends T> flow;
public ReactivePublisherFromFlow(Flow.Publisher<? extends T> flowPublisher) {
this.flow = flowPublisher;
}
@Override
public void subscribe(org.reactivestreams.Subscriber<? super T> reactive) {
flow.subscribe((reactive == null) ? null : new FlowToReactiveSubscriber<T>(reactive));
}
}
/**
* Flow Publisher that wraps a Reactive Streams Publisher.
* @param <T> the element type
*/
static final class FlowPublisherFromReactive<T> implements Flow.Publisher<T> {
final org.reactivestreams.Publisher<? extends T> reactiveStreams;
public FlowPublisherFromReactive(org.reactivestreams.Publisher<? extends T> reactivePublisher) {
this.reactiveStreams = reactivePublisher;
}
@Override
public void subscribe(Flow.Subscriber<? super T> flow) {
reactiveStreams.subscribe((flow == null) ? null : new ReactiveToFlowSubscriber<T>(flow));
}
}
}
|