Debian Codegraph @

src/main/java/io/reactivex/rxjava3/internal/operators/observable/ObservableDistinctUntilChanged.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.observable;
15
16 import io.reactivex.rxjava3.annotations.Nullable;
17 import io.reactivex.rxjava3.core.*;
18 import io.reactivex.rxjava3.functions.*;
19 import io.reactivex.rxjava3.internal.observers.BasicFuseableObserver;
20
21 public final class ObservableDistinctUntilChanged<T, K> extends AbstractObservableWithUpstream<T, T> {
22
23 final Function<? super T, K> keySelector;
24
25 final BiPredicate<? super K, ? super K> comparer;
26
27 public ObservableDistinctUntilChanged(ObservableSource<T> source, Function<? super T, K> keySelector, BiPredicate<? super K, ? super K> comparer) {
28 super(source);
29 this.keySelector = keySelector;
30 this.comparer = comparer;
31 }
32
33 @Override
34 protected void subscribeActual(Observer<? super T> observer) {
35 source.subscribe(new DistinctUntilChangedObserver<>(observer, keySelector, comparer));
36 }
37
38 static final class DistinctUntilChangedObserver<T, K> extends BasicFuseableObserver<T, T> {
39
40 final Function<? super T, K> keySelector;
41
42 final BiPredicate<? super K, ? super K> comparer;
43
44 K last;
45
46 boolean hasValue;
47
48 DistinctUntilChangedObserver(Observer<? super T> actual,
49 Function<? super T, K> keySelector,
50 BiPredicate<? super K, ? super K> comparer) {
51 super(actual);
52 this.keySelector = keySelector;
53 this.comparer = comparer;
54 }
55
56 @Override
57 public void onNext(T t) {
58 if (done) {
59 return;
60 }
61 if (sourceMode != NONE) {
62 downstream.onNext(t);
63 return;
64 }
65
66 K key;
67
68 try {
69 key = keySelector.apply(t);
70 if (hasValue) {
71 boolean equal = comparer.test(last, key);
72 last = key;
73 if (equal) {
74 return;
75 }
76 } else {
77 hasValue = true;
78 last = key;
79 }
80 } catch (Throwable ex) {
81 fail(ex);
82 return;
83 }
84
85 downstream.onNext(t);
86 }
87
88 @Override
89 public int requestFusion(int mode) {
90 return transitiveBoundaryFusion(mode);
91 }
92
93 @Nullable
94 @Override
95 public T poll() throws Throwable {
96 for (;;) {
97 T v = qd.poll();
98 if (v == null) {
99 return null;
100 }
101 K key = keySelector.apply(v);
102 if (!hasValue) {
103 hasValue = true;
104 last = key;
105 return v;
106 }
107
108 if (!comparer.test(last, key)) {
109 last = key;
110 return v;
111 }
112 last = key;
113 }
114 }
115
116 }
117 }