Skip to content

Commit 35cf457

Browse files
committed
Reduce duplication by making "schedule now" the special case
Forwards to "schedule later" with delay of 0 now.
1 parent 0959aa5 commit 35cf457

File tree

1 file changed

+9
-40
lines changed

1 file changed

+9
-40
lines changed

rxjava-contrib/rxjava-android/src/main/java/rx/concurrency/HandlerThreadScheduler.java

Lines changed: 9 additions & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -12,11 +12,8 @@
1212

1313
import java.util.concurrent.TimeUnit;
1414

15-
import static org.mockito.Matchers.any;
16-
import static org.mockito.Matchers.anyLong;
1715
import static org.mockito.Matchers.eq;
1816
import static org.mockito.Mockito.mock;
19-
import static org.mockito.Mockito.never;
2017
import static org.mockito.Mockito.verify;
2118

2219
/**
@@ -32,40 +29,27 @@ public HandlerThreadScheduler(Handler handler) {
3229

3330
@Override
3431
public <T> Subscription schedule(final T state, final Func2<Scheduler, T, Subscription> action) {
32+
return schedule(state, action, 0L, TimeUnit.MILLISECONDS);
33+
}
34+
35+
@Override
36+
public <T> Subscription schedule(final T state, final Func2<Scheduler, T, Subscription> action, long delayTime, TimeUnit unit) {
3537
final AtomicObservableSubscription subscription = new AtomicObservableSubscription();
3638
final Scheduler _scheduler = this;
37-
38-
handler.post(new Runnable() {
39+
handler.postDelayed(new Runnable() {
3940
@Override
4041
public void run() {
4142
subscription.wrap(action.call(_scheduler, state));
4243
}
43-
});
44+
}, unit.toMillis(delayTime));
4445
return subscription;
4546
}
4647

47-
@Override
48-
public <T> Subscription schedule(final T state, final Func2<Scheduler, T, Subscription> action, long delayTime, TimeUnit unit) {
49-
if (delayTime == 0) {
50-
return schedule(state, action);
51-
} else {
52-
final AtomicObservableSubscription subscription = new AtomicObservableSubscription();
53-
final Scheduler _scheduler = this;
54-
handler.postDelayed(new Runnable() {
55-
@Override
56-
public void run() {
57-
subscription.wrap(action.call(_scheduler, state));
58-
}
59-
}, unit.toMillis(delayTime));
60-
return subscription;
61-
}
62-
}
63-
6448
@RunWith(AndroidTestRunner.class)
6549
public static final class UnitTest {
6650

6751
@Test
68-
public void shouldScheduleActionOnHandlerThread() {
52+
public void shouldScheduleImmediateActionOnHandlerThread() {
6953
final Handler handler = mock(Handler.class);
7054
final Object state = new Object();
7155
final Func2<Scheduler, Object, Subscription> action = mock(Func2.class);
@@ -75,7 +59,7 @@ public void shouldScheduleActionOnHandlerThread() {
7559

7660
// verify that we post to the given Handler
7761
ArgumentCaptor<Runnable> runnable = ArgumentCaptor.forClass(Runnable.class);
78-
verify(handler).post(runnable.capture());
62+
verify(handler).postDelayed(runnable.capture(), eq(0L));
7963

8064
// verify that the given handler delegates to our action
8165
runnable.getValue().run();
@@ -99,21 +83,6 @@ public void shouldScheduleDelayedActionOnHandlerThread() {
9983
runnable.getValue().run();
10084
verify(action).call(scheduler, state);
10185
}
102-
103-
@Test
104-
public void scheduleDelayedActionShouldForwardToNormalPostIfDelayIsZero() {
105-
final Handler handler = mock(Handler.class);
106-
final Object state = new Object();
107-
final Func2<Scheduler, Object, Subscription> action = mock(Func2.class);
108-
109-
Scheduler scheduler = new HandlerThreadScheduler(handler);
110-
scheduler.schedule(state, action, 0L, TimeUnit.SECONDS);
111-
112-
// verify that we post to the given Handler
113-
verify(handler).post(any(Runnable.class));
114-
verify(handler, never()).postDelayed(any(Runnable.class), anyLong());
115-
}
116-
11786
}
11887
}
11988

0 commit comments

Comments
 (0)