|
| 1 | +package rx.internal.operators; |
| 2 | + |
| 3 | +import java.util.concurrent.atomic.AtomicInteger; |
| 4 | + |
| 5 | +import rx.Observable.Operator; |
| 6 | +import rx.Producer; |
| 7 | +import rx.Subscriber; |
| 8 | + |
| 9 | +public class OperatorTakeLastOne<T> implements Operator<T, T> { |
| 10 | + |
| 11 | + private static class Holder { |
| 12 | + static final OperatorTakeLastOne<Object> INSTANCE = new OperatorTakeLastOne<Object>(); |
| 13 | + } |
| 14 | + |
| 15 | + @SuppressWarnings("unchecked") |
| 16 | + public static <T> OperatorTakeLastOne<T> instance() { |
| 17 | + return (OperatorTakeLastOne<T>) Holder.INSTANCE; |
| 18 | + } |
| 19 | + |
| 20 | + private OperatorTakeLastOne() { |
| 21 | + |
| 22 | + } |
| 23 | + |
| 24 | + @Override |
| 25 | + public Subscriber<? super T> call(Subscriber<? super T> child) { |
| 26 | + final ParentSubscriber<T> parent = new ParentSubscriber<T>(child); |
| 27 | + child.setProducer(new Producer() { |
| 28 | + |
| 29 | + @Override |
| 30 | + public void request(long n) { |
| 31 | + parent.requestMore(n); |
| 32 | + } |
| 33 | + }); |
| 34 | + child.add(parent); |
| 35 | + return parent; |
| 36 | + } |
| 37 | + |
| 38 | + private static class ParentSubscriber<T> extends Subscriber<T> { |
| 39 | + |
| 40 | + private final static int NOT_REQUESTED_NOT_COMPLETED = 0; |
| 41 | + private final static int NOT_REQUESTED_COMPLETED = 1; |
| 42 | + private final static int REQUESTED_NOT_COMPLETED = 2; |
| 43 | + private final static int REQUESTED_COMPLETED = 3; |
| 44 | + |
| 45 | + /* |
| 46 | + * These are the expected state transitions: |
| 47 | + * |
| 48 | + * NOT_REQUESTED_NOT_COMPLETED --> REQUESTED_NOT_COMPLETED |
| 49 | + * | | |
| 50 | + * V V |
| 51 | + * NOT_REQUESTED_COMPLETED --> REQUESTED_COMPLETED |
| 52 | + * |
| 53 | + * Once at REQUESTED_COMPLETED we emit the last value if one exists |
| 54 | + */ |
| 55 | + |
| 56 | + // Used as the initial value of last |
| 57 | + private static final Object ABSENT = new Object(); |
| 58 | + |
| 59 | + // the downstream subscriber |
| 60 | + private final Subscriber<? super T> child; |
| 61 | + |
| 62 | + @SuppressWarnings("unchecked") |
| 63 | + // we can get away with this cast at runtime because of type erasure |
| 64 | + private T last = (T) ABSENT; |
| 65 | + |
| 66 | + // holds the current state of the stream so that we can make atomic |
| 67 | + // updates to it |
| 68 | + private final AtomicInteger state = new AtomicInteger(NOT_REQUESTED_NOT_COMPLETED); |
| 69 | + |
| 70 | + ParentSubscriber(Subscriber<? super T> child) { |
| 71 | + this.child = child; |
| 72 | + } |
| 73 | + |
| 74 | + void requestMore(long n) { |
| 75 | + if (n > 0) { |
| 76 | + // CAS loop to atomically change state given that onCompleted() |
| 77 | + // or another requestMore() may be acting concurrently |
| 78 | + while (true) { |
| 79 | + // read the value of state and then try state transitions |
| 80 | + // only if the value of state does not change in the |
| 81 | + // meantime (in another requestMore() or onCompleted()). If |
| 82 | + // the value has changed and we expect to do a transition |
| 83 | + // still then we loop and try again. |
| 84 | + final int s = state.get(); |
| 85 | + if (s == NOT_REQUESTED_NOT_COMPLETED) { |
| 86 | + if (state.compareAndSet(NOT_REQUESTED_NOT_COMPLETED, |
| 87 | + REQUESTED_NOT_COMPLETED)) { |
| 88 | + return; |
| 89 | + } |
| 90 | + } else if (s == NOT_REQUESTED_COMPLETED) { |
| 91 | + if (state.compareAndSet(NOT_REQUESTED_COMPLETED, REQUESTED_COMPLETED)) { |
| 92 | + emit(); |
| 93 | + return; |
| 94 | + } |
| 95 | + } else |
| 96 | + // already requested so we exit |
| 97 | + return; |
| 98 | + } |
| 99 | + } |
| 100 | + } |
| 101 | + |
| 102 | + @Override |
| 103 | + public void onCompleted() { |
| 104 | + // CAS loop to atomically change state given that requestMore() |
| 105 | + // may be acting concurrently |
| 106 | + while (true) { |
| 107 | + // read the value of state and then try state transitions |
| 108 | + // only if the value of state does not change in the meantime |
| 109 | + // (in another requestMore()). If the value has changed and |
| 110 | + // we expect to do a transition still then we loop and try |
| 111 | + // again. |
| 112 | + final int s = state.get(); |
| 113 | + if (s == NOT_REQUESTED_NOT_COMPLETED) { |
| 114 | + if (state.compareAndSet(NOT_REQUESTED_NOT_COMPLETED, NOT_REQUESTED_COMPLETED)) { |
| 115 | + return; |
| 116 | + } |
| 117 | + } else if (s == REQUESTED_NOT_COMPLETED) { |
| 118 | + if (state.compareAndSet(REQUESTED_NOT_COMPLETED, REQUESTED_COMPLETED)) { |
| 119 | + emit(); |
| 120 | + return; |
| 121 | + } |
| 122 | + } else |
| 123 | + // already completed so we exit |
| 124 | + return; |
| 125 | + } |
| 126 | + } |
| 127 | + |
| 128 | + /** |
| 129 | + * If not unsubscribed then emits last value and completed to the child |
| 130 | + * subscriber. |
| 131 | + */ |
| 132 | + private void emit() { |
| 133 | + if (isUnsubscribed()) { |
| 134 | + // release for gc |
| 135 | + last = null; |
| 136 | + return; |
| 137 | + } |
| 138 | + // Note that last is safely published despite not being volatile |
| 139 | + // because a CAS update must have happened in the current thread just before |
| 140 | + // emit() was called |
| 141 | + T t = last; |
| 142 | + // release for gc |
| 143 | + last = null; |
| 144 | + if (t != ABSENT) { |
| 145 | + try { |
| 146 | + child.onNext(t); |
| 147 | + } catch (Throwable e) { |
| 148 | + child.onError(e); |
| 149 | + return; |
| 150 | + } |
| 151 | + } |
| 152 | + if (!isUnsubscribed()) |
| 153 | + child.onCompleted(); |
| 154 | + } |
| 155 | + |
| 156 | + @Override |
| 157 | + public void onError(Throwable e) { |
| 158 | + child.onError(e); |
| 159 | + } |
| 160 | + |
| 161 | + @Override |
| 162 | + public void onNext(T t) { |
| 163 | + last = t; |
| 164 | + } |
| 165 | + |
| 166 | + } |
| 167 | + |
| 168 | +} |
0 commit comments