Debian Codegraph @

src/main/java/io/reactivex/rxjava3/internal/operators/observable/ObservableRange.java

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