Debian Codegraph @

src/main/java/io/reactivex/rxjava3/internal/operators/flowable/FlowableDistinct.java

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