| 1 |
/* |
| 2 |
* Copyright (c) 2016-present, RxJava Contributors. |
| 3 |
* |
| 4 |
* Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in |
| 5 |
* compliance with the License. You may obtain a copy of the License at |
| 6 |
* |
| 7 |
* http://www.apache.org/licenses/LICENSE-2.0 |
| 8 |
* |
| 9 |
* Unless required by applicable law or agreed to in writing, software distributed under the License is |
| 10 |
* distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See |
| 11 |
* the License for the specific language governing permissions and limitations under the License. |
| 12 |
*/ |
| 13 |
|
| 14 |
package io.reactivex.rxjava3.internal.operators.flowable; |
| 15 |
|
| 16 |
import java.util.*; |
| 17 |
import java.util.concurrent.*; |
| 18 |
import java.util.concurrent.atomic.AtomicInteger; |
| 19 |
|
| 20 |
import org.reactivestreams.Publisher; |
| 21 |
|
| 22 |
import io.reactivex.rxjava3.core.*; |
| 23 |
import io.reactivex.rxjava3.internal.util.*; |
| 24 |
import io.reactivex.rxjava3.plugins.RxJavaPlugins; |
| 25 |
import io.reactivex.rxjava3.subscribers.DisposableSubscriber; |
| 26 |
|
| 27 |
/** |
| 28 |
* Returns an Iterable that blocks until the Observable emits another item, then returns that item. |
| 29 |
* <p> |
| 30 |
* <img width="640" height="490" src="https://github.com/ReactiveX/RxJava/wiki/images/rx-operators/B.next.v3.png" alt=""> |
| 31 |
* |
| 32 |
* @param <T> the value type |
| 33 |
*/ |
| 34 |
public final class BlockingFlowableNext<T> implements Iterable<T> { |
| 35 |
|
| 36 |
final Publisher<? extends T> source; |
| 37 |
|
| 38 |
public BlockingFlowableNext(Publisher<? extends T> source) { |
| 39 |
this.source = source; |
| 40 |
} |
| 41 |
|
| 42 |
@Override |
| 43 |
public Iterator<T> iterator() { |
| 44 |
NextSubscriber<T> nextSubscriber = new NextSubscriber<>(); |
| 45 |
return new NextIterator<>(source, nextSubscriber); |
| 46 |
} |
| 47 |
|
| 48 |
// test needs to access the observer.waiting flag |
| 49 |
static final class NextIterator<T> implements Iterator<T> { |
| 50 |
|
| 51 |
private final NextSubscriber<T> subscriber; |
| 52 |
private final Publisher<? extends T> items; |
| 53 |
private T next; |
| 54 |
private boolean hasNext = true; |
| 55 |
private boolean isNextConsumed = true; |
| 56 |
private Throwable error; |
| 57 |
private boolean started; |
| 58 |
|
| 59 |
NextIterator(Publisher<? extends T> items, NextSubscriber<T> subscriber) { |
| 60 |
this.items = items; |
| 61 |
this.subscriber = subscriber; |
| 62 |
} |
| 63 |
|
| 64 |
@Override |
| 65 |
public boolean hasNext() { |
| 66 |
if (error != null) { |
| 67 |
// If any error has already been thrown, throw it again. |
| 68 |
throw ExceptionHelper.wrapOrThrow(error); |
| 69 |
} |
| 70 |
// Since an iterator should not be used in different thread, |
| 71 |
// so we do not need any synchronization. |
| 72 |
if (!hasNext) { |
| 73 |
// the iterator has reached the end. |
| 74 |
return false; |
| 75 |
} |
| 76 |
// next has not been used yet. |
| 77 |
return !isNextConsumed || moveToNext(); |
| 78 |
} |
| 79 |
|
| 80 |
private boolean moveToNext() { |
| 81 |
try { |
| 82 |
if (!started) { |
| 83 |
started = true; |
| 84 |
// if not started, start now |
| 85 |
subscriber.setWaiting(); |
| 86 |
Flowable.<T>fromPublisher(items) |
| 87 |
.materialize().subscribe(subscriber); |
| 88 |
} |
| 89 |
|
| 90 |
Notification<T> nextNotification = subscriber.takeNext(); |
| 91 |
if (nextNotification.isOnNext()) { |
| 92 |
isNextConsumed = false; |
| 93 |
next = nextNotification.getValue(); |
| 94 |
return true; |
| 95 |
} |
| 96 |
// If an observable is completed or fails, |
| 97 |
// hasNext() always return false. |
| 98 |
hasNext = false; |
| 99 |
if (nextNotification.isOnComplete()) { |
| 100 |
return false; |
| 101 |
} |
| 102 |
error = nextNotification.getError(); |
| 103 |
throw ExceptionHelper.wrapOrThrow(error); |
| 104 |
} catch (InterruptedException e) { |
| 105 |
subscriber.dispose(); |
| 106 |
error = e; |
| 107 |
throw ExceptionHelper.wrapOrThrow(e); |
| 108 |
} |
| 109 |
} |
| 110 |
|
| 111 |
@Override |
| 112 |
public T next() { |
| 113 |
if (error != null) { |
| 114 |
// If any error has already been thrown, throw it again. |
| 115 |
throw ExceptionHelper.wrapOrThrow(error); |
| 116 |
} |
| 117 |
if (hasNext()) { |
| 118 |
isNextConsumed = true; |
| 119 |
return next; |
| 120 |
} |
| 121 |
else { |
| 122 |
throw new NoSuchElementException("No more elements"); |
| 123 |
} |
| 124 |
} |
| 125 |
|
| 126 |
@Override |
| 127 |
public void remove() { |
| 128 |
throw new UnsupportedOperationException("Read only iterator"); |
| 129 |
} |
| 130 |
} |
| 131 |
|
| 132 |
static final class NextSubscriber<T> extends DisposableSubscriber<Notification<T>> { |
| 133 |
private final BlockingQueue<Notification<T>> buf = new ArrayBlockingQueue<>(1); |
| 134 |
final AtomicInteger waiting = new AtomicInteger(); |
| 135 |
|
| 136 |
@Override |
| 137 |
public void onComplete() { |
| 138 |
// ignore |
| 139 |
} |
| 140 |
|
| 141 |
@Override |
| 142 |
public void onError(Throwable e) { |
| 143 |
RxJavaPlugins.onError(e); |
| 144 |
} |
| 145 |
|
| 146 |
@Override |
| 147 |
public void onNext(Notification<T> args) { |
| 148 |
|
| 149 |
if (waiting.getAndSet(0) == 1 || !args.isOnNext()) { |
| 150 |
Notification<T> toOffer = args; |
| 151 |
while (!buf.offer(toOffer)) { |
| 152 |
Notification<T> concurrentItem = buf.poll(); |
| 153 |
|
| 154 |
// in case if we won race condition with onComplete/onError method |
| 155 |
if (concurrentItem != null && !concurrentItem.isOnNext()) { |
| 156 |
toOffer = concurrentItem; |
| 157 |
} |
| 158 |
} |
| 159 |
} |
| 160 |
|
| 161 |
} |
| 162 |
|
| 163 |
public Notification<T> takeNext() throws InterruptedException { |
| 164 |
setWaiting(); |
| 165 |
BlockingHelper.verifyNonBlocking(); |
| 166 |
return buf.take(); |
| 167 |
} |
| 168 |
void setWaiting() { |
| 169 |
waiting.set(1); |
| 170 |
} |
| 171 |
} |
| 172 |
} |