Debian Codegraph @

src/main/java/io/reactivex/rxjava3/internal/operators/maybe/MaybeZipIterable.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.maybe;
15
16 import java.util.Arrays;
17 import java.util.Objects;
18
19 import io.reactivex.rxjava3.core.*;
20 import io.reactivex.rxjava3.exceptions.Exceptions;
21 import io.reactivex.rxjava3.functions.Function;
22 import io.reactivex.rxjava3.internal.disposables.EmptyDisposable;
23 import io.reactivex.rxjava3.internal.operators.maybe.MaybeZipArray.ZipCoordinator;
24
25 public final class MaybeZipIterable<T, R> extends Maybe<R> {
26
27 final Iterable<? extends MaybeSource<? extends T>> sources;
28
29 final Function<? super Object[], ? extends R> zipper;
30
31 public MaybeZipIterable(Iterable<? extends MaybeSource<? extends T>> sources, Function<? super Object[], ? extends R> zipper) {
32 this.sources = sources;
33 this.zipper = zipper;
34 }
35
36 @Override
37 protected void subscribeActual(MaybeObserver<? super R> observer) {
38 @SuppressWarnings("unchecked")
39 MaybeSource<? extends T>[] a = new MaybeSource[8];
40 int n = 0;
41
42 try {
43 for (MaybeSource<? extends T> source : sources) {
44 if (source == null) {
45 EmptyDisposable.error(new NullPointerException("One of the sources is null"), observer);
46 return;
47 }
48 if (n == a.length) {
49 a = Arrays.copyOf(a, n + (n >> 2));
50 }
51 a[n++] = source;
52 }
53 } catch (Throwable ex) {
54 Exceptions.throwIfFatal(ex);
55 EmptyDisposable.error(ex, observer);
56 return;
57 }
58
59 if (n == 0) {
60 EmptyDisposable.complete(observer);
61 return;
62 }
63
64 if (n == 1) {
65 a[0].subscribe(new MaybeMap.MapMaybeObserver<>(observer, new SingletonArrayFunc()));
66 return;
67 }
68
69 ZipCoordinator<T, R> parent = new ZipCoordinator<>(observer, n, zipper);
70
71 observer.onSubscribe(parent);
72
73 for (int i = 0; i < n; i++) {
74 if (parent.isDisposed()) {
75 return;
76 }
77
78 a[i].subscribe(parent.observers[i]);
79 }
80 }
81
82 final class SingletonArrayFunc implements Function<T, R> {
83 @Override
84 public R apply(T t) throws Throwable {
85 return Objects.requireNonNull(zipper.apply(new Object[] { t }), "The zipper returned a null value");
86 }
87 }
88 }