jimu-decompiled/sources/io/reactivex/internal/observers/FutureObserver.java
2025-05-13 19:24:51 +02:00

142 lines
4.2 KiB
Java

package io.reactivex.internal.observers;
import io.reactivex.Observer;
import io.reactivex.disposables.Disposable;
import io.reactivex.internal.disposables.DisposableHelper;
import io.reactivex.internal.util.BlockingHelper;
import io.reactivex.plugins.RxJavaPlugins;
import java.util.NoSuchElementException;
import java.util.concurrent.CancellationException;
import java.util.concurrent.CountDownLatch;
import java.util.concurrent.ExecutionException;
import java.util.concurrent.Future;
import java.util.concurrent.TimeUnit;
import java.util.concurrent.TimeoutException;
import java.util.concurrent.atomic.AtomicReference;
/* loaded from: classes2.dex */
public final class FutureObserver<T> extends CountDownLatch implements Observer<T>, Future<T>, Disposable {
T a;
Throwable b;
final AtomicReference<Disposable> c;
public FutureObserver() {
super(1);
this.c = new AtomicReference<>();
}
@Override // java.util.concurrent.Future
public boolean cancel(boolean z) {
Disposable disposable;
DisposableHelper disposableHelper;
do {
disposable = this.c.get();
if (disposable == this || disposable == (disposableHelper = DisposableHelper.DISPOSED)) {
return false;
}
} while (!this.c.compareAndSet(disposable, disposableHelper));
if (disposable != null) {
disposable.dispose();
}
countDown();
return true;
}
@Override // io.reactivex.disposables.Disposable
public void dispose() {
}
@Override // java.util.concurrent.Future
public T get() throws InterruptedException, ExecutionException {
if (getCount() != 0) {
BlockingHelper.a();
await();
}
if (isCancelled()) {
throw new CancellationException();
}
Throwable th = this.b;
if (th == null) {
return this.a;
}
throw new ExecutionException(th);
}
@Override // java.util.concurrent.Future
public boolean isCancelled() {
return DisposableHelper.isDisposed(this.c.get());
}
@Override // java.util.concurrent.Future
public boolean isDone() {
return getCount() == 0;
}
@Override // io.reactivex.Observer
public void onComplete() {
Disposable disposable;
if (this.a == null) {
onError(new NoSuchElementException("The source is empty"));
return;
}
do {
disposable = this.c.get();
if (disposable == this || disposable == DisposableHelper.DISPOSED) {
return;
}
} while (!this.c.compareAndSet(disposable, this));
countDown();
}
@Override // io.reactivex.Observer
public void onError(Throwable th) {
Disposable disposable;
if (this.b != null) {
RxJavaPlugins.b(th);
return;
}
this.b = th;
do {
disposable = this.c.get();
if (disposable == this || disposable == DisposableHelper.DISPOSED) {
RxJavaPlugins.b(th);
return;
}
} while (!this.c.compareAndSet(disposable, this));
countDown();
}
@Override // io.reactivex.Observer
public void onNext(T t) {
if (this.a == null) {
this.a = t;
} else {
this.c.get().dispose();
onError(new IndexOutOfBoundsException("More than one element received"));
}
}
@Override // io.reactivex.Observer
public void onSubscribe(Disposable disposable) {
DisposableHelper.setOnce(this.c, disposable);
}
@Override // java.util.concurrent.Future
public T get(long j, TimeUnit timeUnit) throws InterruptedException, ExecutionException, TimeoutException {
if (getCount() != 0) {
BlockingHelper.a();
if (!await(j, timeUnit)) {
throw new TimeoutException();
}
}
if (!isCancelled()) {
Throwable th = this.b;
if (th == null) {
return this.a;
}
throw new ExecutionException(th);
}
throw new CancellationException();
}
}