Skip to content

Commit 61aea25

Browse files
committed
Return null on getSubscription() when previously unsubscribed to match
initial contract
1 parent 39b9787 commit 61aea25

File tree

2 files changed

+273
-276
lines changed

2 files changed

+273
-276
lines changed
Lines changed: 62 additions & 66 deletions
Original file line numberDiff line numberDiff line change
@@ -1,66 +1,62 @@
1-
/**
2-
* Copyright 2013 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.subscriptions;
17-
18-
import static rx.subscriptions.Subscriptions.empty;
19-
20-
import java.util.concurrent.atomic.AtomicReference;
21-
22-
import rx.Subscription;
23-
24-
/**
25-
* Represents a subscription whose underlying subscription can be swapped for another subscription
26-
* which causes the previous underlying subscription to be unsubscribed.
27-
*
28-
* @see <a href="http://msdn.microsoft.com/en-us/library/system.reactive.disposables.serialdisposable(v=vs.103).aspx">Rx.Net equivalent SerialDisposable</a>
29-
*/
30-
public class SerialSubscription implements Subscription {
31-
private final AtomicReference<Subscription> reference = new AtomicReference<Subscription>(empty());
32-
private volatile Subscription unsubscribe;
33-
34-
private static final Subscription UNSUBSCRIBED = new Subscription() {
35-
@Override
36-
public void unsubscribe() {
37-
}
38-
};
39-
40-
@Override
41-
public void unsubscribe() {
42-
setSubscription(UNSUBSCRIBED);
43-
}
44-
45-
public void setSubscription(final Subscription subscription) {
46-
do {
47-
final Subscription current = reference.get();
48-
if (current == UNSUBSCRIBED) {
49-
subscription.unsubscribe();
50-
break;
51-
}
52-
if (reference.compareAndSet(current, subscription)) {
53-
current.unsubscribe();
54-
if(subscription == UNSUBSCRIBED) {
55-
unsubscribe = current;
56-
}
57-
break;
58-
}
59-
} while (true);
60-
}
61-
62-
public Subscription getSubscription() {
63-
Subscription subscription = reference.get();
64-
return subscription == UNSUBSCRIBED ? unsubscribe : subscription;
65-
}
66-
}
1+
/**
2+
* Copyright 2013 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.subscriptions;
17+
18+
import static rx.subscriptions.Subscriptions.empty;
19+
20+
import java.util.concurrent.atomic.AtomicReference;
21+
22+
import rx.Subscription;
23+
24+
/**
25+
* Represents a subscription whose underlying subscription can be swapped for another subscription
26+
* which causes the previous underlying subscription to be unsubscribed.
27+
*
28+
* @see <a href="http://msdn.microsoft.com/en-us/library/system.reactive.disposables.serialdisposable(v=vs.103).aspx">Rx.Net equivalent SerialDisposable</a>
29+
*/
30+
public class SerialSubscription implements Subscription {
31+
private final AtomicReference<Subscription> reference = new AtomicReference<Subscription>(empty());
32+
33+
private static final Subscription UNSUBSCRIBED = new Subscription() {
34+
@Override
35+
public void unsubscribe() {
36+
}
37+
};
38+
39+
@Override
40+
public void unsubscribe() {
41+
setSubscription(UNSUBSCRIBED);
42+
}
43+
44+
public void setSubscription(final Subscription subscription) {
45+
do {
46+
final Subscription current = reference.get();
47+
if (current == UNSUBSCRIBED) {
48+
subscription.unsubscribe();
49+
break;
50+
}
51+
if (reference.compareAndSet(current, subscription)) {
52+
current.unsubscribe();
53+
break;
54+
}
55+
} while (true);
56+
}
57+
58+
public Subscription getSubscription() {
59+
final Subscription subscription = reference.get();
60+
return subscription == UNSUBSCRIBED ? null : subscription;
61+
}
62+
}

0 commit comments

Comments
 (0)