package io.reactivex.internal.operators.observable; import io.reactivex.Notification; import io.reactivex.ObservableSource; import io.reactivex.internal.util.BlockingHelper; import io.reactivex.internal.util.ExceptionHelper; import io.reactivex.observers.DisposableObserver; import io.reactivex.plugins.RxJavaPlugins; import java.util.Iterator; import java.util.NoSuchElementException; import java.util.concurrent.ArrayBlockingQueue; import java.util.concurrent.BlockingQueue; import java.util.concurrent.atomic.AtomicInteger; /* loaded from: classes2.dex */ public final class BlockingObservableNext implements Iterable { final ObservableSource a; static final class NextIterator implements Iterator { private final NextObserver a; private final ObservableSource b; private T c; private boolean d = true; private boolean e = true; private Throwable f; private boolean g; NextIterator(ObservableSource observableSource, NextObserver nextObserver) { this.b = observableSource; this.a = nextObserver; } private boolean a() { if (!this.g) { this.g = true; this.a.b(); new ObservableMaterialize(this.b).subscribe(this.a); } try { Notification c = this.a.c(); if (c.e()) { this.e = false; this.c = c.b(); return true; } this.d = false; if (c.c()) { return false; } this.f = c.a(); throw ExceptionHelper.a(this.f); } catch (InterruptedException e) { this.a.dispose(); this.f = e; throw ExceptionHelper.a(e); } } @Override // java.util.Iterator public boolean hasNext() { Throwable th = this.f; if (th != null) { throw ExceptionHelper.a(th); } if (this.d) { return !this.e || a(); } return false; } @Override // java.util.Iterator public T next() { Throwable th = this.f; if (th != null) { throw ExceptionHelper.a(th); } if (!hasNext()) { throw new NoSuchElementException("No more elements"); } this.e = true; return this.c; } @Override // java.util.Iterator public void remove() { throw new UnsupportedOperationException("Read only iterator"); } } static final class NextObserver extends DisposableObserver> { private final BlockingQueue> b = new ArrayBlockingQueue(1); final AtomicInteger c = new AtomicInteger(); NextObserver() { } @Override // io.reactivex.Observer /* renamed from: a, reason: merged with bridge method [inline-methods] */ public void onNext(Notification notification) { if (this.c.getAndSet(0) == 1 || !notification.e()) { while (!this.b.offer(notification)) { Notification poll = this.b.poll(); if (poll != null && !poll.e()) { notification = poll; } } } } void b() { this.c.set(1); } public Notification c() throws InterruptedException { b(); BlockingHelper.a(); return this.b.take(); } @Override // io.reactivex.Observer public void onComplete() { } @Override // io.reactivex.Observer public void onError(Throwable th) { RxJavaPlugins.b(th); } } public BlockingObservableNext(ObservableSource observableSource) { this.a = observableSource; } @Override // java.lang.Iterable public Iterator iterator() { return new NextIterator(this.a, new NextObserver()); } }