package reactor.core.publisher;
import java.util.concurrent.atomic.AtomicReferenceFieldUpdater;
import java.util.function.Consumer;
import org.reactivestreams.Subscriber;
import org.reactivestreams.Subscription;
import reactor.core.Disposable;
import reactor.core.Exceptions;
import reactor.util.annotation.Nullable;
import reactor.util.context.Context;
final class LambdaMonoSubscriber<T> implements InnerConsumer<T>, Disposable {
final Consumer<? super T> consumer;
final Consumer<? super Throwable> errorConsumer;
final Runnable completeConsumer;
final Consumer<? super Subscription> subscriptionConsumer;
volatile Subscription subscription;
static final AtomicReferenceFieldUpdater<LambdaMonoSubscriber, Subscription> S =
AtomicReferenceFieldUpdater.newUpdater(LambdaMonoSubscriber.class,
Subscription.class,
"subscription");
LambdaMonoSubscriber(@Nullable Consumer<? super T> consumer,
@Nullable Consumer<? super Throwable> errorConsumer,
@Nullable Runnable completeConsumer,
@Nullable Consumer<? super Subscription> subscriptionConsumer) {
this.consumer = consumer;
this.errorConsumer = errorConsumer;
this.completeConsumer = completeConsumer;
this.subscriptionConsumer = subscriptionConsumer;
}
@Override
public final void onSubscribe(Subscription s) {
if (Operators.validate(subscription, s)) {
this.subscription = s;
if (subscriptionConsumer != null) {
try {
subscriptionConsumer.accept(s);
}
catch (Throwable t) {
Exceptions.throwIfFatal(t);
s.cancel();
onError(t);
}
}
else {
s.request(Long.MAX_VALUE);
}
}
}
@Override
public final void onComplete() {
Subscription s = S.getAndSet(this, Operators.cancelledSubscription());
if (s == Operators.cancelledSubscription()) {
return;
}
if (completeConsumer != null) {
try {
completeConsumer.run();
}
catch (Throwable t) {
Operators.onErrorDropped(t, Context.empty());
}
}
}
@Override
public final void onError(Throwable t) {
Subscription s = S.getAndSet(this, Operators.cancelledSubscription());
if (s == Operators.cancelledSubscription()) {
Operators.onErrorDropped(t, Context.empty());
return;
}
doError(t);
}
void doError(Throwable t) {
if (errorConsumer != null) {
errorConsumer.accept(t);
}
else {
throw Exceptions.errorCallbackNotImplemented(t);
}
}
@Override
public final void onNext(T x) {
Subscription s = S.getAndSet(this, Operators.cancelledSubscription());
if (s == Operators.cancelledSubscription()) {
Operators.onNextDropped(x, Context.empty());
return;
}
if (consumer != null) {
try {
consumer.accept(x);
}
catch (Throwable t) {
Operators.onErrorDropped(t, Context.empty());
return;
}
}
if (completeConsumer != null) {
try {
completeConsumer.run();
}
catch (Throwable t) {
Operators.onErrorDropped(t, Context.empty());
}
}
}
@Override
@Nullable
public Object scanUnsafe(Attr key) {
if (key == Attr.PARENT) {
return subscription;
}
if (key == Attr.PREFETCH) {
return Integer.MAX_VALUE;
}
if (key == Attr.TERMINATED || key == Attr.CANCELLED) {
return isDisposed();
}
return null;
}
@Override
public boolean isDisposed() {
return subscription == Operators.cancelledSubscription();
}
@Override
public void dispose() {
Subscription s = S.getAndSet(this, Operators.cancelledSubscription());
if (s != null && s != Operators.cancelledSubscription()) {
s.cancel();
}
}
}