| 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.single; |
| 15 |
|
| 16 |
import io.reactivex.rxjava3.core.*; |
| 17 |
import io.reactivex.rxjava3.disposables.Disposable; |
| 18 |
import io.reactivex.rxjava3.exceptions.Exceptions; |
| 19 |
import io.reactivex.rxjava3.functions.BiPredicate; |
| 20 |
|
| 21 |
public final class SingleContains<T> extends Single<Boolean> { |
| 22 |
|
| 23 |
final SingleSource<T> source; |
| 24 |
|
| 25 |
final Object value; |
| 26 |
|
| 27 |
final BiPredicate<Object, Object> comparer; |
| 28 |
|
| 29 |
public SingleContains(SingleSource<T> source, Object value, BiPredicate<Object, Object> comparer) { |
| 30 |
this.source = source; |
| 31 |
this.value = value; |
| 32 |
this.comparer = comparer; |
| 33 |
} |
| 34 |
|
| 35 |
@Override |
| 36 |
protected void subscribeActual(final SingleObserver<? super Boolean> observer) { |
| 37 |
|
| 38 |
source.subscribe(new ContainsSingleObserver(observer)); |
| 39 |
} |
| 40 |
|
| 41 |
final class ContainsSingleObserver implements SingleObserver<T> { |
| 42 |
|
| 43 |
private final SingleObserver<? super Boolean> downstream; |
| 44 |
|
| 45 |
ContainsSingleObserver(SingleObserver<? super Boolean> observer) { |
| 46 |
this.downstream = observer; |
| 47 |
} |
| 48 |
|
| 49 |
@Override |
| 50 |
public void onSubscribe(Disposable d) { |
| 51 |
downstream.onSubscribe(d); |
| 52 |
} |
| 53 |
|
| 54 |
@Override |
| 55 |
public void onSuccess(T v) { |
| 56 |
boolean b; |
| 57 |
|
| 58 |
try { |
| 59 |
b = comparer.test(v, value); |
| 60 |
} catch (Throwable ex) { |
| 61 |
Exceptions.throwIfFatal(ex); |
| 62 |
downstream.onError(ex); |
| 63 |
return; |
| 64 |
} |
| 65 |
downstream.onSuccess(b); |
| 66 |
} |
| 67 |
|
| 68 |
@Override |
| 69 |
public void onError(Throwable e) { |
| 70 |
downstream.onError(e); |
| 71 |
} |
| 72 |
|
| 73 |
} |
| 74 |
} |