22
22
import static rx .Observable .empty ;
23
23
import static rx .Observable .from ;
24
24
25
+ import java .util .Comparator ;
26
+
25
27
import org .junit .Before ;
26
28
import org .junit .Test ;
27
29
import org .mockito .InOrder ;
41
43
*/
42
44
public final class OperationDistinctUntilChanged {
43
45
46
+ public static <T > OnSubscribeFunc <T > distinctUntilChanged (Observable <? extends T > source , Comparator <T > equalityComparator ) {
47
+ return new DistinctUntilChanged <T , T >(source , Functions .<T >identity (), equalityComparator );
48
+ }
49
+
44
50
/**
45
51
* Returns an Observable that emits all sequentially distinct items emitted by the source.
46
52
* @param source
47
53
* The source Observable to emit the sequentially distinct items for.
48
54
* @return A subscription function for creating the target Observable.
49
55
*/
50
56
public static <T , U > OnSubscribeFunc <T > distinctUntilChanged (Observable <? extends T > source , Func1 <? super T , ? extends U > keySelector ) {
51
- return new DistinctUntilChanged <T , U >(source , keySelector );
57
+ return new DistinctUntilChanged <T , U >(source , keySelector , new DefaultEqualityComparator < U >() );
52
58
}
53
59
54
60
/**
@@ -58,16 +64,30 @@ public static <T, U> OnSubscribeFunc<T> distinctUntilChanged(Observable<? extend
58
64
* @return A subscription function for creating the target Observable.
59
65
*/
60
66
public static <T > OnSubscribeFunc <T > distinctUntilChanged (Observable <? extends T > source ) {
61
- return new DistinctUntilChanged <T , T >(source , Functions .<T >identity ());
67
+ return new DistinctUntilChanged <T , T >(source , Functions .<T >identity (), new DefaultEqualityComparator <T >());
68
+ }
69
+
70
+ // does not define a useful ordering; it's only used for equality tests here
71
+ private static class DefaultEqualityComparator <T > implements Comparator <T > {
72
+ @ Override
73
+ public int compare (T t1 , T t2 ) {
74
+ if (t1 == null ) {
75
+ return t2 == null ? 0 : 1 ;
76
+ } else {
77
+ return t1 .equals (t2 ) ? 0 : 1 ;
78
+ }
79
+ }
62
80
}
63
81
64
82
private static class DistinctUntilChanged <T , U > implements OnSubscribeFunc <T > {
65
83
private final Observable <? extends T > source ;
66
84
private final Func1 <? super T , ? extends U > keySelector ;
85
+ private final Comparator <U > equalityComparator ;
67
86
68
- private DistinctUntilChanged (Observable <? extends T > source , Func1 <? super T , ? extends U > keySelector ) {
87
+ private DistinctUntilChanged (Observable <? extends T > source , Func1 <? super T , ? extends U > keySelector , Comparator < U > equalityComparator ) {
69
88
this .source = source ;
70
89
this .keySelector = keySelector ;
90
+ this .equalityComparator = equalityComparator ;
71
91
}
72
92
73
93
@ Override
@@ -95,16 +115,8 @@ public void onNext(T next) {
95
115
if (!hasEmitted ) {
96
116
hasEmitted = true ;
97
117
observer .onNext (next );
98
- } else {
99
- if (lastKey == null ) {
100
- if (nextKey != null ) {
101
- observer .onNext (next );
102
- }
103
- } else {
104
- if (!lastKey .equals (nextKey )) {
105
- observer .onNext (next );
106
- }
107
- }
118
+ } else if (equalityComparator .compare (lastKey , nextKey ) != 0 ) {
119
+ observer .onNext (next );
108
120
}
109
121
} catch (Throwable t ) {
110
122
// keySelector is a user function, may throw something
0 commit comments