Debian Codegraph @

src/main/java/io/reactivex/rxjava3/internal/operators/observable/ObservableFromArray.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.BasicQueueDisposable;
19
20 import java.util.Objects;
21
22 public final class ObservableFromArray<T> extends Observable<T> {
23 final T[] array;
24 public ObservableFromArray(T[] array) {
25 this.array = array;
26 }
27
28 @Override
29 public void subscribeActual(Observer<? super T> observer) {
30 FromArrayDisposable<T> d = new FromArrayDisposable<>(observer, array);
31
32 observer.onSubscribe(d);
33
34 if (d.fusionMode) {
35 return;
36 }
37
38 d.run();
39 }
40
41 static final class FromArrayDisposable<T> extends BasicQueueDisposable<T> {
42
43 final Observer<? super T> downstream;
44
45 final T[] array;
46
47 int index;
48
49 boolean fusionMode;
50
51 volatile boolean disposed;
52
53 FromArrayDisposable(Observer<? super T> actual, T[] array) {
54 this.downstream = actual;
55 this.array = array;
56 }
57
58 @Override
59 public int requestFusion(int mode) {
60 if ((mode & SYNC) != 0) {
61 fusionMode = true;
62 return SYNC;
63 }
64 return NONE;
65 }
66
67 @Nullable
68 @Override
69 public T poll() {
70 int i = index;
71 T[] a = array;
72 if (i != a.length) {
73 index = i + 1;
74 return Objects.requireNonNull(a[i], "The array element is null");
75 }
76 return null;
77 }
78
79 @Override
80 public boolean isEmpty() {
81 return index == array.length;
82 }
83
84 @Override
85 public void clear() {
86 index = array.length;
87 }
88
89 @Override
90 public void dispose() {
91 disposed = true;
92 }
93
94 @Override
95 public boolean isDisposed() {
96 return disposed;
97 }
98
99 void run() {
100 T[] a = array;
101 int n = a.length;
102
103 for (int i = 0; i < n && !isDisposed(); i++) {
104 T value = a[i];
105 if (value == null) {
106 downstream.onError(new NullPointerException("The element at index " + i + " is null"));
107 return;
108 }
109 downstream.onNext(value);
110 }
111 if (!isDisposed()) {
112 downstream.onComplete();
113 }
114 }
115 }
116 }