Skip to content

Commit 99ceebe

Browse files
rx.concurrent -> rx.schedulers for Android and Swing modules
1 parent abff40f commit 99ceebe

File tree

8 files changed

+490
-406
lines changed

8 files changed

+490
-406
lines changed
Lines changed: 10 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -1,51 +1,24 @@
1-
/**
2-
* Copyright 2013 Netflix, Inc.
3-
*
4-
* Licensed under the Apache License, Version 2.0 (the "License");
5-
* you may not use this file except in compliance with the License.
6-
* You may obtain a copy of the License at
7-
*
8-
* http://www.apache.org/licenses/LICENSE-2.0
9-
*
10-
* Unless required by applicable law or agreed to in writing, software
11-
* distributed under the License is distributed on an "AS IS" BASIS,
12-
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13-
* See the License for the specific language governing permissions and
14-
* limitations under the License.
15-
*/
161
package rx.android.concurrency;
172

18-
import android.os.Handler;
19-
import android.os.Looper;
203
import rx.Scheduler;
4+
import android.os.Handler;
215

226
/**
23-
* Schedulers that have Android specific functionality
7+
* Deprecated. Package changed from rx.android.concurrency to rx.android.schedulers.
8+
*
9+
* @deprecated Use {@link rx.android.schedulers.AndroidSchedulers} instead. This will be removed before 1.0 release.
2410
*/
11+
@Deprecated
2512
public class AndroidSchedulers {
2613

27-
private static final Scheduler MAIN_THREAD_SCHEDULER =
28-
new HandlerThreadScheduler(new Handler(Looper.getMainLooper()));
29-
30-
private AndroidSchedulers(){
31-
32-
}
33-
34-
/**
35-
* {@link Scheduler} which uses the provided {@link Handler} to execute an action
36-
* @param handler The handler that will be used when executing the action
37-
* @return A handler based scheduler
38-
*/
14+
@Deprecated
3915
public static Scheduler handlerThread(final Handler handler) {
40-
return new HandlerThreadScheduler(handler);
16+
return rx.android.schedulers.AndroidSchedulers.handlerThread(handler);
4117
}
4218

43-
/**
44-
* {@link Scheduler} which will execute an action on the main Android UI thread.
45-
*
46-
* @return A Main {@link Looper} based scheduler
47-
*/
19+
@Deprecated
4820
public static Scheduler mainThread() {
49-
return MAIN_THREAD_SCHEDULER;
21+
return rx.android.schedulers.AndroidSchedulers.mainThread();
5022
}
23+
5124
}
Lines changed: 6 additions & 121 deletions
Original file line numberDiff line numberDiff line change
@@ -1,132 +1,17 @@
1-
/**
2-
* Copyright 2013 Netflix, Inc.
3-
*
4-
* Licensed under the Apache License, Version 2.0 (the "License");
5-
* you may not use this file except in compliance with the License.
6-
* You may obtain a copy of the License at
7-
*
8-
* http://www.apache.org/licenses/LICENSE-2.0
9-
*
10-
* Unless required by applicable law or agreed to in writing, software
11-
* distributed under the License is distributed on an "AS IS" BASIS,
12-
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13-
* See the License for the specific language governing permissions and
14-
* limitations under the License.
15-
*/
161
package rx.android.concurrency;
172

183
import android.os.Handler;
194

