Debian Codegraph @

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