|
| 1 | +/** |
| 2 | + * Copyright 2014 Netflix, Inc. |
| 3 | + * |
| 4 | + * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | + * you may not use this file except in compliance with the License. |
| 6 | + * You may obtain a copy of 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, |
| 12 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | + * See the License for the specific language governing permissions and |
| 14 | + * limitations under the License. |
| 15 | + */ |
| 16 | +package rx.operators; |
| 17 | + |
| 18 | +import java.util.LinkedList; |
| 19 | +import java.util.Queue; |
| 20 | +import rx.Subscriber; |
| 21 | +import rx.subscriptions.CompositeSubscription; |
| 22 | + |
| 23 | +/** |
| 24 | + * Buffers the incoming events until notified, then replays the |
| 25 | + * buffered events and continues as a simple pass-through subscriber. |
| 26 | + * @param <T> the streamed value type |
| 27 | + */ |
| 28 | +public class BufferUntilSubscriber<T> extends Subscriber<T> { |
| 29 | + /** The actual subscriber. */ |
| 30 | + private final Subscriber<? super T> actual; |
| 31 | + /** Indicate the pass-through mode. */ |
| 32 | + private volatile boolean passthroughMode; |
| 33 | + /** Protect mode transition. */ |
| 34 | + private final Object gate = new Object(); |
| 35 | + /** The buffered items. */ |
| 36 | + private final Queue<Object> queue = new LinkedList<Object>(); |
| 37 | + /** The queue capacity. */ |
| 38 | + private final int capacity; |
| 39 | + /** Null sentinel (in case queue type is changed). */ |
| 40 | + private static final Object NULL_SENTINEL = new Object(); |
| 41 | + /** Complete sentinel. */ |
| 42 | + private static final Object COMPLETE_SENTINEL = new Object(); |
| 43 | + /** |
| 44 | + * Container for an onError event. |
| 45 | + */ |
| 46 | + private static final class ErrorSentinel { |
| 47 | + final Throwable t; |
| 48 | + |
| 49 | + public ErrorSentinel(Throwable t) { |
| 50 | + this.t = t; |
| 51 | + } |
| 52 | + |
| 53 | + } |
| 54 | + /** |
| 55 | + * Constructor that wraps the actual subscriber and shares its subscription. |
| 56 | + * @param capacity the queue capacity to accept before blocking, negative value indicates an unbounded queue |
| 57 | + * @param actual |
| 58 | + */ |
| 59 | + public BufferUntilSubscriber(int capacity, Subscriber<? super T> actual) { |
| 60 | + super(actual); |
| 61 | + this.actual = actual; |
| 62 | + this.capacity = capacity; |
| 63 | + } |
| 64 | + /** |
| 65 | + * Constructor that wraps the actual subscriber and uses the given composite |
| 66 | + * subscription. |
| 67 | + * @param capacity the queue capacity to accept before blocking, negative value indicates an unbounded queue |
| 68 | + * @param actual |
| 69 | + * @param cs |
| 70 | + */ |
| 71 | + public BufferUntilSubscriber(int capacity, Subscriber<? super T> actual, CompositeSubscription cs) { |
| 72 | + super(cs); |
| 73 | + this.actual = actual; |
| 74 | + this.capacity = capacity; |
| 75 | + } |
| 76 | + |
| 77 | + /** |
| 78 | + * Call this method to replay the buffered events and continue as a pass-through subscriber. |
| 79 | + * If already in pass-through mode, this method is a no-op. |
| 80 | + */ |
| 81 | + public void enterPassthroughMode() { |
| 82 | + if (!passthroughMode) { |
| 83 | + synchronized (gate) { |
| 84 | + if (!passthroughMode) { |
| 85 | + while (!queue.isEmpty()) { |
| 86 | + Object o = queue.poll(); |
| 87 | + if (!actual.isUnsubscribed()) { |
| 88 | + if (o == NULL_SENTINEL) { |
| 89 | + actual.onNext(null); |
| 90 | + } else |
| 91 | + if (o == COMPLETE_SENTINEL) { |
| 92 | + actual.onCompleted(); |
| 93 | + } else |
| 94 | + if (o instanceof ErrorSentinel) { |
| 95 | + actual.onError(((ErrorSentinel)o).t); |
| 96 | + } else |
| 97 | + if (o != null) { |
| 98 | + @SuppressWarnings("unchecked") |
| 99 | + T v = (T)o; |
| 100 | + actual.onNext(v); |
| 101 | + } else { |
| 102 | + throw new NullPointerException(); |
| 103 | + } |
| 104 | + } |
| 105 | + } |
| 106 | + passthroughMode = true; |
| 107 | + gate.notifyAll(); |
| 108 | + } |
| 109 | + } |
| 110 | + } |
| 111 | + } |
| 112 | + @Override |
| 113 | + public void onNext(T t) { |
| 114 | + if (!passthroughMode) { |
| 115 | + synchronized (gate) { |
| 116 | + if (!passthroughMode) { |
| 117 | + if (capacity < 0 || queue.size() < capacity) { |
| 118 | + queue.offer(t != null ? t : NULL_SENTINEL); |
| 119 | + return; |
| 120 | + } |
| 121 | + try { |
| 122 | + while (!passthroughMode) { |
| 123 | + gate.wait(); |
| 124 | + } |
| 125 | + if (actual.isUnsubscribed()) { |
| 126 | + return; |
| 127 | + } |
| 128 | + } catch (InterruptedException ex) { |
| 129 | + Thread.currentThread().interrupt(); |
| 130 | + actual.onError(ex); |
| 131 | + return; |
| 132 | + } |
| 133 | + } |
| 134 | + } |
| 135 | + } |
| 136 | + actual.onNext(t); |
| 137 | + } |
| 138 | + |
| 139 | + @Override |
| 140 | + public void onError(Throwable e) { |
| 141 | + if (!passthroughMode) { |
| 142 | + synchronized (gate) { |
| 143 | + if (!passthroughMode) { |
| 144 | + if (capacity < 0 || queue.size() < capacity) { |
| 145 | + queue.offer(new ErrorSentinel(e)); |
| 146 | + return; |
| 147 | + } |
| 148 | + try { |
| 149 | + while (!passthroughMode) { |
| 150 | + gate.wait(); |
| 151 | + } |
| 152 | + if (actual.isUnsubscribed()) { |
| 153 | + return; |
| 154 | + } |
| 155 | + } catch (InterruptedException ex) { |
| 156 | + Thread.currentThread().interrupt(); |
| 157 | + actual.onError(ex); |
| 158 | + return; |
| 159 | + } |
| 160 | + } |
| 161 | + } |
| 162 | + } |
| 163 | + actual.onError(e); |
| 164 | + } |
| 165 | + |
| 166 | + @Override |
| 167 | + public void onCompleted() { |
| 168 | + if (!passthroughMode) { |
| 169 | + synchronized (gate) { |
| 170 | + if (!passthroughMode) { |
| 171 | + if (capacity < 0 || queue.size() < capacity) { |
| 172 | + queue.offer(COMPLETE_SENTINEL); |
| 173 | + return; |
| 174 | + } |
| 175 | + try { |
| 176 | + while (!passthroughMode) { |
| 177 | + gate.wait(); |
| 178 | + } |
| 179 | + if (actual.isUnsubscribed()) { |
| 180 | + return; |
| 181 | + } |
| 182 | + } catch (InterruptedException ex) { |
| 183 | + Thread.currentThread().interrupt(); |
| 184 | + actual.onError(ex); |
| 185 | + return; |
| 186 | + } |
| 187 | + } |
| 188 | + } |
| 189 | + } |
| 190 | + actual.onCompleted(); |
| 191 | + } |
| 192 | + |
| 193 | +} |
0 commit comments