Debian Codegraph @

src/main/java/io/reactivex/rxjava3/internal/operators/observable/BlockingObservableNext.java

raw
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.observable;
15
16 import java.util.*;
17 import java.util.concurrent.*;
18 import java.util.concurrent.atomic.AtomicInteger;
19
20 import io.reactivex.rxjava3.core.*;
21 import io.reactivex.rxjava3.internal.util.*;
22 import io.reactivex.rxjava3.observers.DisposableObserver;
23 import io.reactivex.rxjava3.plugins.RxJavaPlugins;
24
25 /**
26 * Returns an Iterable that blocks until the Observable emits another item, then returns that item.
27 * <p>
28 * <img width="640" height="490" src="https://github.com/ReactiveX/RxJava/wiki/images/rx-operators/B.next.v3.png" alt="">
29 *
30 * @param <T> the value type
31 */
32 public final class BlockingObservableNext<T> implements Iterable<T> {
33
34 final ObservableSource<T> source;
35
36 public BlockingObservableNext(ObservableSource<T> source) {
37 this.source = source;
38 }
39
40 @Override
41 public Iterator<T> iterator() {
42 NextObserver<T> nextObserver = new NextObserver<>();
43 return new NextIterator<>(source, nextObserver);
44 }
45
46 // test needs to access the observer.waiting flag
47 static final class NextIterator<T> implements Iterator<T> {
48
49 private final NextObserver<T> observer;
50 private final ObservableSource<T> items;
51 private T next;
52 private boolean hasNext = true;
53 private boolean isNextConsumed = true;
54 private Throwable error;
55 private boolean started;
56
57 NextIterator(ObservableSource<T> items, NextObserver<T> observer) {
58 this.items = items;
59 this.observer = observer;
60 }
61
62 @Override
63 public boolean hasNext() {
64 if (error != null) {
65 // If any error has already been thrown, throw it again.
66 throw ExceptionHelper.wrapOrThrow(error);
67 }
68 // Since an iterator should not be used in different thread,
69 // so we do not need any synchronization.
70 if (!hasNext) {
71 // the iterator has reached the end.
72 return false;
73 }
74 // next has not been used yet.
75 return !isNextConsumed || moveToNext();
76 }
77
78 private boolean moveToNext() {
79 if (!started) {
80 started = true;
81 // if not started, start now
82 observer.setWaiting();
83 new ObservableMaterialize<>(items).subscribe(observer);
84 }
85
86 Notification<T> nextNotification;
87
88 try {
89 nextNotification = observer.takeNext();
90 } catch (InterruptedException e) {
91 observer.dispose();
92 error = e;
93 throw ExceptionHelper.wrapOrThrow(e);
94 }
95
96 if (nextNotification.isOnNext()) {
97 isNextConsumed = false;
98 next = nextNotification.getValue();
99 return true;
100 }
101 // If an observable is completed or fails,
102 // hasNext() always return false.
103 hasNext = false;
104 if (nextNotification.isOnComplete()) {
105 return false;
106 }
107 error = nextNotification.getError();
108 throw ExceptionHelper.wrapOrThrow(error);
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 NextObserver<T> extends DisposableObserver<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 }