|
| 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.internal.subscriptions; |
| 17 | + |
| 18 | +import java.util.concurrent.atomic.AtomicReference; |
| 19 | + |
| 20 | +import rx.Subscription; |
| 21 | +import rx.subscriptions.Subscriptions; |
| 22 | + |
| 23 | +/** |
| 24 | + * A container of a Subscription that supports operations of SerialSubscription |
| 25 | + * and MultipleAssignmentSubscription via methods (update, replace) and extends |
| 26 | + * AtomicReference to reduce allocation count (beware the API leak of AtomicReference!). |
| 27 | + * @since 1.1.9 |
| 28 | + */ |
| 29 | +public final class SequentialSubscription extends AtomicReference<Subscription> implements Subscription { |
| 30 | + |
| 31 | + /** */ |
| 32 | + private static final long serialVersionUID = 995205034283130269L; |
| 33 | + |
| 34 | + /** |
| 35 | + * Create an empty SequentialSubscription. |
| 36 | + */ |
| 37 | + public SequentialSubscription() { |
| 38 | + |
| 39 | + } |
| 40 | + |
| 41 | + /** |
| 42 | + * Create a SequentialSubscription with the given initial Subscription. |
| 43 | + * @param initial the initial Subscription, may be null |
| 44 | + */ |
| 45 | + public SequentialSubscription(Subscription initial) { |
| 46 | + lazySet(initial); |
| 47 | + } |
| 48 | + |
| 49 | + /** |
| 50 | + * Returns the current contained Subscription (may be null). |
| 51 | + * <p>(Remark: named as such because get() is final). |
| 52 | + * @return the current contained Subscription (may be null) |
| 53 | + */ |
| 54 | + public Subscription current() { |
| 55 | + Subscription current = super.get(); |
| 56 | + if (current == Unsubscribed.INSTANCE) { |
| 57 | + return Subscriptions.unsubscribed(); |
| 58 | + } |
| 59 | + return current; |
| 60 | + } |
| 61 | + |
| 62 | + /** |
| 63 | + * Atomically sets the contained Subscription to the provided next value and unsubscribes |
| 64 | + * the previous value or unsubscribes the next value if this container is unsubscribed. |
| 65 | + * <p>(Remark: named as such because set() is final). |
| 66 | + * @param next the next Subscription to contain, may be null |
| 67 | + * @return true if the update succeded, false if the container was unsubscribed |
| 68 | + */ |
| 69 | + public boolean update(Subscription next) { |
| 70 | + for (;;) { |
| 71 | + Subscription current = get(); |
| 72 | + |
| 73 | + if (current == Unsubscribed.INSTANCE) { |
| 74 | + if (next != null) { |
| 75 | + next.unsubscribe(); |
| 76 | + } |
| 77 | + return false; |
| 78 | + } |
| 79 | + |
| 80 | + if (compareAndSet(current, next)) { |
| 81 | + if (current != null) { |
| 82 | + current.unsubscribe(); |
| 83 | + } |
| 84 | + return true; |
| 85 | + } |
| 86 | + } |
| 87 | + } |
| 88 | + |
| 89 | + /** |
| 90 | + * Atomically replaces the contained Subscription to the provided next value but |
| 91 | + * does not unsubscribe the previous value or unsubscribes the next value if this |
| 92 | + * container is unsubscribed. |
| 93 | + * @param next the next Subscription to contain, may be null |
| 94 | + * @return true if the update succeded, false if the container was unsubscribed |
| 95 | + */ |
| 96 | + public boolean replace(Subscription next) { |
| 97 | + for (;;) { |
| 98 | + Subscription current = get(); |
| 99 | + |
| 100 | + if (current == Unsubscribed.INSTANCE) { |
| 101 | + if (next != null) { |
| 102 | + next.unsubscribe(); |
| 103 | + } |
| 104 | + return false; |
| 105 | + } |
| 106 | + |
| 107 | + if (compareAndSet(current, next)) { |
| 108 | + return true; |
| 109 | + } |
| 110 | + } |
| 111 | + } |
| 112 | + |
| 113 | + /** |
| 114 | + * Atomically tries to set the contained Subscription to the provided next value and unsubscribes |
| 115 | + * the previous value or unsubscribes the next value if this container is unsubscribed. |
| 116 | + * <p> |
| 117 | + * Unlike {@link #update(Subscription)}, this doesn't retry if the replace failed |
| 118 | + * because a concurrent operation changed the underlying contained object. |
| 119 | + * @param next the next Subscription to contain, may be null |
| 120 | + * @return true if the update succeded, false if the container was unsubscribed |
| 121 | + */ |
| 122 | + public boolean updateWeak(Subscription next) { |
| 123 | + Subscription current = get(); |
| 124 | + if (current == Unsubscribed.INSTANCE) { |
| 125 | + if (next != null) { |
| 126 | + next.unsubscribe(); |
| 127 | + } |
| 128 | + return false; |
| 129 | + } |
| 130 | + if (compareAndSet(current, next)) { |
| 131 | + return true; |
| 132 | + } |
| 133 | + |
| 134 | + current = get(); |
| 135 | + |
| 136 | + if (next != null) { |
| 137 | + next.unsubscribe(); |
| 138 | + } |
| 139 | + return current == Unsubscribed.INSTANCE; |
| 140 | + } |
| 141 | + |
| 142 | + /** |
| 143 | + * Atomically tries to replace the contained Subscription to the provided next value but |
| 144 | + * does not unsubscribe the previous value or unsubscribes the next value if this container |
| 145 | + * is unsubscribed. |
| 146 | + * <p> |
| 147 | + * Unlike {@link #replace(Subscription)}, this doesn't retry if the replace failed |
| 148 | + * because a concurrent operation changed the underlying contained object. |
| 149 | + * @param next the next Subscription to contain, may be null |
| 150 | + * @return true if the update succeded, false if the container was unsubscribed |
| 151 | + */ |
| 152 | + public boolean replaceWeak(Subscription next) { |
| 153 | + Subscription current = get(); |
| 154 | + if (current == Unsubscribed.INSTANCE) { |
| 155 | + if (next != null) { |
| 156 | + next.unsubscribe(); |
| 157 | + } |
| 158 | + return false; |
| 159 | + } |
| 160 | + if (compareAndSet(current, next)) { |
| 161 | + return true; |
| 162 | + } |
| 163 | + |
| 164 | + current = get(); |
| 165 | + if (current == Unsubscribed.INSTANCE) { |
| 166 | + if (next != null) { |
| 167 | + next.unsubscribe(); |
| 168 | + } |
| 169 | + return false; |
| 170 | + } |
| 171 | + return true; |
| 172 | + } |
| 173 | + |
| 174 | + @Override |
| 175 | + public void unsubscribe() { |
| 176 | + Subscription current = get(); |
| 177 | + if (current != Unsubscribed.INSTANCE) { |
| 178 | + current = getAndSet(Unsubscribed.INSTANCE); |
| 179 | + if (current != null && current != Unsubscribed.INSTANCE) { |
| 180 | + current.unsubscribe(); |
| 181 | + } |
| 182 | + } |
| 183 | + } |
| 184 | + |
| 185 | + @Override |
| 186 | + public boolean isUnsubscribed() { |
| 187 | + return get() == Unsubscribed.INSTANCE; |
| 188 | + } |
| 189 | +} |
0 commit comments