| 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 io.reactivex.rxjava3.annotations.Nullable; |
| 17 |
import io.reactivex.rxjava3.core.*; |
| 18 |
import io.reactivex.rxjava3.internal.observers.BasicIntQueueDisposable; |
| 19 |
|
| 20 |
public final class ObservableRangeLong extends Observable<Long> { |
| 21 |
private final long start; |
| 22 |
private final long count; |
| 23 |
|
| 24 |
public ObservableRangeLong(long start, long count) { |
| 25 |
this.start = start; |
| 26 |
this.count = count; |
| 27 |
} |
| 28 |
|
| 29 |
@Override |
| 30 |
protected void subscribeActual(Observer<? super Long> o) { |
| 31 |
RangeDisposable parent = new RangeDisposable(o, start, start + count); |
| 32 |
o.onSubscribe(parent); |
| 33 |
parent.run(); |
| 34 |
} |
| 35 |
|
| 36 |
static final class RangeDisposable |
| 37 |
extends BasicIntQueueDisposable<Long> { |
| 38 |
|
| 39 |
private static final long serialVersionUID = 396518478098735504L; |
| 40 |
|
| 41 |
final Observer<? super Long> downstream; |
| 42 |
|
| 43 |
final long end; |
| 44 |
|
| 45 |
long index; |
| 46 |
|
| 47 |
boolean fused; |
| 48 |
|
| 49 |
RangeDisposable(Observer<? super Long> actual, long start, long end) { |
| 50 |
this.downstream = actual; |
| 51 |
this.index = start; |
| 52 |
this.end = end; |
| 53 |
} |
| 54 |
|
| 55 |
void run() { |
| 56 |
if (fused) { |
| 57 |
return; |
| 58 |
} |
| 59 |
Observer<? super Long> actual = this.downstream; |
| 60 |
long e = end; |
| 61 |
for (long i = index; i != e && get() == 0; i++) { |
| 62 |
actual.onNext(i); |
| 63 |
} |
| 64 |
if (get() == 0) { |
| 65 |
lazySet(1); |
| 66 |
actual.onComplete(); |
| 67 |
} |
| 68 |
} |
| 69 |
|
| 70 |
@Nullable |
| 71 |
@Override |
| 72 |
public Long poll() { |
| 73 |
long i = index; |
| 74 |
if (i != end) { |
| 75 |
index = i + 1; |
| 76 |
return i; |
| 77 |
} |
| 78 |
lazySet(1); |
| 79 |
return null; |
| 80 |
} |
| 81 |
|
| 82 |
@Override |
| 83 |
public boolean isEmpty() { |
| 84 |
return index == end; |
| 85 |
} |
| 86 |
|
| 87 |
@Override |
| 88 |
public void clear() { |
| 89 |
index = end; |
| 90 |
lazySet(1); |
| 91 |
} |
| 92 |
|
| 93 |
@Override |
| 94 |
public void dispose() { |
| 95 |
set(1); |
| 96 |
} |
| 97 |
|
| 98 |
@Override |
| 99 |
public boolean isDisposed() { |
| 100 |
return get() != 0; |
| 101 |
} |
| 102 |
|
| 103 |
@Override |
| 104 |
public int requestFusion(int mode) { |
| 105 |
if ((mode & SYNC) != 0) { |
| 106 |
fused = true; |
| 107 |
return SYNC; |
| 108 |
} |
| 109 |
return NONE; |
| 110 |
} |
| 111 |
} |
| 112 |
} |