|
| 1 | +/** |
| 2 | + * Copyright 2014 Netflix, Inc. |
| 3 | + * |
| 4 | + * Licensed under the Apache License, Version 2.0 (the "License"); you may not |
| 5 | + * use this file except in compliance with the License. You may obtain a copy of |
| 6 | + * the License at |
| 7 | + * |
| 8 | + * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | + * |
| 10 | + * Unless required by applicable law or agreed to in writing, software |
| 11 | + * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT |
| 12 | + * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the |
| 13 | + * License for the specific language governing permissions and limitations under |
| 14 | + * the License. |
| 15 | + */ |
| 16 | +package rx.internal.operators; |
| 17 | + |
| 18 | +import rx.Observable; |
| 19 | +import rx.Observable.Operator; |
| 20 | +import rx.Producer; |
| 21 | +import rx.Subscriber; |
| 22 | +import rx.internal.producers.ProducerArbiter; |
| 23 | +import rx.subscriptions.SerialSubscription; |
| 24 | + |
| 25 | +/** |
| 26 | + * Returns an Observable that emits an error if any item is emitted by the source and emits items from the supplied |
| 27 | + * alternate {@code Observable} after the source completes. |
| 28 | + * |
| 29 | + * @param <T> the source value type |
| 30 | + * @param <R> the result value type |
| 31 | + */ |
| 32 | +public final class OperatorConcatEmptyWith<T, R> implements Operator<R, T> { |
| 33 | + |
| 34 | + private final Observable<? extends R> alternate; |
| 35 | + |
| 36 | + public OperatorConcatEmptyWith(Observable<? extends R> alternate) { |
| 37 | + this.alternate = alternate; |
| 38 | + } |
| 39 | + |
| 40 | + @Override |
| 41 | + public Subscriber<? super T> call(Subscriber<? super R> child) { |
| 42 | + final SerialSubscription ssub = new SerialSubscription(); |
| 43 | + final ParentSubscriber parent = new ParentSubscriber(child, ssub, alternate); |
| 44 | + ssub.set(parent); |
| 45 | + child.add(ssub); |
| 46 | + child.setProducer(parent.emptyProducer); |
| 47 | + return parent; |
| 48 | + } |
| 49 | + |
| 50 | + private final class ParentSubscriber extends Subscriber<T> { |
| 51 | + |
| 52 | + private final Subscriber<? super R> child; |
| 53 | + private final SerialSubscription ssub; |
| 54 | + private final EmptyProducer emptyProducer; |
| 55 | + private final Observable<? extends R> alternate; |
| 56 | + |
| 57 | + ParentSubscriber(Subscriber<? super R> child, final SerialSubscription ssub, Observable<? extends R> alternate) { |
| 58 | + this.child = child; |
| 59 | + this.ssub = ssub; |
| 60 | + this.emptyProducer = new EmptyProducer(); |
| 61 | + this.alternate = alternate; |
| 62 | + } |
| 63 | + |
| 64 | + @Override |
| 65 | + public void setProducer(final Producer producer) { |
| 66 | + /* |
| 67 | + * Always request Max from the parent as we never really expect the parent to emit an item, so the |
| 68 | + * actual value does not matter. However, if the parent producer is waiting for a request to emit |
| 69 | + * a terminal event, not requesting the same will cause a deadlock of the parent never completing and |
| 70 | + * the child never subscribed. |
| 71 | + */ |
| 72 | + producer.request(Long.MAX_VALUE); |
| 73 | + } |
| 74 | + |
| 75 | + @Override |
| 76 | + public void onCompleted() { |
| 77 | + if (!child.isUnsubscribed()) { |
| 78 | + AlternateSubscriber as = new AlternateSubscriber(child, emptyProducer); |
| 79 | + ssub.set(as); |
| 80 | + alternate.unsafeSubscribe(as); |
| 81 | + } |
| 82 | + } |
| 83 | + |
| 84 | + @Override |
| 85 | + public void onError(Throwable e) { |
| 86 | + child.onError(e); |
| 87 | + } |
| 88 | + |
| 89 | + @Override |
| 90 | + public void onNext(T t) { |
| 91 | + onError(new IllegalStateException("Concat empty with source emitted an item: " + t)); |
| 92 | + } |
| 93 | + } |
| 94 | + |
| 95 | + private final class AlternateSubscriber extends Subscriber<R> { |
| 96 | + |
| 97 | + private final EmptyProducer emptyProducer; |
| 98 | + private final Subscriber<? super R> child; |
| 99 | + |
| 100 | + AlternateSubscriber(Subscriber<? super R> child, EmptyProducer emptyProducer) { |
| 101 | + this.child = child; |
| 102 | + this.emptyProducer = emptyProducer; |
| 103 | + } |
| 104 | + |
| 105 | + @Override |
| 106 | + public void setProducer(final Producer producer) { |
| 107 | + emptyProducer.setAltProducer(producer); |
| 108 | + } |
| 109 | + |
| 110 | + @Override |
| 111 | + public void onCompleted() { |
| 112 | + child.onCompleted(); |
| 113 | + } |
| 114 | + |
| 115 | + @Override |
| 116 | + public void onError(Throwable e) { |
| 117 | + child.onError(e); |
| 118 | + } |
| 119 | + |
| 120 | + @Override |
| 121 | + public void onNext(R r) { |
| 122 | + child.onNext(r); |
| 123 | + } |
| 124 | + } |
| 125 | + |
| 126 | + /** |
| 127 | + * This is a producer implementation that does the following: |
| 128 | + * |
| 129 | + * <ul> |
| 130 | + * <li>If the alternate producer has not yet arrived, store the total requested count from downstream.</li> |
| 131 | + * <li>If the alternate producer has arrived, then relay the request demand to it.</li> |
| 132 | + * <li>Request {@link Long#MAX_VALUE} from the parent producer, the first time the child requests anything.</li> |
| 133 | + * </ul> |
| 134 | + * |
| 135 | + * Since, this is only applicable to this operator, it does not check for emissions from the source, as the source |
| 136 | + * is never expected to emit any item. Thus it is "lighter" weight than {@link ProducerArbiter} |
| 137 | + */ |
| 138 | + private static final class EmptyProducer implements Producer { |
| 139 | + |
| 140 | + /*Total requested items till the time the alternate producer arrives.*/ |
| 141 | + private long missedRequested; /*Guarded by this*/ |
| 142 | + /*Producer from the alternate Observable for this operator*/ |
| 143 | + private Producer altProducer; /*Guarded by this*/ |
| 144 | + |
| 145 | + @Override |
| 146 | + public void request(final long requested) { |
| 147 | + if (requested < 0) { |
| 148 | + throw new IllegalArgumentException("Requested items can not be negative."); |
| 149 | + } |
| 150 | + |
| 151 | + if (requested == 0) { |
| 152 | + return; |
| 153 | + } |
| 154 | + |
| 155 | + boolean requestToAlternate = false; |
| 156 | + |
| 157 | + synchronized (this) { |
| 158 | + if (null == altProducer) { |
| 159 | + /*Accumulate requested till the time an alternate producer arrives.*/ |
| 160 | + long r = this.missedRequested; |
| 161 | + long u = r + requested; |
| 162 | + if (u < 0) { |
| 163 | + u = Long.MAX_VALUE; |
| 164 | + } |
| 165 | + this.missedRequested = u; |
| 166 | + } else { |
| 167 | + /*If the alternate producer exists, then relay a valid request. The missed requested will be |
| 168 | + requested from the alt producer on setProducer()*/ |
| 169 | + requestToAlternate = true; |
| 170 | + } |
| 171 | + } |
| 172 | + |
| 173 | + if (requestToAlternate) { |
| 174 | + altProducer.request(requested); |
| 175 | + } |
| 176 | + } |
| 177 | + |
| 178 | + private void setAltProducer(Producer altProducer) { |
| 179 | + if (null == altProducer) { |
| 180 | + throw new IllegalArgumentException("Producer can not be null."); |
| 181 | + } |
| 182 | + |
| 183 | + boolean requestToAlternate = false; |
| 184 | + |
| 185 | + synchronized (this) { |
| 186 | + if (0 != missedRequested) { |
| 187 | + /*Something was requested from the source Observable, relay that to the new producer*/ |
| 188 | + requestToAlternate = true; |
| 189 | + } |
| 190 | + this.altProducer = altProducer; |
| 191 | + } |
| 192 | + |
| 193 | + if (requestToAlternate) { |
| 194 | + this.altProducer.request(missedRequested); |
| 195 | + } |
| 196 | + } |
| 197 | + } |
| 198 | +} |
0 commit comments