| 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.observable; |
| 15 |
|
| 16 |
import java.util.Collection; |
| 17 |
import java.util.Objects; |
| 18 |
|
| 19 |
import io.reactivex.rxjava3.annotations.Nullable; |
| 20 |
import io.reactivex.rxjava3.core.*; |
| 21 |
import io.reactivex.rxjava3.exceptions.Exceptions; |
| 22 |
import io.reactivex.rxjava3.functions.*; |
| 23 |
import io.reactivex.rxjava3.internal.disposables.EmptyDisposable; |
| 24 |
import io.reactivex.rxjava3.internal.observers.BasicFuseableObserver; |
| 25 |
import io.reactivex.rxjava3.internal.util.ExceptionHelper; |
| 26 |
import io.reactivex.rxjava3.plugins.RxJavaPlugins; |
| 27 |
|
| 28 |
public final class ObservableDistinct<T, K> extends AbstractObservableWithUpstream<T, T> { |
| 29 |
|
| 30 |
final Function<? super T, K> keySelector; |
| 31 |
|
| 32 |
final Supplier<? extends Collection<? super K>> collectionSupplier; |
| 33 |
|
| 34 |
public ObservableDistinct(ObservableSource<T> source, Function<? super T, K> keySelector, Supplier<? extends Collection<? super K>> collectionSupplier) { |
| 35 |
super(source); |
| 36 |
this.keySelector = keySelector; |
| 37 |
this.collectionSupplier = collectionSupplier; |
| 38 |
} |
| 39 |
|
| 40 |
@Override |
| 41 |
protected void subscribeActual(Observer<? super T> observer) { |
| 42 |
Collection<? super K> collection; |
| 43 |
|
| 44 |
try { |
| 45 |
collection = ExceptionHelper.nullCheck(collectionSupplier.get(), "The collectionSupplier returned a null Collection."); |
| 46 |
} catch (Throwable ex) { |
| 47 |
Exceptions.throwIfFatal(ex); |
| 48 |
EmptyDisposable.error(ex, observer); |
| 49 |
return; |
| 50 |
} |
| 51 |
|
| 52 |
source.subscribe(new DistinctObserver<>(observer, keySelector, collection)); |
| 53 |
} |
| 54 |
|
| 55 |
static final class DistinctObserver<T, K> extends BasicFuseableObserver<T, T> { |
| 56 |
|
| 57 |
final Collection<? super K> collection; |
| 58 |
|
| 59 |
final Function<? super T, K> keySelector; |
| 60 |
|
| 61 |
DistinctObserver(Observer<? super T> actual, Function<? super T, K> keySelector, Collection<? super K> collection) { |
| 62 |
super(actual); |
| 63 |
this.keySelector = keySelector; |
| 64 |
this.collection = collection; |
| 65 |
} |
| 66 |
|
| 67 |
@Override |
| 68 |
public void onNext(T value) { |
| 69 |
if (done) { |
| 70 |
return; |
| 71 |
} |
| 72 |
if (sourceMode == NONE) { |
| 73 |
K key; |
| 74 |
boolean b; |
| 75 |
|
| 76 |
try { |
| 77 |
key = Objects.requireNonNull(keySelector.apply(value), "The keySelector returned a null key"); |
| 78 |
b = collection.add(key); |
| 79 |
} catch (Throwable ex) { |
| 80 |
fail(ex); |
| 81 |
return; |
| 82 |
} |
| 83 |
|
| 84 |
if (b) { |
| 85 |
downstream.onNext(value); |
| 86 |
} |
| 87 |
} else { |
| 88 |
downstream.onNext(null); |
| 89 |
} |
| 90 |
} |
| 91 |
|
| 92 |
@Override |
| 93 |
public void onError(Throwable e) { |
| 94 |
if (done) { |
| 95 |
RxJavaPlugins.onError(e); |
| 96 |
} else { |
| 97 |
done = true; |
| 98 |
collection.clear(); |
| 99 |
downstream.onError(e); |
| 100 |
} |
| 101 |
} |
| 102 |
|
| 103 |
@Override |
| 104 |
public void onComplete() { |
| 105 |
if (!done) { |
| 106 |
done = true; |
| 107 |
collection.clear(); |
| 108 |
downstream.onComplete(); |
| 109 |
} |
| 110 |
} |
| 111 |
|
| 112 |
@Override |
| 113 |
public int requestFusion(int mode) { |
| 114 |
return transitiveBoundaryFusion(mode); |
| 115 |
} |
| 116 |
|
| 117 |
@Nullable |
| 118 |
@Override |
| 119 |
public T poll() throws Throwable { |
| 120 |
for (;;) { |
| 121 |
T v = qd.poll(); |
| 122 |
|
| 123 |
if (v == null || collection.add(Objects.requireNonNull(keySelector.apply(v), "The keySelector returned a null key"))) { |
| 124 |
return v; |
| 125 |
} |
| 126 |
} |
| 127 |
} |
| 128 |
|
| 129 |
@Override |
| 130 |
public void clear() { |
| 131 |
collection.clear(); |
| 132 |
super.clear(); |
| 133 |
} |
| 134 |
} |
| 135 |
} |