| 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.jdk8; |
| 15 |
|
| 16 |
import java.util.*; |
| 17 |
import java.util.stream.Stream; |
| 18 |
|
| 19 |
import io.reactivex.rxjava3.annotations.*; |
| 20 |
import io.reactivex.rxjava3.core.Observable; |
| 21 |
import io.reactivex.rxjava3.core.Observer; |
| 22 |
import io.reactivex.rxjava3.exceptions.Exceptions; |
| 23 |
import io.reactivex.rxjava3.internal.disposables.EmptyDisposable; |
| 24 |
import io.reactivex.rxjava3.operators.QueueDisposable; |
| 25 |
import io.reactivex.rxjava3.plugins.RxJavaPlugins; |
| 26 |
|
| 27 |
/** |
| 28 |
* Wraps a {@link Stream} and emits its values as an {@link Observable} sequence. |
| 29 |
* @param <T> the element type of the Stream |
| 30 |
* @since 3.0.0 |
| 31 |
*/ |
| 32 |
public final class ObservableFromStream<T> extends Observable<T> { |
| 33 |
|
| 34 |
final Stream<T> stream; |
| 35 |
|
| 36 |
public ObservableFromStream(Stream<T> stream) { |
| 37 |
this.stream = stream; |
| 38 |
} |
| 39 |
|
| 40 |
@Override |
| 41 |
protected void subscribeActual(Observer<? super T> observer) { |
| 42 |
subscribeStream(observer, stream); |
| 43 |
} |
| 44 |
|
| 45 |
/** |
| 46 |
* Subscribes to the Stream. |
| 47 |
* @param <T> the element type of the flow |
| 48 |
* @param observer the observer to drive |
| 49 |
* @param stream the sequence to consume |
| 50 |
*/ |
| 51 |
public static <T> void subscribeStream(Observer<? super T> observer, Stream<T> stream) { |
| 52 |
Iterator<T> iterator; |
| 53 |
try { |
| 54 |
iterator = stream.iterator(); |
| 55 |
|
| 56 |
if (!iterator.hasNext()) { |
| 57 |
EmptyDisposable.complete(observer); |
| 58 |
closeSafely(stream); |
| 59 |
return; |
| 60 |
} |
| 61 |
} catch (Throwable ex) { |
| 62 |
Exceptions.throwIfFatal(ex); |
| 63 |
EmptyDisposable.error(ex, observer); |
| 64 |
closeSafely(stream); |
| 65 |
return; |
| 66 |
} |
| 67 |
|
| 68 |
StreamDisposable<T> disposable = new StreamDisposable<>(observer, iterator, stream); |
| 69 |
observer.onSubscribe(disposable); |
| 70 |
disposable.run(); |
| 71 |
} |
| 72 |
|
| 73 |
static void closeSafely(AutoCloseable c) { |
| 74 |
try { |
| 75 |
c.close(); |
| 76 |
} catch (Throwable ex) { |
| 77 |
Exceptions.throwIfFatal(ex); |
| 78 |
RxJavaPlugins.onError(ex); |
| 79 |
} |
| 80 |
} |
| 81 |
|
| 82 |
static final class StreamDisposable<T> implements QueueDisposable<T> { |
| 83 |
|
| 84 |
final Observer<? super T> downstream; |
| 85 |
|
| 86 |
Iterator<T> iterator; |
| 87 |
|
| 88 |
AutoCloseable closeable; |
| 89 |
|
| 90 |
volatile boolean disposed; |
| 91 |
|
| 92 |
boolean once; |
| 93 |
|
| 94 |
boolean outputFused; |
| 95 |
|
| 96 |
StreamDisposable(Observer<? super T> downstream, Iterator<T> iterator, AutoCloseable closeable) { |
| 97 |
this.downstream = downstream; |
| 98 |
this.iterator = iterator; |
| 99 |
this.closeable = closeable; |
| 100 |
} |
| 101 |
|
| 102 |
@Override |
| 103 |
public void dispose() { |
| 104 |
disposed = true; |
| 105 |
run(); |
| 106 |
} |
| 107 |
|
| 108 |
@Override |
| 109 |
public boolean isDisposed() { |
| 110 |
return disposed; |
| 111 |
} |
| 112 |
|
| 113 |
@Override |
| 114 |
public int requestFusion(int mode) { |
| 115 |
if ((mode & SYNC) != 0) { |
| 116 |
outputFused = true; |
| 117 |
return SYNC; |
| 118 |
} |
| 119 |
return NONE; |
| 120 |
} |
| 121 |
|
| 122 |
@Override |
| 123 |
public boolean offer(@NonNull T value) { |
| 124 |
throw new UnsupportedOperationException(); |
| 125 |
} |
| 126 |
|
| 127 |
@Override |
| 128 |
public boolean offer(@NonNull T v1, @NonNull T v2) { |
| 129 |
throw new UnsupportedOperationException(); |
| 130 |
} |
| 131 |
|
| 132 |
@Nullable |
| 133 |
@Override |
| 134 |
public T poll() { |
| 135 |
if (iterator == null) { |
| 136 |
return null; |
| 137 |
} |
| 138 |
if (!once) { |
| 139 |
once = true; |
| 140 |
} else { |
| 141 |
if (!iterator.hasNext()) { |
| 142 |
clear(); |
| 143 |
return null; |
| 144 |
} |
| 145 |
} |
| 146 |
return Objects.requireNonNull(iterator.next(), "The Stream's Iterator.next() returned a null value"); |
| 147 |
} |
| 148 |
|
| 149 |
@Override |
| 150 |
public boolean isEmpty() { |
| 151 |
Iterator<T> it = iterator; |
| 152 |
if (it != null) { |
| 153 |
if (!once || it.hasNext()) { |
| 154 |
return false; |
| 155 |
} |
| 156 |
clear(); |
| 157 |
} |
| 158 |
return true; |
| 159 |
} |
| 160 |
|
| 161 |
@Override |
| 162 |
public void clear() { |
| 163 |
iterator = null; |
| 164 |
AutoCloseable c = closeable; |
| 165 |
closeable = null; |
| 166 |
if (c != null) { |
| 167 |
closeSafely(c); |
| 168 |
} |
| 169 |
} |
| 170 |
|
| 171 |
public void run() { |
| 172 |
if (outputFused) { |
| 173 |
return; |
| 174 |
} |
| 175 |
Iterator<T> iterator = this.iterator; |
| 176 |
Observer<? super T> downstream = this.downstream; |
| 177 |
|
| 178 |
for (;;) { |
| 179 |
if (disposed) { |
| 180 |
clear(); |
| 181 |
break; |
| 182 |
} |
| 183 |
|
| 184 |
T next; |
| 185 |
try { |
| 186 |
next = Objects.requireNonNull(iterator.next(), "The Stream's Iterator.next returned a null value"); |
| 187 |
} catch (Throwable ex) { |
| 188 |
Exceptions.throwIfFatal(ex); |
| 189 |
downstream.onError(ex); |
| 190 |
disposed = true; |
| 191 |
continue; |
| 192 |
} |
| 193 |
|
| 194 |
if (disposed) { |
| 195 |
continue; |
| 196 |
} |
| 197 |
|
| 198 |
downstream.onNext(next); |
| 199 |
|
| 200 |
if (disposed) { |
| 201 |
continue; |
| 202 |
} |
| 203 |
|
| 204 |
try { |
| 205 |
if (iterator.hasNext()) { |
| 206 |
continue; |
| 207 |
} |
| 208 |
} catch (Throwable ex) { |
| 209 |
Exceptions.throwIfFatal(ex); |
| 210 |
downstream.onError(ex); |
| 211 |
disposed = true; |
| 212 |
continue; |
| 213 |
} |
| 214 |
|
| 215 |
downstream.onComplete(); |
| 216 |
disposed = true; |
| 217 |
} |
| 218 |
} |
| 219 |
} |
| 220 |
} |