15
15
*/
16
16
package rx .operators ;
17
17
18
- import static org .mockito .Matchers .*;
19
- import static org .mockito .Mockito .*;
18
+ import static org .mockito .Matchers .any ;
19
+ import static org .mockito .Matchers .anyString ;
20
+ import static org .mockito .Mockito .inOrder ;
21
+ import static org .mockito .Mockito .mock ;
22
+ import static org .mockito .Mockito .never ;
23
+ import static org .mockito .Mockito .times ;
24
+ import static org .mockito .Mockito .verify ;
20
25
21
26
import java .util .concurrent .Executors ;
22
27
import java .util .concurrent .TimeUnit ;
27
32
import org .junit .Test ;
28
33
import org .mockito .InOrder ;
29
34
30
- import rx .Notification ;
31
35
import rx .Observable ;
32
36
import rx .Observer ;
33
37
import rx .Scheduler ;
@@ -119,25 +123,25 @@ public ThrottledObserver(Observer<T> observer, long timeout, TimeUnit unit, Sche
119
123
120
124
@ Override
121
125
public void onCompleted () {
122
- throttle (new Notification <T >());
126
+ throttle (new ThrottledOnComplete <T >(observer ));
123
127
}
124
128
125
129
@ Override
126
130
public void onError (Exception e ) {
127
- throttle (new Notification <T >(e ));
131
+ throttle (new ThrottledOnError <T >(observer , e ));
128
132
}
129
133
130
134
@ Override
131
135
public void onNext (final T args ) {
132
- throttle (new Notification <T >(args ));
136
+ throttle (new ThrottledOnNext <T >(observer , args ));
133
137
}
134
138
135
- private void throttle (final Notification < T > args ) {
139
+ private void throttle (Action0 action ) {
136
140
synchronized (subscription ) {
137
141
if (!timerHasExpired ()) {
138
142
subscription .get ().unsubscribe ();
139
143
}
140
- subscription .set (scheduler .schedule (new ThrottleAction < T >( observer , args ) , timeout , unit ));
144
+ subscription .set (scheduler .schedule (action , timeout , unit ));
141
145
}
142
146
}
143
147
@@ -149,27 +153,49 @@ private boolean timerHasExpired() {
149
153
}
150
154
}
151
155
152
- private static final class ThrottleAction <T > implements Action0 {
156
+ private static final class ThrottledOnNext <T > implements Action0 {
153
157
154
158
private final Observer <T > observer ;
155
- private final Notification < T > notification ;
159
+ private final T value ;
156
160
157
- public ThrottleAction (Observer <T > observer , Notification < T > notification ) {
161
+ public ThrottledOnNext (Observer <T > observer , T value ) {
158
162
this .observer = observer ;
159
- this .notification = notification ;
163
+ this .value = value ;
160
164
}
161
165
162
166
@ Override
163
167
public void call () {
164
- if (notification .isOnNext ()) {
165
- observer .onNext (notification .getValue ());
166
- }
167
- else if (notification .isOnError ()) {
168
- observer .onError (notification .getException ());
169
- }
170
- else if (notification .isOnCompleted ()) {
171
- observer .onCompleted ();
172
- }
168
+ observer .onNext (value );
169
+ }
170
+ }
171
+
172
+ private static final class ThrottledOnError <T > implements Action0 {
173
+
174
+ private final Observer <T > observer ;
175
+ private final Exception error ;
176
+
177
+ public ThrottledOnError (Observer <T > observer , Exception error ) {
178
+ this .observer = observer ;
179
+ this .error = error ;
180
+ }
181
+
182
+ @ Override
183
+ public void call () {
184
+ observer .onError (error );
185
+ }
186
+ }
187
+
188
+ private static final class ThrottledOnComplete <T > implements Action0 {
189
+
190
+ private final Observer <T > observer ;
191
+
192
+ public ThrottledOnComplete (Observer <T > observer ) {
193
+ this .observer = observer ;
194
+ }
195
+
196
+ @ Override
197
+ public void call () {
198
+ observer .onCompleted ();
173
199
}
174
200
}
175
201
0 commit comments