|
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 |
| - */ |
| 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 | 16 | package rx.operators;
|
17 | 17 |
|
18 | 18 | import java.util.concurrent.TimeUnit;
|
19 |
| - |
20 | 19 | import rx.Observable.OnSubscribeFunc;
|
21 | 20 | import rx.Observer;
|
22 | 21 | import rx.Scheduler;
|
23 | 22 | import rx.Subscription;
|
24 |
| -import rx.schedulers.Schedulers; |
25 |
| -import rx.subscriptions.Subscriptions; |
26 | 23 | import rx.util.functions.Action0;
|
27 | 24 |
|
| 25 | +/** |
| 26 | + * Operation Timer with several overloads. |
| 27 | + * |
| 28 | + * @see <a href='http://msdn.microsoft.com/en-us/library/system.reactive.linq.observable.timer.aspx'>MSDN Observable.Timer</a> |
| 29 | + */ |
28 | 30 | public final class OperationTimer {
|
29 |
| - |
30 |
| - public static OnSubscribeFunc<Void> timer(long interval, TimeUnit unit) { |
31 |
| - return timer(interval, unit, Schedulers.threadPoolForComputation()); |
32 |
| - } |
33 |
| - |
34 |
| - public static OnSubscribeFunc<Void> timer(final long delay, final TimeUnit unit, final Scheduler scheduler) { |
35 |
| - return new OnSubscribeFunc<Void>() { |
36 |
| - @Override |
37 |
| - public Subscription onSubscribe(Observer<? super Void> observer) { |
38 |
| - return new Timer(delay, unit, scheduler, observer).start(); |
39 |
| - } |
40 |
| - }; |
41 |
| - } |
42 |
| - |
43 |
| - private static class Timer { |
44 |
| - private final long period; |
45 |
| - private final TimeUnit unit; |
| 31 | + private OperationTimer() { throw new IllegalStateException("No instances!"); } |
| 32 | + |
| 33 | + /** |
| 34 | + * Emit a single 0L after the specified time elapses. |
| 35 | + */ |
| 36 | + public static class TimerOnce implements OnSubscribeFunc<Long> { |
46 | 37 | private final Scheduler scheduler;
|
47 |
| - private final Observer<? super Void> observer; |
48 |
| - |
49 |
| - private Timer(long period, TimeUnit unit, Scheduler scheduler, Observer<? super Void> observer) { |
50 |
| - this.period = period; |
51 |
| - this.unit = unit; |
| 38 | + private final long dueTime; |
| 39 | + private final TimeUnit dueUnit; |
| 40 | + public TimerOnce(long dueTime, TimeUnit unit, Scheduler scheduler) { |
52 | 41 | this.scheduler = scheduler;
|
53 |
| - this.observer = observer; |
| 42 | + this.dueTime = dueTime; |
| 43 | + this.dueUnit = unit; |
54 | 44 | }
|
55 |
| - |
56 |
| - public Subscription start() { |
57 |
| - final Subscription s = scheduler.schedule(new Action0() { |
| 45 | + |
| 46 | + @Override |
| 47 | + public Subscription onSubscribe(final Observer<? super Long> t1) { |
| 48 | + return scheduler.schedule(new Action0() { |
58 | 49 | @Override
|
59 | 50 | public void call() {
|
60 |
| - observer.onNext(null); |
61 |
| - observer.onCompleted(); |
| 51 | + t1.onNext(0L); |
| 52 | + t1.onCompleted(); |
62 | 53 | }
|
63 |
| - }, period, unit); |
64 |
| - |
65 |
| - return Subscriptions.create(new Action0() { |
| 54 | + |
| 55 | + }, dueTime, dueUnit); |
| 56 | + } |
| 57 | + } |
| 58 | + /** |
| 59 | + * Emit 0L after the initial period and ever increasing number after each period. |
| 60 | + */ |
| 61 | + public static class TimerPeriodically implements OnSubscribeFunc<Long> { |
| 62 | + private final Scheduler scheduler; |
| 63 | + private final long initialDelay; |
| 64 | + private final long period; |
| 65 | + private final TimeUnit unit; |
| 66 | + public TimerPeriodically(long initialDelay, long period, TimeUnit unit, Scheduler scheduler) { |
| 67 | + this.scheduler = scheduler; |
| 68 | + this.initialDelay = initialDelay; |
| 69 | + this.period = period; |
| 70 | + this.unit = unit; |
| 71 | + } |
| 72 | + |
| 73 | + @Override |
| 74 | + public Subscription onSubscribe(final Observer<? super Long> t1) { |
| 75 | + return scheduler.schedulePeriodically(new Action0() { |
| 76 | + long count; |
66 | 77 | @Override
|
67 | 78 | public void call() {
|
68 |
| - s.unsubscribe(); |
| 79 | + t1.onNext(count++); |
69 | 80 | }
|
70 |
| - }); |
| 81 | + }, |
| 82 | + initialDelay, period, unit |
| 83 | + ); |
71 | 84 | }
|
72 | 85 | }
|
73 |
| - |
74 | 86 | }
|
0 commit comments