4343 */
4444public final class OperationDistinctUntilChanged {
4545
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+ */
4654 public static <T > OnSubscribeFunc <T > distinctUntilChanged (Observable <? extends T > source , Comparator <T > equalityComparator ) {
4755 return new DistinctUntilChanged <T , T >(source , Functions .<T >identity (), equalityComparator );
4856 }
@@ -51,6 +59,22 @@ public static <T> OnSubscribeFunc<T> distinctUntilChanged(Observable<? extends T
5159 * Returns an Observable that emits all sequentially distinct items emitted by the source.
5260 * @param source
5361 * 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.
5478 * @return A subscription function for creating the target Observable.
5579 */
5680 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 {
142166 final Func1 <String , String > TO_UPPER_WITH_EXCEPTION = new Func1 <String , String >() {
143167 @ Override
144168 public String call (String s ) {
169+ if (s .equals ("x" )) {
170+ return "xx" ;
171+ }
145172 return s .toUpperCase ();
146173 }
147174 };
@@ -254,5 +281,19 @@ public void testDistinctUntilChangedWithComparator() {
254281 inOrder .verify (w , never ()).onNext (anyString ());
255282 verify (w , never ()).onError (any (Throwable .class ));
256283 }
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+ }
257298 }
258299}
0 commit comments