Debian Codegraph @

src/main/java/io/reactivex/rxjava3/internal/operators/flowable/FlowableZipIterable.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.flowable;
15
16 import java.util.Iterator;
17 import java.util.Objects;
18
19 import org.reactivestreams.*;
20
21 import io.reactivex.rxjava3.core.*;
22 import io.reactivex.rxjava3.exceptions.Exceptions;
23 import io.reactivex.rxjava3.functions.BiFunction;
24 import io.reactivex.rxjava3.internal.subscriptions.*;
25 import io.reactivex.rxjava3.plugins.RxJavaPlugins;
26
27 public final class FlowableZipIterable<T, U, V> extends AbstractFlowableWithUpstream<T, V> {
28 final Iterable<U> other;
29 final BiFunction<? super T, ? super U, ? extends V> zipper;
30
31 public FlowableZipIterable(
32 Flowable<T> source,
33 Iterable<U> other, BiFunction<? super T, ? super U, ? extends V> zipper) {
34 super(source);
35 this.other = other;
36 this.zipper = zipper;
37 }
38
39 @Override
40 public void subscribeActual(Subscriber<? 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 EmptySubscription.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 EmptySubscription.error(e, t);
58 return;
59 }
60
61 if (!b) {
62 EmptySubscription.complete(t);
63 return;
64 }
65
66 source.subscribe(new ZipIterableSubscriber<T, U, V>(t, it, zipper));
67 }
68
69 static final class ZipIterableSubscriber<T, U, V> implements FlowableSubscriber<T>, Subscription {
70 final Subscriber<? super V> downstream;
71 final Iterator<U> iterator;
72 final BiFunction<? super T, ? super U, ? extends V> zipper;
73
74 Subscription upstream;
75
76 boolean done;
77
78 ZipIterableSubscriber(Subscriber<? 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(Subscription s) {
87 if (SubscriptionHelper.validate(this.upstream, s)) {
88 this.upstream = s;
89 downstream.onSubscribe(this);
90 }
91 }
92
93 @Override
94 public void onNext(T t) {
95 if (done) {
96 return;
97 }
98
99 U u;
100
101 try {
102 u = Objects.requireNonNull(iterator.next(), "The iterator returned a null value");
103 } catch (Throwable e) {
104 fail(e);
105 return;
106 }
107
108 V v;
109 try {
110 v = Objects.requireNonNull(zipper.apply(t, u), "The zipper function returned a null value");
111 } catch (Throwable e) {
112 fail(e);
113 return;
114 }
115
116 downstream.onNext(v);
117
118 boolean b;
119
120 try {
121 b = iterator.hasNext();
122 } catch (Throwable e) {
123 fail(e);
124 return;
125 }
126
127 if (!b) {
128 done = true;
129 upstream.cancel();
130 downstream.onComplete();
131 }
132 }
133
134 void fail(Throwable e) {
135 Exceptions.throwIfFatal(e);
136 done = true;
137 upstream.cancel();
138 downstream.onError(e);
139 }
140
141 @Override
142 public void onError(Throwable t) {
143 if (done) {
144 RxJavaPlugins.onError(t);
145 return;
146 }
147 done = true;
148 downstream.onError(t);
149 }
150
151 @Override
152 public void onComplete() {
153 if (done) {
154 return;
155 }
156 done = true;
157 downstream.onComplete();
158 }
159
160 @Override
161 public void request(long n) {
162 upstream.request(n);
163 }
164
165 @Override
166 public void cancel() {
167 upstream.cancel();
168 }
169
170 }
171 }