Debian Codegraph @

src/main/java/io/reactivex/rxjava3/internal/operators/observable/BlockingObservableLatest.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.Semaphore;
18 import java.util.concurrent.atomic.AtomicReference;
19
20 import io.reactivex.rxjava3.core.*;
21 import io.reactivex.rxjava3.core.Observable;
22 import io.reactivex.rxjava3.internal.util.*;
23 import io.reactivex.rxjava3.observers.DisposableObserver;
24 import io.reactivex.rxjava3.plugins.RxJavaPlugins;
25
26 /**
27 * Wait for and iterate over the latest values of the source observable. If the source works faster than the
28 * iterator, values may be skipped, but not the {@code onError} or {@code onComplete} events.
29 * @param <T> the value type
30 */
31 public final class BlockingObservableLatest<T> implements Iterable<T> {
32
33 final ObservableSource<T> source;
34
35 public BlockingObservableLatest(ObservableSource<T> source) {
36 this.source = source;
37 }
38
39 @Override
40 public Iterator<T> iterator() {
41 BlockingObservableLatestIterator<T> lio = new BlockingObservableLatestIterator<>();
42
43 Observable<Notification<T>> materialized = Observable.wrap(source).materialize();
44
45 materialized.subscribe(lio);
46 return lio;
47 }
48
49 static final class BlockingObservableLatestIterator<T> extends DisposableObserver<Notification<T>> implements Iterator<T> {
50 // iterator's notification
51 Notification<T> iteratorNotification;
52
53 final Semaphore notify = new Semaphore(0);
54 // observer's notification
55 final AtomicReference<Notification<T>> value = new AtomicReference<>();
56
57 @Override
58 public void onNext(Notification<T> args) {
59 boolean wasNotAvailable = value.getAndSet(args) == null;
60 if (wasNotAvailable) {
61 notify.release();
62 }
63 }
64
65 @Override
66 public void onError(Throwable e) {
67 RxJavaPlugins.onError(e);
68 }
69
70 @Override
71 public void onComplete() {
72 // not expected
73 }
74
75 @Override
76 public boolean hasNext() {
77 if (iteratorNotification != null && iteratorNotification.isOnError()) {
78 throw ExceptionHelper.wrapOrThrow(iteratorNotification.getError());
79 }
80 if (iteratorNotification == null) {
81 try {
82 BlockingHelper.verifyNonBlocking();
83 notify.acquire();
84 } catch (InterruptedException ex) {
85 dispose();
86 iteratorNotification = Notification.createOnError(ex);
87 throw ExceptionHelper.wrapOrThrow(ex);
88 }
89
90 Notification<T> n = value.getAndSet(null);
91 iteratorNotification = n;
92 if (n.isOnError()) {
93 throw ExceptionHelper.wrapOrThrow(n.getError());
94 }
95 }
96 return iteratorNotification.isOnNext();
97 }
98
99 @Override
100 public T next() {
101 if (hasNext()) {
102 T v = iteratorNotification.getValue();
103 iteratorNotification = null;
104 return v;
105 }
106 throw new NoSuchElementException();
107 }
108
109 @Override
110 public void remove() {
111 throw new UnsupportedOperationException("Read-only iterator.");
112 }
113 }
114 }