| 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.Subscriber; |
| 17 |
|
| 18 |
import io.reactivex.rxjava3.annotations.Nullable; |
| 19 |
import io.reactivex.rxjava3.core.Flowable; |
| 20 |
import io.reactivex.rxjava3.functions.*; |
| 21 |
import io.reactivex.rxjava3.internal.subscribers.*; |
| 22 |
import io.reactivex.rxjava3.operators.ConditionalSubscriber; |
| 23 |
|
| 24 |
public final class FlowableDistinctUntilChanged<T, K> extends AbstractFlowableWithUpstream<T, T> { |
| 25 |
|
| 26 |
final Function<? super T, K> keySelector; |
| 27 |
|
| 28 |
final BiPredicate<? super K, ? super K> comparer; |
| 29 |
|
| 30 |
public FlowableDistinctUntilChanged(Flowable<T> source, Function<? super T, K> keySelector, BiPredicate<? super K, ? super K> comparer) { |
| 31 |
super(source); |
| 32 |
this.keySelector = keySelector; |
| 33 |
this.comparer = comparer; |
| 34 |
} |
| 35 |
|
| 36 |
@Override |
| 37 |
protected void subscribeActual(Subscriber<? super T> s) { |
| 38 |
if (s instanceof ConditionalSubscriber) { |
| 39 |
ConditionalSubscriber<? super T> cs = (ConditionalSubscriber<? super T>) s; |
| 40 |
source.subscribe(new DistinctUntilChangedConditionalSubscriber<>(cs, keySelector, comparer)); |
| 41 |
} else { |
| 42 |
source.subscribe(new DistinctUntilChangedSubscriber<>(s, keySelector, comparer)); |
| 43 |
} |
| 44 |
} |
| 45 |
|
| 46 |
static final class DistinctUntilChangedSubscriber<T, K> extends BasicFuseableSubscriber<T, T> |
| 47 |
implements ConditionalSubscriber<T> { |
| 48 |
|
| 49 |
final Function<? super T, K> keySelector; |
| 50 |
|
| 51 |
final BiPredicate<? super K, ? super K> comparer; |
| 52 |
|
| 53 |
K last; |
| 54 |
|
| 55 |
boolean hasValue; |
| 56 |
|
| 57 |
DistinctUntilChangedSubscriber(Subscriber<? super T> actual, |
| 58 |
Function<? super T, K> keySelector, |
| 59 |
BiPredicate<? super K, ? super K> comparer) { |
| 60 |
super(actual); |
| 61 |
this.keySelector = keySelector; |
| 62 |
this.comparer = comparer; |
| 63 |
} |
| 64 |
|
| 65 |
@Override |
| 66 |
public void onNext(T t) { |
| 67 |
if (!tryOnNext(t)) { |
| 68 |
upstream.request(1); |
| 69 |
} |
| 70 |
} |
| 71 |
|
| 72 |
@Override |
| 73 |
public boolean tryOnNext(T t) { |
| 74 |
if (done) { |
| 75 |
return false; |
| 76 |
} |
| 77 |
if (sourceMode != NONE) { |
| 78 |
downstream.onNext(t); |
| 79 |
return true; |
| 80 |
} |
| 81 |
|
| 82 |
K key; |
| 83 |
|
| 84 |
try { |
| 85 |
key = keySelector.apply(t); |
| 86 |
if (hasValue) { |
| 87 |
boolean equal = comparer.test(last, key); |
| 88 |
last = key; |
| 89 |
if (equal) { |
| 90 |
return false; |
| 91 |
} |
| 92 |
} else { |
| 93 |
hasValue = true; |
| 94 |
last = key; |
| 95 |
} |
| 96 |
} catch (Throwable ex) { |
| 97 |
fail(ex); |
| 98 |
return true; |
| 99 |
} |
| 100 |
|
| 101 |
downstream.onNext(t); |
| 102 |
return true; |
| 103 |
} |
| 104 |
|
| 105 |
@Override |
| 106 |
public int requestFusion(int mode) { |
| 107 |
return transitiveBoundaryFusion(mode); |
| 108 |
} |
| 109 |
|
| 110 |
@Nullable |
| 111 |
@Override |
| 112 |
public T poll() throws Throwable { |
| 113 |
for (;;) { |
| 114 |
T v = qs.poll(); |
| 115 |
if (v == null) { |
| 116 |
return null; |
| 117 |
} |
| 118 |
K key = keySelector.apply(v); |
| 119 |
if (!hasValue) { |
| 120 |
hasValue = true; |
| 121 |
last = key; |
| 122 |
return v; |
| 123 |
} |
| 124 |
|
| 125 |
if (!comparer.test(last, key)) { |
| 126 |
last = key; |
| 127 |
return v; |
| 128 |
} |
| 129 |
last = key; |
| 130 |
if (sourceMode != SYNC) { |
| 131 |
upstream.request(1); |
| 132 |
} |
| 133 |
} |
| 134 |
} |
| 135 |
|
| 136 |
} |
| 137 |
|
| 138 |
static final class DistinctUntilChangedConditionalSubscriber<T, K> extends BasicFuseableConditionalSubscriber<T, T> { |
| 139 |
|
| 140 |
final Function<? super T, K> keySelector; |
| 141 |
|
| 142 |
final BiPredicate<? super K, ? super K> comparer; |
| 143 |
|
| 144 |
K last; |
| 145 |
|
| 146 |
boolean hasValue; |
| 147 |
|
| 148 |
DistinctUntilChangedConditionalSubscriber(ConditionalSubscriber<? super T> actual, |
| 149 |
Function<? super T, K> keySelector, |
| 150 |
BiPredicate<? super K, ? super K> comparer) { |
| 151 |
super(actual); |
| 152 |
this.keySelector = keySelector; |
| 153 |
this.comparer = comparer; |
| 154 |
} |
| 155 |
|
| 156 |
@Override |
| 157 |
public void onNext(T t) { |
| 158 |
if (!tryOnNext(t)) { |
| 159 |
upstream.request(1); |
| 160 |
} |
| 161 |
} |
| 162 |
|
| 163 |
@Override |
| 164 |
public boolean tryOnNext(T t) { |
| 165 |
if (done) { |
| 166 |
return false; |
| 167 |
} |
| 168 |
if (sourceMode != NONE) { |
| 169 |
return downstream.tryOnNext(t); |
| 170 |
} |
| 171 |
|
| 172 |
K key; |
| 173 |
|
| 174 |
try { |
| 175 |
key = keySelector.apply(t); |
| 176 |
if (hasValue) { |
| 177 |
boolean equal = comparer.test(last, key); |
| 178 |
last = key; |
| 179 |
if (equal) { |
| 180 |
return false; |
| 181 |
} |
| 182 |
} else { |
| 183 |
hasValue = true; |
| 184 |
last = key; |
| 185 |
} |
| 186 |
} catch (Throwable ex) { |
| 187 |
fail(ex); |
| 188 |
return true; |
| 189 |
} |
| 190 |
|
| 191 |
downstream.onNext(t); |
| 192 |
return true; |
| 193 |
} |
| 194 |
|
| 195 |
@Override |
| 196 |
public int requestFusion(int mode) { |
| 197 |
return transitiveBoundaryFusion(mode); |
| 198 |
} |
| 199 |
|
| 200 |
@Nullable |
| 201 |
@Override |
| 202 |
public T poll() throws Throwable { |
| 203 |
for (;;) { |
| 204 |
T v = qs.poll(); |
| 205 |
if (v == null) { |
| 206 |
return null; |
| 207 |
} |
| 208 |
K key = keySelector.apply(v); |
| 209 |
if (!hasValue) { |
| 210 |
hasValue = true; |
| 211 |
last = key; |
| 212 |
return v; |
| 213 |
} |
| 214 |
|
| 215 |
if (!comparer.test(last, key)) { |
| 216 |
last = key; |
| 217 |
return v; |
| 218 |
} |
| 219 |
last = key; |
| 220 |
if (sourceMode != SYNC) { |
| 221 |
upstream.request(1); |
| 222 |
} |
| 223 |
} |
| 224 |
} |
| 225 |
|
| 226 |
} |
| 227 |
} |