Debian Codegraph @

src/main/java/io/reactivex/rxjava3/internal/operators/single/SingleInternalHelper.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 org.reactivestreams.Publisher;
19
20 import io.reactivex.rxjava3.core.*;
21 import io.reactivex.rxjava3.functions.*;
22
23 /**
24 * Helper utility class to support Single with inner classes.
25 */
26 public final class SingleInternalHelper {
27
28 /** Utility class. */
29 private SingleInternalHelper() {
30 throw new IllegalStateException("No instances!");
31 }
32
33 enum NoSuchElementSupplier implements Supplier<NoSuchElementException> {
34 INSTANCE;
35
36 @Override
37 public NoSuchElementException get() {
38 return new NoSuchElementException();
39 }
40 }
41
42 public static Supplier<NoSuchElementException> emptyThrower() {
43 return NoSuchElementSupplier.INSTANCE;
44 }
45
46 @SuppressWarnings("rawtypes")
47 enum ToFlowable implements Function<SingleSource, Publisher> {
48 INSTANCE;
49 @SuppressWarnings("unchecked")
50 @Override
51 public Publisher apply(SingleSource v) {
52 return new SingleToFlowable(v);
53 }
54 }
55
56 @SuppressWarnings({ "rawtypes", "unchecked" })
57 public static <T> Function<SingleSource<? extends T>, Publisher<? extends T>> toFlowable() {
58 return (Function)ToFlowable.INSTANCE;
59 }
60
61 static final class ToFlowableIterator<T> implements Iterator<Flowable<T>> {
62 private final Iterator<? extends SingleSource<? extends T>> sit;
63
64 ToFlowableIterator(Iterator<? extends SingleSource<? extends T>> sit) {
65 this.sit = sit;
66 }
67
68 @Override
69 public boolean hasNext() {
70 return sit.hasNext();
71 }
72
73 @Override
74 public Flowable<T> next() {
75 return new SingleToFlowable<>(sit.next());
76 }
77
78 @Override
79 public void remove() {
80 throw new UnsupportedOperationException();
81 }
82 }
83
84 static final class ToFlowableIterable<T> implements Iterable<Flowable<T>> {
85
86 private final Iterable<? extends SingleSource<? extends T>> sources;
87
88 ToFlowableIterable(Iterable<? extends SingleSource<? extends T>> sources) {
89 this.sources = sources;
90 }
91
92 @Override
93 public Iterator<Flowable<T>> iterator() {
94 return new ToFlowableIterator<>(sources.iterator());
95 }
96 }
97
98 public static <T> Iterable<? extends Flowable<T>> iterableToFlowable(final Iterable<? extends SingleSource<? extends T>> sources) {
99 return new ToFlowableIterable<>(sources);
100 }
101 }