package reactor.core.publisher;
import java.util.Objects;
import java.util.function.Predicate;
import org.reactivestreams.Subscription;
import reactor.core.CoreSubscriber;
import reactor.core.Fuseable;
import reactor.util.annotation.Nullable;
import reactor.util.context.Context;
final class FluxFilterFuseable<T> extends FluxOperator<T, T> implements Fuseable {
final Predicate<? super T> predicate;
FluxFilterFuseable(Flux<? extends T> source, Predicate<? super T> predicate) {
super(source);
this.predicate = Objects.requireNonNull(predicate, "predicate");
}
@Override
@SuppressWarnings("unchecked")
public void subscribe(CoreSubscriber<? super T> actual) {
if (actual instanceof ConditionalSubscriber) {
source.subscribe(new FilterFuseableConditionalSubscriber<>((ConditionalSubscriber<? super T>) actual,
predicate));
return;
}
source.subscribe(new FilterFuseableSubscriber<>(actual, predicate));
}
static final class FilterFuseableSubscriber<T>
implements InnerOperator<T, T>, QueueSubscription<T>,
ConditionalSubscriber<T> {
final CoreSubscriber<? super T> actual;
final Context ctx;
final Predicate<? super T> predicate;
QueueSubscription<T> s;
boolean done;
int sourceMode;
FilterFuseableSubscriber(CoreSubscriber<? super T> actual,
Predicate<? super T> predicate) {
this.actual = actual;
this.ctx = actual.currentContext();
this.predicate = predicate;
}
@SuppressWarnings("unchecked")
@Override
public void onSubscribe(Subscription s) {
if (Operators.validate(this.s, s)) {
this.s = (QueueSubscription<T>) s;
actual.onSubscribe(this);
}
}
@Override
public void onNext(T t) {
if (sourceMode == ASYNC) {
actual.onNext(null);
}
else {
if (done) {
Operators.onNextDropped(t, this.ctx);
return;
}
boolean b;
try {
b = predicate.test(t);
}
catch (Throwable e) {
Throwable e_ = Operators.onNextError(t, e, this.ctx, s);
if (e_ != null) {
onError(e_);
}
else {
s.request(1);
}
Operators.onDiscard(t, this.ctx);
return;
}
if (b) {
actual.onNext(t);
}
else {
s.request(1);
Operators.onDiscard(t, this.ctx);
}
}
}
@Override
public boolean tryOnNext(T t) {
if (done) {
Operators.onNextDropped(t, this.ctx);
return false;
}
boolean b;
try {
b = predicate.test(t);
}
catch (Throwable e) {
Throwable e_ = Operators.onNextError(t, e, this.ctx, s);
if (e_ != null) {
onError(e_);
}
Operators.onDiscard(t, this.ctx);
return false;
}
if (b) {
actual.onNext(t);
return true;
}
Operators.onDiscard(t, this.ctx);
return false;
}
@Override
public void onError(Throwable t) {
if (done) {
Operators.onErrorDropped(t, this.ctx);
return;
}
done = true;
actual.onError(t);
}
@Override
public void onComplete() {
if (done) {
return;
}
done = true;
actual.onComplete();
}
@Override
@Nullable
public Object scanUnsafe(Attr key) {
if (key == Attr.PARENT) return s;
if (key == Attr.TERMINATED) return done;
return InnerOperator.super.scanUnsafe(key);
}
@Override
public CoreSubscriber<? super T> actual() {
return actual;
}
@Override
public void request(long n) {
s.request(n);
}
@Override
public void cancel() {
s.cancel();
}
@Override
@Nullable
public T poll() {
if (sourceMode == ASYNC) {
long dropped = 0;
for (; ; ) {
T v = s.poll();
try {
if (v == null || predicate.test(v)) {
if (dropped != 0) {
request(dropped);
}
return v;
}
Operators.onDiscard(v, this.ctx);
dropped++;
}
catch (Throwable e) {
RuntimeException e_ = Operators.onNextPollError(v, e, currentContext());
Operators.onDiscard(v, this.ctx);
if (e_ != null) {
throw e_;
}
}
}
}
else {
for (; ; ) {
T v = s.poll();
try {
if (v == null || predicate.test(v)) {
return v;
}
Operators.onDiscard(v, this.ctx);
}
catch (Throwable e) {
RuntimeException e_ = Operators.onNextPollError(v, e, currentContext());
Operators.onDiscard(v, this.ctx);
if (e_ != null) {
throw e_;
}
}
}
}
}
@Override
public boolean isEmpty() {
return s.isEmpty();
}
@Override
public void clear() {
s.clear();
}
@Override
public int requestFusion(int requestedMode) {
int m;
if ((requestedMode & Fuseable.THREAD_BARRIER) != 0) {
return Fuseable.NONE;
}
else {
m = s.requestFusion(requestedMode);
}
sourceMode = m;
return m;
}
@Override
public int size() {
return s.size();
}
}
static final class FilterFuseableConditionalSubscriber<T>
implements InnerOperator<T, T>, ConditionalSubscriber<T>,
QueueSubscription<T> {
final ConditionalSubscriber<? super T> actual;
final Context ctx;
final Predicate<? super T> predicate;
QueueSubscription<T> s;
boolean done;
int sourceMode;
FilterFuseableConditionalSubscriber(ConditionalSubscriber<? super T> actual,
Predicate<? super T> predicate) {
this.actual = actual;
this.ctx = actual.currentContext();
this.predicate = predicate;
}
@SuppressWarnings("unchecked")
@Override
public void onSubscribe(Subscription s) {
if (Operators.validate(this.s, s)) {
this.s = (QueueSubscription<T>) s;
actual.onSubscribe(this);
}
}
@Override
public void onNext(T t) {
if (sourceMode == ASYNC) {
actual.onNext(null);
}
else {
if (done) {
Operators.onNextDropped(t, this.ctx);
return;
}
boolean b;
try {
b = predicate.test(t);
}
catch (Throwable e) {
Throwable e_ = Operators.onNextError(t, e, this.ctx, s);
if (e_ != null) {
onError(e_);
}
else {
s.request(1);
}
Operators.onDiscard(t, this.ctx);
return;
}
if (b) {
actual.onNext(t);
}
else {
s.request(1);
Operators.onDiscard(t, this.ctx);
}
}
}
@Override
public boolean tryOnNext(T t) {
if (done) {
Operators.onNextDropped(t, this.ctx);
return false;
}
boolean b;
try {
b = predicate.test(t);
}
catch (Throwable e) {
Throwable e_ = Operators.onNextError(t, e, this.ctx, s);
if (e_ != null) {
onError(e_);
}
Operators.onDiscard(t, this.ctx);
return false;
}
if (b) {
return actual.tryOnNext(t);
}
else {
Operators.onDiscard(t, this.ctx);
return false;
}
}
@Override
public void onError(Throwable t) {
if (done) {
Operators.onErrorDropped(t, this.ctx);
return;
}
done = true;
actual.onError(t);
}
@Override
public void onComplete() {
if (done) {
return;
}
done = true;
actual.onComplete();
}
@Override
@Nullable
public Object scanUnsafe(Attr key) {
if (key == Attr.PARENT) return s;
if (key == Attr.TERMINATED) return done;
return InnerOperator.super.scanUnsafe(key);
}
@Override
public CoreSubscriber<? super T> actual() {
return actual;
}
@Override
public void request(long n) {
s.request(n);
}
@Override
public void cancel() {
s.cancel();
}
@Override
@Nullable
public T poll() {
if (sourceMode == ASYNC) {
long dropped = 0;
for (; ; ) {
T v = s.poll();
try {
if (v == null || predicate.test(v)) {
if (dropped != 0) {
request(dropped);
}
return v;
}
Operators.onDiscard(v, this.ctx);
dropped++;
}
catch (Throwable e) {
RuntimeException e_ = Operators.onNextPollError(v, e, this.ctx);
Operators.onDiscard(v, this.ctx);
if (e_ != null) {
throw e_;
}
}
}
}
else {
for (; ; ) {
T v = s.poll();
try {
if (v == null || predicate.test(v)) {
return v;
}
Operators.onDiscard(v, this.ctx);
}
catch (Throwable e) {
RuntimeException e_ = Operators.onNextPollError(v, e, this.ctx);
Operators.onDiscard(v, this.ctx);
if (e_ != null) {
throw e_;
}
}
}
}
}
@Override
public boolean isEmpty() {
return s.isEmpty();
}
@Override
public void clear() {
s.clear();
}
@Override
public int size() {
return s.size();
}
@Override
public int requestFusion(int requestedMode) {
int m;
if ((requestedMode & Fuseable.THREAD_BARRIER) != 0) {
return Fuseable.NONE;
}
else {
m = s.requestFusion(requestedMode);
}
sourceMode = m;
return m;
}
}
}