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