Debian Codegraph @

src/main/java/io/reactivex/rxjava3/internal/operators/observable/ObservableZipIterable.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 java.util.Iterator;
17 import java.util.Objects;
18
19 import io.reactivex.rxjava3.core.*;
20 import io.reactivex.rxjava3.disposables.Disposable;
21 import io.reactivex.rxjava3.exceptions.Exceptions;
22 import io.reactivex.rxjava3.functions.BiFunction;
23 import io.reactivex.rxjava3.internal.disposables.*;
24 import io.reactivex.rxjava3.plugins.RxJavaPlugins;
25
26 public final class ObservableZipIterable<T, U, V> extends Observable<V> {
27 final Observable<? extends T> source;
28 final Iterable<U> other;
29 final BiFunction<? super T, ? super U, ? extends V> zipper;
30
31 public ObservableZipIterable(
32 Observable<? extends T> source,
33 Iterable<U> other, BiFunction<? super T, ? super U, ? extends V> zipper) {
34 this.source = source;
35 this.other = other;
36 this.zipper = zipper;
37 }
38
39 @Override
40 public void subscribeActual(Observer<? super V> t) {
41 Iterator<U> it;
42
43 try {
44 it = Objects.requireNonNull(other.iterator(), "The iterator returned by other is null");
45 } catch (Throwable e) {
46 Exceptions.throwIfFatal(e);
47 EmptyDisposable.error(e, t);
48 return;
49 }
50
51 boolean b;
52
53 try {
54 b = it.hasNext();
55 } catch (Throwable e) {
56 Exceptions.throwIfFatal(e);
57 EmptyDisposable.error(e, t);
58 return;
59 }
60
61 if (!b) {
62 EmptyDisposable.complete(t);
63 return;
64 }
65
66 source.subscribe(new ZipIterableObserver<T, U, V>(t, it, zipper));
67 }
68
69 static final class ZipIterableObserver<T, U, V> implements Observer<T>, Disposable {
70 final Observer<? super V> downstream;
71 final Iterator<U> iterator;
72 final BiFunction<? super T, ? super U, ? extends V> zipper;
73
74 Disposable upstream;
75
76 boolean done;
77
78 ZipIterableObserver(Observer<? super V> actual, Iterator<U> iterator,
79 BiFunction<? super T, ? super U, ? extends V> zipper) {
80 this.downstream = actual;
81 this.iterator = iterator;
82 this.zipper = zipper;
83 }
84
85 @Override
86 public void onSubscribe(Disposable d) {
87 if (DisposableHelper.validate(this.upstream, d)) {
88 this.upstream = d;
89 downstream.onSubscribe(this);
90 }
91 }
92
93 @Override
94 public void dispose() {
95 upstream.dispose();
96 }
97
98 @Override
99 public boolean isDisposed() {
100 return upstream.isDisposed();
101 }
102
103 @Override
104 public void onNext(T t) {
105 if (done) {
106 return;
107 }
108
109 U u;
110
111 try {
112 u = Objects.requireNonNull(iterator.next(), "The iterator returned a null value");
113 } catch (Throwable e) {
114 Exceptions.throwIfFatal(e);
115 fail(e);
116 return;
117 }
118
119 V v;
120 try {
121 v = Objects.requireNonNull(zipper.apply(t, u), "The zipper function returned a null value");
122 } catch (Throwable e) {
123 Exceptions.throwIfFatal(e);
124 fail(e);
125 return;
126 }
127
128 downstream.onNext(v);
129
130 boolean b;
131
132 try {
133 b = iterator.hasNext();
134 } catch (Throwable e) {
135 Exceptions.throwIfFatal(e);
136 fail(e);
137 return;
138 }
139
140 if (!b) {
141 done = true;
142 upstream.dispose();
143 downstream.onComplete();
144 }
145 }
146
147 void fail(Throwable e) {
148 done = true;
149 upstream.dispose();
150 downstream.onError(e);
151 }
152
153 @Override
154 public void onError(Throwable t) {
155 if (done) {
156 RxJavaPlugins.onError(t);
157 return;
158 }
159 done = true;
160 downstream.onError(t);
161 }
162
163 @Override
164 public void onComplete() {
165 if (done) {
166 return;
167 }
168 done = true;
169 downstream.onComplete();
170 }
171
172 }
173 }