| 1 |
/* |
| 2 |
* Copyright (c) 2016-present, RxJava Contributors. |
| 3 |
* |
| 4 |
* Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in |
| 5 |
* compliance with the License. You may obtain a copy of the License at |
| 6 |
* |
| 7 |
* http://www.apache.org/licenses/LICENSE-2.0 |
| 8 |
* |
| 9 |
* Unless required by applicable law or agreed to in writing, software distributed under the License is |
| 10 |
* distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See |
| 11 |
* the License for the specific language governing permissions and limitations under the License. |
| 12 |
*/ |
| 13 |
|
| 14 |
package io.reactivex.rxjava3.internal.operators.flowable; |
| 15 |
|
| 16 |
import org.reactivestreams.*; |
| 17 |
|
| 18 |
import io.reactivex.rxjava3.core.*; |
| 19 |
import io.reactivex.rxjava3.exceptions.Exceptions; |
| 20 |
import io.reactivex.rxjava3.functions.Predicate; |
| 21 |
import io.reactivex.rxjava3.internal.subscriptions.*; |
| 22 |
import io.reactivex.rxjava3.plugins.RxJavaPlugins; |
| 23 |
|
| 24 |
public final class FlowableAny<T> extends AbstractFlowableWithUpstream<T, Boolean> { |
| 25 |
final Predicate<? super T> predicate; |
| 26 |
public FlowableAny(Flowable<T> source, Predicate<? super T> predicate) { |
| 27 |
super(source); |
| 28 |
this.predicate = predicate; |
| 29 |
} |
| 30 |
|
| 31 |
@Override |
| 32 |
protected void subscribeActual(Subscriber<? super Boolean> s) { |
| 33 |
source.subscribe(new AnySubscriber<>(s, predicate)); |
| 34 |
} |
| 35 |
|
| 36 |
static final class AnySubscriber<T> extends DeferredScalarSubscription<Boolean> implements FlowableSubscriber<T> { |
| 37 |
|
| 38 |
private static final long serialVersionUID = -2311252482644620661L; |
| 39 |
|
| 40 |
final Predicate<? super T> predicate; |
| 41 |
|
| 42 |
Subscription upstream; |
| 43 |
|
| 44 |
boolean done; |
| 45 |
|
| 46 |
AnySubscriber(Subscriber<? super Boolean> actual, Predicate<? super T> predicate) { |
| 47 |
super(actual); |
| 48 |
this.predicate = predicate; |
| 49 |
} |
| 50 |
|
| 51 |
@Override |
| 52 |
public void onSubscribe(Subscription s) { |
| 53 |
if (SubscriptionHelper.validate(this.upstream, s)) { |
| 54 |
this.upstream = s; |
| 55 |
downstream.onSubscribe(this); |
| 56 |
s.request(Long.MAX_VALUE); |
| 57 |
} |
| 58 |
} |
| 59 |
|
| 60 |
@Override |
| 61 |
public void onNext(T t) { |
| 62 |
if (done) { |
| 63 |
return; |
| 64 |
} |
| 65 |
boolean b; |
| 66 |
try { |
| 67 |
b = predicate.test(t); |
| 68 |
} catch (Throwable e) { |
| 69 |
Exceptions.throwIfFatal(e); |
| 70 |
upstream.cancel(); |
| 71 |
onError(e); |
| 72 |
return; |
| 73 |
} |
| 74 |
if (b) { |
| 75 |
done = true; |
| 76 |
upstream.cancel(); |
| 77 |
complete(true); |
| 78 |
} |
| 79 |
} |
| 80 |
|
| 81 |
@Override |
| 82 |
public void onError(Throwable t) { |
| 83 |
if (done) { |
| 84 |
RxJavaPlugins.onError(t); |
| 85 |
return; |
| 86 |
} |
| 87 |
|
| 88 |
done = true; |
| 89 |
downstream.onError(t); |
| 90 |
} |
| 91 |
|
| 92 |
@Override |
| 93 |
public void onComplete() { |
| 94 |
if (!done) { |
| 95 |
done = true; |
| 96 |
complete(false); |
| 97 |
} |
| 98 |
} |
| 99 |
|
| 100 |
@Override |
| 101 |
public void cancel() { |
| 102 |
super.cancel(); |
| 103 |
upstream.cancel(); |
| 104 |
} |
| 105 |
} |
| 106 |
} |