| 1 |
/* |
| 2 |
* Copyright (c) 2016-present, RxJava Contributors. |
| 3 |
* |
| 4 |
* Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in |
| 5 |
* compliance with the License. You may obtain a copy of the License at |
| 6 |
* |
| 7 |
* http://www.apache.org/licenses/LICENSE-2.0 |
| 8 |
* |
| 9 |
* Unless required by applicable law or agreed to in writing, software distributed under the License is |
| 10 |
* distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See |
| 11 |
* the License for the specific language governing permissions and limitations under the License. |
| 12 |
*/ |
| 13 |
|
| 14 |
package io.reactivex.rxjava3.internal.operators.observable; |
| 15 |
|
| 16 |
import java.util.concurrent.TimeUnit; |
| 17 |
import java.util.concurrent.atomic.AtomicReference; |
| 18 |
|
| 19 |
import io.reactivex.rxjava3.core.*; |
| 20 |
import io.reactivex.rxjava3.core.Scheduler.Worker; |
| 21 |
import io.reactivex.rxjava3.disposables.Disposable; |
| 22 |
import io.reactivex.rxjava3.internal.disposables.DisposableHelper; |
| 23 |
import io.reactivex.rxjava3.internal.schedulers.TrampolineScheduler; |
| 24 |
|
| 25 |
public final class ObservableInterval extends Observable<Long> { |
| 26 |
final Scheduler scheduler; |
| 27 |
final long initialDelay; |
| 28 |
final long period; |
| 29 |
final TimeUnit unit; |
| 30 |
|
| 31 |
public ObservableInterval(long initialDelay, long period, TimeUnit unit, Scheduler scheduler) { |
| 32 |
this.initialDelay = initialDelay; |
| 33 |
this.period = period; |
| 34 |
this.unit = unit; |
| 35 |
this.scheduler = scheduler; |
| 36 |
} |
| 37 |
|
| 38 |
@Override |
| 39 |
public void subscribeActual(Observer<? super Long> observer) { |
| 40 |
IntervalObserver is = new IntervalObserver(observer); |
| 41 |
observer.onSubscribe(is); |
| 42 |
|
| 43 |
Scheduler sch = scheduler; |
| 44 |
|
| 45 |
if (sch instanceof TrampolineScheduler) { |
| 46 |
Worker worker = sch.createWorker(); |
| 47 |
is.setResource(worker); |
| 48 |
worker.schedulePeriodically(is, initialDelay, period, unit); |
| 49 |
} else { |
| 50 |
Disposable d = sch.schedulePeriodicallyDirect(is, initialDelay, period, unit); |
| 51 |
is.setResource(d); |
| 52 |
} |
| 53 |
} |
| 54 |
|
| 55 |
static final class IntervalObserver |
| 56 |
extends AtomicReference<Disposable> |
| 57 |
implements Disposable, Runnable { |
| 58 |
|
| 59 |
private static final long serialVersionUID = 346773832286157679L; |
| 60 |
|
| 61 |
final Observer<? super Long> downstream; |
| 62 |
|
| 63 |
long count; |
| 64 |
|
| 65 |
IntervalObserver(Observer<? super Long> downstream) { |
| 66 |
this.downstream = downstream; |
| 67 |
} |
| 68 |
|
| 69 |
@Override |
| 70 |
public void dispose() { |
| 71 |
DisposableHelper.dispose(this); |
| 72 |
} |
| 73 |
|
| 74 |
@Override |
| 75 |
public boolean isDisposed() { |
| 76 |
return get() == DisposableHelper.DISPOSED; |
| 77 |
} |
| 78 |
|
| 79 |
@Override |
| 80 |
public void run() { |
| 81 |
if (get() != DisposableHelper.DISPOSED) { |
| 82 |
downstream.onNext(count++); |
| 83 |
} |
| 84 |
} |
| 85 |
|
| 86 |
public void setResource(Disposable d) { |
| 87 |
DisposableHelper.setOnce(this, d); |
| 88 |
} |
| 89 |
} |
| 90 |
} |