43
43
*/
44
44
public final class OperationDistinctUntilChanged {
45
45
46
+ /**
47
+ * Returns an Observable that emits all sequentially distinct items emitted by the source.
48
+ * @param source
49
+ * The source Observable to emit the sequentially distinct items for.
50
+ * @param equalityComparator
51
+ * The comparator to use for deciding whether to consider two items as equal or not.
52
+ * @return A subscription function for creating the target Observable.
53
+ */
46
54
public static <T > OnSubscribeFunc <T > distinctUntilChanged (Observable <? extends T > source , Comparator <T > equalityComparator ) {
47
55
return new DistinctUntilChanged <T , T >(source , Functions .<T >identity (), equalityComparator );
48
56
}
@@ -51,6 +59,22 @@ public static <T> OnSubscribeFunc<T> distinctUntilChanged(Observable<? extends T
51
59
* Returns an Observable that emits all sequentially distinct items emitted by the source.
52
60
* @param source
53
61
* The source Observable to emit the sequentially distinct items for.
62
+ * @param keySelector
63
+ * The function to select the key to use for the equality checks.
64
+ * @param equalityComparator
65
+ * The comparator to use for deciding whether to consider the two item keys as equal or not.
66
+ * @return A subscription function for creating the target Observable.
67
+ */
68
+ public static <T , U > OnSubscribeFunc <T > distinctUntilChanged (Observable <? extends T > source , Func1 <? super T , ? extends U > keySelector , Comparator <U > equalityComparator ) {
69
+ return new DistinctUntilChanged <T , U >(source , keySelector , equalityComparator );
70
+ }
71
+
72
+ /**
73
+ * Returns an Observable that emits all sequentially distinct items emitted by the source.
74
+ * @param source
75
+ * The source Observable to emit the sequentially distinct items for.
76
+ * @param keySelector
77
+ * The function to select the key to use for the equality checks.
54
78
* @return A subscription function for creating the target Observable.
55
79
*/
56
80
public static <T , U > OnSubscribeFunc <T > distinctUntilChanged (Observable <? extends T > source , Func1 <? super T , ? extends U > keySelector ) {
@@ -142,6 +166,9 @@ public static class UnitTest {
142
166
final Func1 <String , String > TO_UPPER_WITH_EXCEPTION = new Func1 <String , String >() {
143
167
@ Override
144
168
public String call (String s ) {
169
+ if (s .equals ("x" )) {
170
+ return "xx" ;
171
+ }
145
172
return s .toUpperCase ();
146
173
}
147
174
};
@@ -254,5 +281,19 @@ public void testDistinctUntilChangedWithComparator() {
254
281
inOrder .verify (w , never ()).onNext (anyString ());
255
282
verify (w , never ()).onError (any (Throwable .class ));
256
283
}
284
+
285
+ @ Test
286
+ public void testDistinctUntilChangedWithComparatorAndKeySelector () {
287
+ Observable <String > src = from ("a" , "b" , "x" , "aa" , "bb" , "c" , "ddd" );
288
+ create (distinctUntilChanged (src , TO_UPPER_WITH_EXCEPTION , COMPARE_LENGTH )).subscribe (w );
289
+ InOrder inOrder = inOrder (w );
290
+ inOrder .verify (w , times (1 )).onNext ("a" );
291
+ inOrder .verify (w , times (1 )).onNext ("x" );
292
+ inOrder .verify (w , times (1 )).onNext ("c" );
293
+ inOrder .verify (w , times (1 )).onNext ("ddd" );
294
+ inOrder .verify (w , times (1 )).onCompleted ();
295
+ inOrder .verify (w , never ()).onNext (anyString ());
296
+ verify (w , never ()).onError (any (Throwable .class ));
297
+ }
257
298
}
258
299
}
0 commit comments