20-
import org.junit.Test;
21-
import org.junit.runner.RunWith;
22-
import org.mockito.ArgumentCaptor;
23-
import org.robolectric.RobolectricTestRunner;
24-
import org.robolectric.annotation.Config;
25-
26-
import rx.Scheduler;
27-
import rx.Subscription;
28-
import rx.operators.SafeObservableSubscription;
29-
import rx.util.functions.Func2;
30-
31-
import java.util.concurrent.TimeUnit;
32-
33-
import static org.mockito.Matchers.eq;
34-
import static org.mockito.Mockito.mock;
35-
import static org.mockito.Mockito.verify;
36-
375
/**
38-
* Schedules actions to run on an Android Handler thread.
6+
* Deprecated. Package changed from rx.android.concurrency to rx.android.schedulers.
7+
*
8+
* @deprecated Use {@link rx.android.schedulers.HandlerThreadScheduler} instead. This will be removed before 1.0 release.
399
*/
40-
public class HandlerThreadScheduler extends Scheduler {
10+
@Deprecated
11+
public class HandlerThreadScheduler extends rx.android.schedulers.HandlerThreadScheduler {
4112

42-
private final Handler handler;
43-
44-
/**
45-
* Constructs a {@link HandlerThreadScheduler} using the given {@link Handler}
46-
* @param handler {@link Handler} to use when scheduling actions
47-
*/
4813
public HandlerThreadScheduler(Handler handler) {
49-
this.handler = handler;
50-
}
51-
52-
/**
53-
* Calls {@link HandlerThreadScheduler#schedule(Object, rx.util.functions.Func2, long, java.util.concurrent.TimeUnit)}
54-
* with a delay of zero milliseconds.
55-
*
56-
* See {@link #schedule(Object, rx.util.functions.Func2, long, java.util.concurrent.TimeUnit)}
57-
*/
58-
@Override
59-
public <T> Subscription schedule(final T state, final Func2<? super Scheduler, ? super T, ? extends Subscription> action) {
60-
return schedule(state, action, 0L, TimeUnit.MILLISECONDS);
61-
}
62-
63-
/**
64-
* Calls {@link Handler#postDelayed(Runnable, long)} with a runnable that executes the given action.
65-
* @param state
66-
* State to pass into the action.
67-
* @param action
68-
* Action to schedule.
69-
* @param delayTime
70-
* Time the action is to be delayed before executing.
71-
* @param unit
72-
* Time unit of the delay time.
73-
* @return A Subscription from which one can unsubscribe from.
74-
*/
75-
@Override
76-
public <T> Subscription schedule(final T state, final Func2<? super Scheduler, ? super T, ? extends Subscription> action, long delayTime, TimeUnit unit) {
77-
final SafeObservableSubscription subscription = new SafeObservableSubscription();
78-
final Scheduler _scheduler = this;
79-
handler.postDelayed(new Runnable() {
80-
@Override
81-
public void run() {
82-
subscription.wrap(action.call(_scheduler, state));
83-
}
84-
}, unit.toMillis(delayTime));
85-
return subscription;
14+
super(handler);
8615
}
8716

88-
@RunWith(RobolectricTestRunner.class)
89-
@Config(manifest=Config.NONE)
90-
public static final class UnitTest {
91-
92-
@Test
93-
public void shouldScheduleImmediateActionOnHandlerThread() {
94-
final Handler handler = mock(Handler.class);
95-
final Object state = new Object();
96-
@SuppressWarnings("unchecked")
97-
final Func2<Scheduler, Object, Subscription> action = mock(Func2.class);
98-
99-
Scheduler scheduler = new HandlerThreadScheduler(handler);
100-
scheduler.schedule(state, action);
101-
102-
// verify that we post to the given Handler
103-
ArgumentCaptor<Runnable> runnable = ArgumentCaptor.forClass(Runnable.class);
104-
verify(handler).postDelayed(runnable.capture(), eq(0L));
105-
106-
// verify that the given handler delegates to our action
107-
runnable.getValue().run();
108-
verify(action).call(scheduler, state);
109-
}
110-
111-
@Test
112-
public void shouldScheduleDelayedActionOnHandlerThread() {
113-
final Handler handler = mock(Handler.class);
114-
final Object state = new Object();
115-
@SuppressWarnings("unchecked")
116-
final Func2<Scheduler, Object, Subscription> action = mock(Func2.class);
117-
118-
Scheduler scheduler = new HandlerThreadScheduler(handler);
119-
scheduler.schedule(state, action, 1L, TimeUnit.SECONDS);
120-
121-
// verify that we post to the given Handler
122-
ArgumentCaptor<Runnable> runnable = ArgumentCaptor.forClass(Runnable.class);
123-
verify(handler).postDelayed(runnable.capture(), eq(1000L));
124-
125-
// verify that the given handler delegates to our action
126-
runnable.getValue().run();
127-
verify(action).call(scheduler, state);
128-
}
129-
}
13017
}
131-
132-
Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
/**
2+
* Copyright 2013 Netflix, Inc.
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
package rx.android.schedulers;
17+
18+
import android.os.Handler;
19+
import android.os.Looper;
20+
import rx.Scheduler;
21+
22+
/**
23+
* Schedulers that have Android specific functionality
24+
*/
25+
public class AndroidSchedulers {
26+
27+
private static final Scheduler MAIN_THREAD_SCHEDULER =
28+
new HandlerThreadScheduler(new Handler(Looper.getMainLooper()));
29+
30+
private AndroidSchedulers(){
31+
32+
}
33+
34+
/**
35+
* {@link Scheduler} which uses the provided {@link Handler} to execute an action
36+
* @param handler The handler that will be used when executing the action
37+
* @return A handler based scheduler
38+
*/
39+
public static Scheduler handlerThread(final Handler handler) {
40+
return new HandlerThreadScheduler(handler);
41+
}
42+
43+
/**
44+
* {@link Scheduler} which will execute an action on the main Android UI thread.
45+
*
46+
* @return A Main {@link Looper} based scheduler
47+
*/
48+
public static Scheduler mainThread() {
49+
return MAIN_THREAD_SCHEDULER;
50+
}
51+
}
Lines changed: 132 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,132 @@
1+
/**
2+
* Copyright 2013 Netflix, Inc.
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
package rx.android.schedulers;
17+
18+
import android.os.Handler;
19+
20+
import org.junit.Test;
21+
import org.junit.runner.RunWith;
22+
import org.mockito.ArgumentCaptor;
23+
import org.robolectric.RobolectricTestRunner;
24+
import org.robolectric.annotation.Config;
25+
26+
import rx.Scheduler;
27+
import rx.Subscription;
28+
import rx.operators.SafeObservableSubscription;
29+
import rx.util.functions.Func2;
30+
31+
import java.util.concurrent.TimeUnit;
32+
33+
import static org.mockito.Matchers.eq;
34+
import static org.mockito.Mockito.mock;
35+
import static org.mockito.Mockito.verify;
36+
37+
/**
38+
* Schedules actions to run on an Android Handler thread.
39+
*/
40+
public class HandlerThreadScheduler extends Scheduler {
41+
42+
private final Handler handler;
43+
44+
/**
45+
* Constructs a {@link HandlerThreadScheduler} using the given {@link Handler}
46+
* @param handler {@link Handler} to use when scheduling actions
47+
*/
48+
public HandlerThreadScheduler(Handler handler) {
49+
this.handler = handler;
50+
}
51+
52+
/**
53+
* Calls {@link HandlerThreadScheduler#schedule(Object, rx.util.functions.Func2, long, java.util.concurrent.TimeUnit)}
54+
* with a delay of zero milliseconds.
55+
*
56+
* See {@link #schedule(Object, rx.util.functions.Func2, long, java.util.concurrent.TimeUnit)}
57+
*/
58+
@Override
59+
public <T> Subscription schedule(final T state, final Func2<? super Scheduler, ? super T, ? extends Subscription> action) {
60+
return schedule(state, action, 0L, TimeUnit.MILLISECONDS);
61+
}
62+
63+
/**
64+
* Calls {@link Handler#postDelayed(Runnable, long)} with a runnable that executes the given action.
65+
* @param state
66+
* State to pass into the action.
67+
* @param action
68+
* Action to schedule.
69+
* @param delayTime
70+
* Time the action is to be delayed before executing.
71+
* @param unit
72+
* Time unit of the delay time.
73+
* @return A Subscription from which one can unsubscribe from.
74+
*/
75+
@Override
76+
public <T> Subscription schedule(final T state, final Func2<? super Scheduler, ? super T, ? extends Subscription> action, long delayTime, TimeUnit unit) {
77+
final SafeObservableSubscription subscription = new SafeObservableSubscription();
78+
final Scheduler _scheduler = this;
79+
handler.postDelayed(new Runnable() {
80+
@Override
81+
public void run() {
82+
subscription.wrap(action.call(_scheduler, state));
83+
}
84+
}, unit.toMillis(delayTime));
85+
return subscription;
86+
}
87+
88+
@RunWith(RobolectricTestRunner.class)
89+
@Config(manifest=Config.NONE)
90+
public static final class UnitTest {
91+
92+
@Test
93+
public void shouldScheduleImmediateActionOnHandlerThread() {
94+
final Handler handler = mock(Handler.class);
95+
final Object state = new Object();
96+
@SuppressWarnings("unchecked")
97+
final Func2<Scheduler, Object, Subscription> action = mock(Func2.class);
98+
99+
Scheduler scheduler = new HandlerThreadScheduler(handler);
100+
scheduler.schedule(state, action);
101+
102+
// verify that we post to the given Handler
103+
ArgumentCaptor<Runnable> runnable = ArgumentCaptor.forClass(Runnable.class);
104+
verify(handler).postDelayed(runnable.capture(), eq(0L));
105+
106+
// verify that the given handler delegates to our action
107+
runnable.getValue().run();
108+
verify(action).call(scheduler, state);
109+
}
110+
111+
@Test
112+
public void shouldScheduleDelayedActionOnHandlerThread() {
113+
final Handler handler = mock(Handler.class);
114+
final Object state = new Object();
115+
@SuppressWarnings("unchecked")
116+
final Func2<Scheduler, Object, Subscription> action = mock(Func2.class);
117+
118+
Scheduler scheduler = new HandlerThreadScheduler(handler);
119+
scheduler.schedule(state, action, 1L, TimeUnit.SECONDS);
120+
121+
// verify that we post to the given Handler
122+
ArgumentCaptor<Runnable> runnable = ArgumentCaptor.forClass(Runnable.class);
123+
verify(handler).postDelayed(runnable.capture(), eq(1000L));
124+
125+
// verify that the given handler delegates to our action
126+
runnable.getValue().run();
127+
verify(action).call(scheduler, state);
128+
}
129+
}
130+
}
131+
132+

0 commit comments

Comments
 (0)