| 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.core.*; |
| 17 |
import io.reactivex.rxjava3.disposables.Disposable; |
| 18 |
import io.reactivex.rxjava3.exceptions.Exceptions; |
| 19 |
import io.reactivex.rxjava3.functions.*; |
| 20 |
import io.reactivex.rxjava3.internal.disposables.EmptyDisposable; |
| 21 |
import io.reactivex.rxjava3.internal.util.ExceptionHelper; |
| 22 |
import io.reactivex.rxjava3.plugins.RxJavaPlugins; |
| 23 |
|
| 24 |
public final class ObservableGenerate<T, S> extends Observable<T> { |
| 25 |
final Supplier<S> stateSupplier; |
| 26 |
final BiFunction<S, Emitter<T>, S> generator; |
| 27 |
final Consumer<? super S> disposeState; |
| 28 |
|
| 29 |
public ObservableGenerate(Supplier<S> stateSupplier, BiFunction<S, Emitter<T>, S> generator, |
| 30 |
Consumer<? super S> disposeState) { |
| 31 |
this.stateSupplier = stateSupplier; |
| 32 |
this.generator = generator; |
| 33 |
this.disposeState = disposeState; |
| 34 |
} |
| 35 |
|
| 36 |
@Override |
| 37 |
public void subscribeActual(Observer<? super T> observer) { |
| 38 |
S state; |
| 39 |
|
| 40 |
try { |
| 41 |
state = stateSupplier.get(); |
| 42 |
} catch (Throwable e) { |
| 43 |
Exceptions.throwIfFatal(e); |
| 44 |
EmptyDisposable.error(e, observer); |
| 45 |
return; |
| 46 |
} |
| 47 |
|
| 48 |
GeneratorDisposable<T, S> gd = new GeneratorDisposable<>(observer, generator, disposeState, state); |
| 49 |
observer.onSubscribe(gd); |
| 50 |
gd.run(); |
| 51 |
} |
| 52 |
|
| 53 |
static final class GeneratorDisposable<T, S> |
| 54 |
implements Emitter<T>, Disposable { |
| 55 |
|
| 56 |
final Observer<? super T> downstream; |
| 57 |
final BiFunction<S, ? super Emitter<T>, S> generator; |
| 58 |
final Consumer<? super S> disposeState; |
| 59 |
|
| 60 |
S state; |
| 61 |
|
| 62 |
volatile boolean cancelled; |
| 63 |
|
| 64 |
boolean terminate; |
| 65 |
|
| 66 |
boolean hasNext; |
| 67 |
|
| 68 |
GeneratorDisposable(Observer<? super T> actual, |
| 69 |
BiFunction<S, ? super Emitter<T>, S> generator, |
| 70 |
Consumer<? super S> disposeState, S initialState) { |
| 71 |
this.downstream = actual; |
| 72 |
this.generator = generator; |
| 73 |
this.disposeState = disposeState; |
| 74 |
this.state = initialState; |
| 75 |
} |
| 76 |
|
| 77 |
public void run() { |
| 78 |
S s = state; |
| 79 |
|
| 80 |
if (cancelled) { |
| 81 |
state = null; |
| 82 |
dispose(s); |
| 83 |
return; |
| 84 |
} |
| 85 |
|
| 86 |
final BiFunction<S, ? super Emitter<T>, S> f = generator; |
| 87 |
|
| 88 |
for (;;) { |
| 89 |
|
| 90 |
if (cancelled) { |
| 91 |
state = null; |
| 92 |
dispose(s); |
| 93 |
return; |
| 94 |
} |
| 95 |
|
| 96 |
hasNext = false; |
| 97 |
|
| 98 |
try { |
| 99 |
s = f.apply(s, this); |
| 100 |
} catch (Throwable ex) { |
| 101 |
Exceptions.throwIfFatal(ex); |
| 102 |
state = null; |
| 103 |
cancelled = true; |
| 104 |
onError(ex); |
| 105 |
dispose(s); |
| 106 |
return; |
| 107 |
} |
| 108 |
|
| 109 |
if (terminate) { |
| 110 |
cancelled = true; |
| 111 |
state = null; |
| 112 |
dispose(s); |
| 113 |
return; |
| 114 |
} |
| 115 |
} |
| 116 |
|
| 117 |
} |
| 118 |
|
| 119 |
private void dispose(S s) { |
| 120 |
try { |
| 121 |
disposeState.accept(s); |
| 122 |
} catch (Throwable ex) { |
| 123 |
Exceptions.throwIfFatal(ex); |
| 124 |
RxJavaPlugins.onError(ex); |
| 125 |
} |
| 126 |
} |
| 127 |
|
| 128 |
@Override |
| 129 |
public void dispose() { |
| 130 |
cancelled = true; |
| 131 |
} |
| 132 |
|
| 133 |
@Override |
| 134 |
public boolean isDisposed() { |
| 135 |
return cancelled; |
| 136 |
} |
| 137 |
|
| 138 |
@Override |
| 139 |
public void onNext(T t) { |
| 140 |
if (!terminate) { |
| 141 |
if (hasNext) { |
| 142 |
onError(new IllegalStateException("onNext already called in this generate turn")); |
| 143 |
} else { |
| 144 |
if (t == null) { |
| 145 |
onError(ExceptionHelper.createNullPointerException("onNext called with a null value.")); |
| 146 |
} else { |
| 147 |
hasNext = true; |
| 148 |
downstream.onNext(t); |
| 149 |
} |
| 150 |
} |
| 151 |
} |
| 152 |
} |
| 153 |
|
| 154 |
@Override |
| 155 |
public void onError(Throwable t) { |
| 156 |
if (terminate) { |
| 157 |
RxJavaPlugins.onError(t); |
| 158 |
} else { |
| 159 |
if (t == null) { |
| 160 |
t = ExceptionHelper.createNullPointerException("onError called with a null Throwable."); |
| 161 |
} |
| 162 |
terminate = true; |
| 163 |
downstream.onError(t); |
| 164 |
} |
| 165 |
} |
| 166 |
|
| 167 |
@Override |
| 168 |
public void onComplete() { |
| 169 |
if (!terminate) { |
| 170 |
terminate = true; |
| 171 |
downstream.onComplete(); |
| 172 |
} |
| 173 |
} |
| 174 |
} |
| 175 |
} |