hu.akarnokd.rxjava2.internal.operators.BlockingOperatorNext Maven / Gradle / Ivy
Show all versions of rxjava2-backport Show documentation
/**
* Copyright 2015 David Karnok and Netflix, Inc.
*
* Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in
* compliance with the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software distributed under the License is
* distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See
* the License for the specific language governing permissions and limitations under the License.
*/
package hu.akarnokd.rxjava2.internal.operators;
import java.util.*;
import java.util.concurrent.*;
import java.util.concurrent.atomic.AtomicIntegerFieldUpdater;
import org.reactivestreams.Publisher;
import hu.akarnokd.rxjava2.Observable;
import hu.akarnokd.rxjava2.Optional;
import hu.akarnokd.rxjava2.Try;
import hu.akarnokd.rxjava2.internal.subscribers.DisposableSubscriber;
import hu.akarnokd.rxjava2.internal.util.Exceptions;
/**
* Returns an Iterable that blocks until the Observable emits another item, then returns that item.
*
*
*/
public enum BlockingOperatorNext {
;
/**
* Returns an {@code Iterable} that blocks until the {@code Observable} emits another item, then returns
* that item.
*
* @param the value type
* @param items
* the {@code Observable} to observe
* @return an {@code Iterable} that behaves like a blocking version of {@code items}
*/
public static Iterable next(final Publisher extends T> items) {
return new Iterable() {
@Override
public Iterator iterator() {
NextObserver nextObserver = new NextObserver();
return new NextIterator(items, nextObserver);
}
};
}
// test needs to access the observer.waiting flag non-blockingly.
/* private */static final class NextIterator implements Iterator {
private final NextObserver observer;
private final Publisher extends T> items;
private T next;
private boolean hasNext = true;
private boolean isNextConsumed = true;
private Throwable error = null;
private boolean started = false;
private NextIterator(Publisher extends T> items, NextObserver observer) {
this.items = items;
this.observer = observer;
}
@Override
public boolean hasNext() {
if (error != null) {
// If any error has already been thrown, throw it again.
throw Exceptions.propagate(error);
}
// Since an iterator should not be used in different thread,
// so we do not need any synchronization.
if (hasNext == false) {
// the iterator has reached the end.
return false;
}
if (isNextConsumed == false) {
// next has not been used yet.
return true;
}
return moveToNext();
}
private boolean moveToNext() {
try {
if (!started) {
started = true;
// if not started, start now
observer.setWaiting(1);
Observable.fromPublisher(items)
.materialize().subscribe(observer);
}
Try> nextNotification = observer.takeNext();
if (isOnNext(nextNotification)) {
isNextConsumed = false;
next = nextNotification.value().get();
return true;
}
// If an observable is completed or fails,
// hasNext() always return false.
hasNext = false;
if (isOnComplete(nextNotification)) {
return false;
}
if (nextNotification.hasError()) {
error = nextNotification.error();
throw Exceptions.propagate(error);
}
throw new IllegalStateException("Should not reach here");
} catch (InterruptedException e) {
observer.dispose();
Thread.currentThread().interrupt();
error = e;
throw Exceptions.propagate(error);
}
}
@Override
public T next() {
if (error != null) {
// If any error has already been thrown, throw it again.
throw Exceptions.propagate(error);
}
if (hasNext()) {
isNextConsumed = true;
return next;
}
else {
throw new NoSuchElementException("No more elements");
}
}
@Override
public void remove() {
throw new UnsupportedOperationException("Read only iterator");
}
}
private static class NextObserver extends DisposableSubscriber>> {
private final BlockingQueue>> buf = new ArrayBlockingQueue>>(1);
@SuppressWarnings("unused")
volatile int waiting;
@SuppressWarnings("rawtypes")
static final AtomicIntegerFieldUpdater WAITING_UPDATER
= AtomicIntegerFieldUpdater.newUpdater(NextObserver.class, "waiting");
@Override
public void onComplete() {
// ignore
}
@Override
public void onError(Throwable e) {
// ignore
}
@Override
public void onNext(Try> args) {
if (WAITING_UPDATER.getAndSet(this, 0) == 1 || !isOnNext(args)) {
Try> toOffer = args;
while (!buf.offer(toOffer)) {
Try> concurrentItem = buf.poll();
// in case if we won race condition with onComplete/onError method
if (concurrentItem != null && !isOnNext(concurrentItem)) {
toOffer = concurrentItem;
}
}
}
}
public Try> takeNext() throws InterruptedException {
setWaiting(1);
return buf.take();
}
void setWaiting(int value) {
waiting = value;
}
}
static boolean isOnNext(Try> notification) {
return notification.hasValue() && notification.value().isPresent();
}
static boolean isOnComplete(Try> notification) {
return notification.hasValue() && !notification.value().isPresent();
}
}