|
| 1 | +/** |
| 2 | + * Copyright 2016 Netflix, Inc. |
| 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.internal.operators.maybe; |
| 15 | + |
| 16 | +import java.util.*; |
| 17 | +import java.util.concurrent.atomic.AtomicBoolean; |
| 18 | + |
| 19 | +import io.reactivex.*; |
| 20 | +import io.reactivex.disposables.*; |
| 21 | +import io.reactivex.plugins.RxJavaPlugins; |
| 22 | + |
| 23 | +public final class MaybeAmbIterable<T> extends Maybe<T> { |
| 24 | + |
| 25 | + final Iterable<? extends MaybeSource<? extends T>> sources; |
| 26 | + |
| 27 | + public MaybeAmbIterable(Iterable<? extends MaybeSource<? extends T>> sources) { |
| 28 | + this.sources = sources; |
| 29 | + } |
| 30 | + |
| 31 | + @Override |
| 32 | + protected void subscribeActual(final MaybeObserver<? super T> s) { |
| 33 | + final CompositeDisposable set = new CompositeDisposable(); |
| 34 | + s.onSubscribe(set); |
| 35 | + |
| 36 | + Iterator<? extends MaybeSource<? extends T>> iterator; |
| 37 | + |
| 38 | + try { |
| 39 | + iterator = sources.iterator(); |
| 40 | + } catch (Throwable e) { |
| 41 | + s.onError(e); |
| 42 | + return; |
| 43 | + } |
| 44 | + |
| 45 | + if (iterator == null) { |
| 46 | + s.onError(new NullPointerException("The iterator returned is null")); |
| 47 | + return; |
| 48 | + } |
| 49 | + |
| 50 | + final AtomicBoolean once = new AtomicBoolean(); |
| 51 | + int c = 0; |
| 52 | + |
| 53 | + for (;;) { |
| 54 | + if (once.get()) { |
| 55 | + return; |
| 56 | + } |
| 57 | + |
| 58 | + boolean b; |
| 59 | + |
| 60 | + try { |
| 61 | + b = iterator.hasNext(); |
| 62 | + } catch (Throwable e) { |
| 63 | + s.onError(e); |
| 64 | + return; |
| 65 | + } |
| 66 | + |
| 67 | + if (once.get()) { |
| 68 | + return; |
| 69 | + } |
| 70 | + |
| 71 | + if (!b) { |
| 72 | + break; |
| 73 | + } |
| 74 | + |
| 75 | + if (once.get()) { |
| 76 | + return; |
| 77 | + } |
| 78 | + |
| 79 | + MaybeSource<? extends T> s1; |
| 80 | + |
| 81 | + try { |
| 82 | + s1 = iterator.next(); |
| 83 | + } catch (Throwable e) { |
| 84 | + set.dispose(); |
| 85 | + s.onError(e); |
| 86 | + return; |
| 87 | + } |
| 88 | + |
| 89 | + if (s1 == null) { |
| 90 | + set.dispose(); |
| 91 | + s.onError(new NullPointerException("The single source returned by the iterator is null")); |
| 92 | + return; |
| 93 | + } |
| 94 | + |
| 95 | + s1.subscribe(new MaybeObserver<T>() { |
| 96 | + |
| 97 | + @Override |
| 98 | + public void onSubscribe(Disposable d) { |
| 99 | + set.add(d); |
| 100 | + } |
| 101 | + |
| 102 | + @Override |
| 103 | + public void onSuccess(T value) { |
| 104 | + if (once.compareAndSet(false, true)) { |
| 105 | + s.onSuccess(value); |
| 106 | + } |
| 107 | + } |
| 108 | + |
| 109 | + @Override |
| 110 | + public void onComplete() { |
| 111 | + if (once.compareAndSet(false, true)) { |
| 112 | + s.onComplete(); |
| 113 | + } |
| 114 | + } |
| 115 | + |
| 116 | + @Override |
| 117 | + public void onError(Throwable e) { |
| 118 | + if (once.compareAndSet(false, true)) { |
| 119 | + s.onError(e); |
| 120 | + } else { |
| 121 | + RxJavaPlugins.onError(e); |
| 122 | + } |
| 123 | + } |
| 124 | + |
| 125 | + }); |
| 126 | + c++; |
| 127 | + } |
| 128 | + |
| 129 | + if (c == 0 && !set.isDisposed()) { |
| 130 | + s.onError(new NoSuchElementException()); |
| 131 | + } |
| 132 | + } |
| 133 | + |
| 134 | +} |
0 commit comments