| 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 ObservableIntervalRange extends Observable<Long> { |
| 26 |
final Scheduler scheduler; |
| 27 |
final long start; |
| 28 |
final long end; |
| 29 |
final long initialDelay; |
| 30 |
final long period; |
| 31 |
final TimeUnit unit; |
| 32 |
|
| 33 |
public ObservableIntervalRange(long start, long end, long initialDelay, long period, TimeUnit unit, Scheduler scheduler) { |
| 34 |
this.initialDelay = initialDelay; |
| 35 |
this.period = period; |
| 36 |
this.unit = unit; |
| 37 |
this.scheduler = scheduler; |
| 38 |
this.start = start; |
| 39 |
this.end = end; |
| 40 |
} |
| 41 |
|
| 42 |
@Override |
| 43 |
public void subscribeActual(Observer<? super Long> observer) { |
| 44 |
IntervalRangeObserver is = new IntervalRangeObserver(observer, start, end); |
| 45 |
observer.onSubscribe(is); |
| 46 |
|
| 47 |
Scheduler sch = scheduler; |
| 48 |
|
| 49 |
if (sch instanceof TrampolineScheduler) { |
| 50 |
Worker worker = sch.createWorker(); |
| 51 |
is.setResource(worker); |
| 52 |
worker.schedulePeriodically(is, initialDelay, period, unit); |
| 53 |
} else { |
| 54 |
Disposable d = sch.schedulePeriodicallyDirect(is, initialDelay, period, unit); |
| 55 |
is.setResource(d); |
| 56 |
} |
| 57 |
} |
| 58 |
|
| 59 |
static final class IntervalRangeObserver |
| 60 |
extends AtomicReference<Disposable> |
| 61 |
implements Disposable, Runnable { |
| 62 |
|
| 63 |
private static final long serialVersionUID = 1891866368734007884L; |
| 64 |
|
| 65 |
final Observer<? super Long> downstream; |
| 66 |
final long end; |
| 67 |
|
| 68 |
long count; |
| 69 |
|
| 70 |
IntervalRangeObserver(Observer<? super Long> actual, long start, long end) { |
| 71 |
this.downstream = actual; |
| 72 |
this.count = start; |
| 73 |
this.end = end; |
| 74 |
} |
| 75 |
|
| 76 |
@Override |
| 77 |
public void dispose() { |
| 78 |
DisposableHelper.dispose(this); |
| 79 |
} |
| 80 |
|
| 81 |
@Override |
| 82 |
public boolean isDisposed() { |
| 83 |
return get() == DisposableHelper.DISPOSED; |
| 84 |
} |
| 85 |
|
| 86 |
@Override |
| 87 |
public void run() { |
| 88 |
if (!isDisposed()) { |
| 89 |
long c = count; |
| 90 |
downstream.onNext(c); |
| 91 |
|
| 92 |
if (c == end) { |
| 93 |
if (!isDisposed()) { |
| 94 |
downstream.onComplete(); |
| 95 |
} |
| 96 |
DisposableHelper.dispose(this); |
| 97 |
return; |
| 98 |
} |
| 99 |
|
| 100 |
count = c + 1; |
| 101 |
|
| 102 |
} |
| 103 |
} |
| 104 |
|
| 105 |
public void setResource(Disposable d) { |
| 106 |
DisposableHelper.setOnce(this, d); |
| 107 |
} |
| 108 |
} |
| 109 |
} |