Debian Codegraph @

src/main/java/io/reactivex/rxjava3/internal/operators/observable/BlockingObservableIterable.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.atomic.AtomicReference;
18 import java.util.concurrent.locks.*;
19
20 import io.reactivex.rxjava3.core.ObservableSource;
21 import io.reactivex.rxjava3.disposables.Disposable;
22 import io.reactivex.rxjava3.internal.disposables.DisposableHelper;
23 import io.reactivex.rxjava3.internal.util.*;
24 import io.reactivex.rxjava3.operators.SpscLinkedArrayQueue;
25
26 public final class BlockingObservableIterable<T> implements Iterable<T> {
27 final ObservableSource<? extends T> source;
28
29 final int bufferSize;
30
31 public BlockingObservableIterable(ObservableSource<? extends T> source, int bufferSize) {
32 this.source = source;
33 this.bufferSize = bufferSize;
34 }
35
36 @Override
37 public Iterator<T> iterator() {
38 BlockingObservableIterator<T> it = new BlockingObservableIterator<>(bufferSize);
39 source.subscribe(it);
40 return it;
41 }
42
43 static final class BlockingObservableIterator<T>
44 extends AtomicReference<Disposable>
45 implements io.reactivex.rxjava3.core.Observer<T>, Iterator<T>, Disposable {
46
47 private static final long serialVersionUID = 6695226475494099826L;
48
49 final SpscLinkedArrayQueue<T> queue;
50
51 final Lock lock;
52
53 final Condition condition;
54
55 volatile boolean done;
56 volatile Throwable error;
57
58 BlockingObservableIterator(int batchSize) {
59 this.queue = new SpscLinkedArrayQueue<>(batchSize);
60 this.lock = new ReentrantLock();
61 this.condition = lock.newCondition();
62 }
63
64 @Override
65 public boolean hasNext() {
66 for (;;) {
67 if (isDisposed()) {
68 Throwable e = error;
69 if (e != null) {
70 throw ExceptionHelper.wrapOrThrow(e);
71 }
72 return false;
73 }
74 boolean d = done;
75 boolean empty = queue.isEmpty();
76 if (d) {
77 Throwable e = error;
78 if (e != null) {
79 throw ExceptionHelper.wrapOrThrow(e);
80 } else
81 if (empty) {
82 return false;
83 }
84 }
85 if (empty) {
86 try {
87 BlockingHelper.verifyNonBlocking();
88 lock.lock();
89 try {
90 while (!done && queue.isEmpty() && !isDisposed()) {
91 condition.await();
92 }
93 } finally {
94 lock.unlock();
95 }
96 } catch (InterruptedException ex) {
97 DisposableHelper.dispose(this);
98 signalConsumer();
99 throw ExceptionHelper.wrapOrThrow(ex);
100 }
101 } else {
102 return true;
103 }
104 }
105 }
106
107 @Override
108 public T next() {
109 if (hasNext()) {
110 return queue.poll();
111 }
112 throw new NoSuchElementException();
113 }
114
115 @Override
116 public void onSubscribe(Disposable d) {
117 DisposableHelper.setOnce(this, d);
118 }
119
120 @Override
121 public void onNext(T t) {
122 queue.offer(t);
123 signalConsumer();
124 }
125
126 @Override
127 public void onError(Throwable t) {
128 error = t;
129 done = true;
130 signalConsumer();
131 }
132
133 @Override
134 public void onComplete() {
135 done = true;
136 signalConsumer();
137 }
138
139 void signalConsumer() {
140 lock.lock();
141 try {
142 condition.signalAll();
143 } finally {
144 lock.unlock();
145 }
146 }
147
148 @Override // otherwise default method which isn't available in Java 7
149 public void remove() {
150 throw new UnsupportedOperationException("remove");
151 }
152
153 @Override
154 public void dispose() {
155 DisposableHelper.dispose(this);
156 signalConsumer(); // Just in case it is currently blocking in hasNext.
157 }
158
159 @Override
160 public boolean isDisposed() {
161 return DisposableHelper.isDisposed(get());
162 }
163 }
164 }