| 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.Semaphore; |
| 18 |
import java.util.concurrent.atomic.AtomicReference; |
| 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 |
* Wait for and iterate over the latest values of the source observable. If the source works faster than the |
| 29 |
* iterator, values may be skipped, but not the {@code onError} or {@code onComplete} events. |
| 30 |
* @param <T> the value type emitted |
| 31 |
*/ |
| 32 |
public final class BlockingFlowableLatest<T> implements Iterable<T> { |
| 33 |
|
| 34 |
final Publisher<? extends T> source; |
| 35 |
|
| 36 |
public BlockingFlowableLatest(Publisher<? extends T> source) { |
| 37 |
this.source = source; |
| 38 |
} |
| 39 |
|
| 40 |
@Override |
| 41 |
public Iterator<T> iterator() { |
| 42 |
LatestSubscriberIterator<T> lio = new LatestSubscriberIterator<>(); |
| 43 |
Flowable.<T>fromPublisher(source).materialize().subscribe(lio); |
| 44 |
return lio; |
| 45 |
} |
| 46 |
|
| 47 |
/** Subscriber of source, iterator for output. */ |
| 48 |
static final class LatestSubscriberIterator<T> extends DisposableSubscriber<Notification<T>> implements Iterator<T> { |
| 49 |
final Semaphore notify = new Semaphore(0); |
| 50 |
// observer's notification |
| 51 |
final AtomicReference<Notification<T>> value = new AtomicReference<>(); |
| 52 |
|
| 53 |
// iterator's notification |
| 54 |
Notification<T> iteratorNotification; |
| 55 |
|
| 56 |
@Override |
| 57 |
public void onNext(Notification<T> args) { |
| 58 |
boolean wasNotAvailable = value.getAndSet(args) == null; |
| 59 |
if (wasNotAvailable) { |
| 60 |
notify.release(); |
| 61 |
} |
| 62 |
} |
| 63 |
|
| 64 |
@Override |
| 65 |
public void onError(Throwable e) { |
| 66 |
RxJavaPlugins.onError(e); |
| 67 |
} |
| 68 |
|
| 69 |
@Override |
| 70 |
public void onComplete() { |
| 71 |
// not expected |
| 72 |
} |
| 73 |
|
| 74 |
@Override |
| 75 |
public boolean hasNext() { |
| 76 |
if (iteratorNotification != null && iteratorNotification.isOnError()) { |
| 77 |
throw ExceptionHelper.wrapOrThrow(iteratorNotification.getError()); |
| 78 |
} |
| 79 |
if (iteratorNotification == null || iteratorNotification.isOnNext()) { |
| 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 |
} |
| 97 |
return iteratorNotification.isOnNext(); |
| 98 |
} |
| 99 |
|
| 100 |
@Override |
| 101 |
public T next() { |
| 102 |
if (hasNext()) { |
| 103 |
if (iteratorNotification.isOnNext()) { |
| 104 |
T v = iteratorNotification.getValue(); |
| 105 |
iteratorNotification = null; |
| 106 |
return v; |
| 107 |
} |
| 108 |
} |
| 109 |
throw new NoSuchElementException(); |
| 110 |
} |
| 111 |
|
| 112 |
@Override |
| 113 |
public void remove() { |
| 114 |
throw new UnsupportedOperationException("Read-only iterator."); |
| 115 |
} |
| 116 |
|
| 117 |
} |
| 118 |
} |