|
| 1 | +/** |
| 2 | + * Copyright 2015 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; |
| 15 | + |
| 16 | +import java.util.Optional; |
| 17 | +import java.util.concurrent.atomic.*; |
| 18 | + |
| 19 | +import org.reactivestreams.*; |
| 20 | + |
| 21 | +import io.reactivex.*; |
| 22 | +import io.reactivex.Observable.Operator; |
| 23 | +import io.reactivex.internal.subscriptions.SubscriptionHelper; |
| 24 | +import io.reactivex.internal.util.BackpressureHelper; |
| 25 | + |
| 26 | +public enum OperatorMaterialize implements Operator<Try<Optional<Object>>, Object> { |
| 27 | + INSTANCE; |
| 28 | + |
| 29 | + @SuppressWarnings({ "rawtypes", "unchecked" }) |
| 30 | + public static <T> Operator<Try<Optional<T>>, T> instance() { |
| 31 | + return (Operator)INSTANCE; |
| 32 | + } |
| 33 | + |
| 34 | + @Override |
| 35 | + public Subscriber<? super Object> apply(Subscriber<? super Try<Optional<Object>>> t) { |
| 36 | + return new OnErrorReturnSubscriber<>(t); |
| 37 | + } |
| 38 | + |
| 39 | + static final class OnErrorReturnSubscriber<T> extends AtomicLong implements Subscriber<T>, Subscription { |
| 40 | + /** */ |
| 41 | + private static final long serialVersionUID = -3740826063558713822L; |
| 42 | + final Subscriber<? super Try<Optional<T>>> actual; |
| 43 | + |
| 44 | + Subscription s; |
| 45 | + |
| 46 | + volatile int state; |
| 47 | + @SuppressWarnings("rawtypes") |
| 48 | + static final AtomicIntegerFieldUpdater<OnErrorReturnSubscriber> STATE = |
| 49 | + AtomicIntegerFieldUpdater.newUpdater(OnErrorReturnSubscriber.class, "state"); |
| 50 | + Try<Optional<T>> value; |
| 51 | + |
| 52 | + volatile boolean done; |
| 53 | + |
| 54 | + static final int NO_REQUEST_NO_VALUE = 0; |
| 55 | + static final int NO_REQUEST_HAS_VALUE = 1; |
| 56 | + static final int HAS_REQUEST_NO_VALUE = 2; |
| 57 | + static final int HAS_REQUEST_HAS_VALUE = 3; |
| 58 | + |
| 59 | + public OnErrorReturnSubscriber(Subscriber<? super Try<Optional<T>>> actual) { |
| 60 | + this.actual = actual; |
| 61 | + } |
| 62 | + |
| 63 | + @Override |
| 64 | + public void onSubscribe(Subscription s) { |
| 65 | + if (SubscriptionHelper.validateSubscription(this.s, s)) { |
| 66 | + return; |
| 67 | + } |
| 68 | + this.s = s; |
| 69 | + actual.onSubscribe(this); |
| 70 | + } |
| 71 | + |
| 72 | + @Override |
| 73 | + public void onNext(T t) { |
| 74 | + actual.onNext(Notification.next(t)); |
| 75 | + |
| 76 | + if (get() != Long.MAX_VALUE) { |
| 77 | + decrementAndGet(); |
| 78 | + } |
| 79 | + } |
| 80 | + |
| 81 | + void tryEmit(Try<Optional<T>> v) { |
| 82 | + if (get() != 0L) { |
| 83 | + STATE.lazySet(this, HAS_REQUEST_HAS_VALUE); |
| 84 | + actual.onNext(v); |
| 85 | + actual.onComplete(); |
| 86 | + } else { |
| 87 | + for (;;) { |
| 88 | + int s = state; |
| 89 | + if (s == HAS_REQUEST_NO_VALUE) { |
| 90 | + if (STATE.compareAndSet(this, s, HAS_REQUEST_HAS_VALUE)) { |
| 91 | + actual.onNext(v); |
| 92 | + actual.onComplete(); |
| 93 | + return; |
| 94 | + } |
| 95 | + } else |
| 96 | + if (s == NO_REQUEST_HAS_VALUE) { |
| 97 | + return; |
| 98 | + } else |
| 99 | + if (s == HAS_REQUEST_HAS_VALUE) { |
| 100 | + value = null; |
| 101 | + return; |
| 102 | + } else { |
| 103 | + value = v; |
| 104 | + if (STATE.compareAndSet(this, s, NO_REQUEST_HAS_VALUE)) { |
| 105 | + return; |
| 106 | + } |
| 107 | + } |
| 108 | + } |
| 109 | + } |
| 110 | + } |
| 111 | + |
| 112 | + @Override |
| 113 | + public void onError(Throwable t) { |
| 114 | + done = true; |
| 115 | + |
| 116 | + Try<Optional<T>> v = Notification.error(t); |
| 117 | + |
| 118 | + tryEmit(v); |
| 119 | + } |
| 120 | + |
| 121 | + @Override |
| 122 | + public void onComplete() { |
| 123 | + done = true; |
| 124 | + |
| 125 | + Try<Optional<T>> v = Notification.complete(); |
| 126 | + |
| 127 | + tryEmit(v); |
| 128 | + } |
| 129 | + |
| 130 | + @Override |
| 131 | + public void request(long n) { |
| 132 | + if (SubscriptionHelper.validateRequest(n)) { |
| 133 | + return; |
| 134 | + } |
| 135 | + if (BackpressureHelper.add(this, n) == 0) { |
| 136 | + if (done) { |
| 137 | + for (;;) { |
| 138 | + int s = state; |
| 139 | + if (s == NO_REQUEST_HAS_VALUE) { |
| 140 | + if (STATE.compareAndSet(this, s, HAS_REQUEST_HAS_VALUE)) { |
| 141 | + Try<Optional<T>> v = value; |
| 142 | + value = null; |
| 143 | + actual.onNext(v); |
| 144 | + actual.onComplete(); |
| 145 | + return; |
| 146 | + } |
| 147 | + } else |
| 148 | + if (s == HAS_REQUEST_NO_VALUE || s == HAS_REQUEST_HAS_VALUE) { |
| 149 | + return; |
| 150 | + } else |
| 151 | + if (STATE.compareAndSet(this, s, HAS_REQUEST_NO_VALUE)) { |
| 152 | + return; |
| 153 | + } |
| 154 | + } |
| 155 | + } |
| 156 | + } |
| 157 | + } |
| 158 | + |
| 159 | + @Override |
| 160 | + public void cancel() { |
| 161 | + STATE.lazySet(this, HAS_REQUEST_HAS_VALUE); |
| 162 | + s.cancel(); |
| 163 | + } |
| 164 | + } |
| 165 | +} |
0 commit comments