Skip to content

Commit 2c2bc0d

Browse files
Merge pull request #2990 from davidmoten/subscriber-readability
Improve Subscriber readability
2 parents f956293 + 0cf7082 commit 2c2bc0d

File tree

1 file changed

+53
-43
lines changed

1 file changed

+53
-43
lines changed

src/main/java/rx/Subscriber.java

Lines changed: 53 additions & 43 deletions
Original file line numberDiff line numberDiff line change
@@ -31,20 +31,23 @@
3131
* the type of items the Subscriber expects to observe
3232
*/
3333
public abstract class Subscriber<T> implements Observer<T>, Subscription {
34+
35+
// represents requested not set yet
36+
private static final Long NOT_SET = Long.MIN_VALUE;
3437

35-
private final SubscriptionList cs;
36-
private final Subscriber<?> op;
38+
private final SubscriptionList subscriptions;
39+
private final Subscriber<?> subscriber;
3740
/* protected by `this` */
38-
private Producer p;
41+
private Producer producer;
3942
/* protected by `this` */
40-
private long requested = Long.MIN_VALUE; // default to not set
43+
private long requested = NOT_SET; // default to not set
4144

4245
protected Subscriber() {
4346
this(null, false);
4447
}
4548

46-
protected Subscriber(Subscriber<?> op) {
47-
this(op, true);
49+
protected Subscriber(Subscriber<?> subscriber) {
50+
this(subscriber, true);
4851
}
4952

5053
/**
@@ -53,15 +56,15 @@ protected Subscriber(Subscriber<?> op) {
5356
* <p>
5457
* To retain the chaining of subscribers, add the created instance to {@code op} via {@link #add}.
5558
*
56-
* @param op
59+
* @param subscriber
5760
* the other Subscriber
5861
* @param shareSubscriptions
5962
* {@code true} to share the subscription list in {@code op} with this instance
6063
* @since 1.0.6
6164
*/
62-
protected Subscriber(Subscriber<?> op, boolean shareSubscriptions) {
63-
this.op = op;
64-
this.cs = shareSubscriptions && op != null ? op.cs : new SubscriptionList();
65+
protected Subscriber(Subscriber<?> subscriber, boolean shareSubscriptions) {
66+
this.subscriber = subscriber;
67+
this.subscriptions = shareSubscriptions && subscriber != null ? subscriber.subscriptions : new SubscriptionList();
6568
}
6669

6770
/**
@@ -73,12 +76,12 @@ protected Subscriber(Subscriber<?> op, boolean shareSubscriptions) {
7376
* the {@code Subscription} to add
7477
*/
7578
public final void add(Subscription s) {
76-
cs.add(s);
79+
subscriptions.add(s);
7780
}
7881

7982
@Override
8083
public final void unsubscribe() {
81-
cs.unsubscribe();
84+
subscriptions.unsubscribe();
8285
}
8386

8487
/**
@@ -88,7 +91,7 @@ public final void unsubscribe() {
8891
*/
8992
@Override
9093
public final boolean isUnsubscribed() {
91-
return cs.isUnsubscribed();
94+
return subscriptions.isUnsubscribed();
9295
}
9396

9497
/**
@@ -124,57 +127,64 @@ protected final void request(long n) {
124127
if (n < 0) {
125128
throw new IllegalArgumentException("number requested cannot be negative: " + n);
126129
}
127-
Producer shouldRequest = null;
130+
131+
// if producer is set then we will request from it
132+
// otherwise we increase the requested count by n
133+
Producer producerToRequestFrom = null;
128134
synchronized (this) {
129-
if (p != null) {
130-
shouldRequest = p;
131-
} else if (requested == Long.MIN_VALUE) {
132-
requested = n;
133-
} else {
134-
final long total = requested + n;
135-
// check if overflow occurred
136-
if (total < 0) {
137-
requested = Long.MAX_VALUE;
138-
} else {
139-
requested = total;
140-
}
135+
if (producer != null) {
136+
producerToRequestFrom = producer;
137+
} else {
138+
addToRequested(n);
139+
return;
141140
}
142141
}
143-
// after releasing lock
144-
if (shouldRequest != null) {
145-
shouldRequest.request(n);
146-
}
142+
// after releasing lock (we should not make requests holding a lock)
143+
producerToRequestFrom.request(n);
147144
}
148145

146+
private void addToRequested(long n) {
147+
if (requested == NOT_SET) {
148+
requested = n;
149+
} else {
150+
final long total = requested + n;
151+
// check if overflow occurred
152+
if (total < 0) {
153+
requested = Long.MAX_VALUE;
154+
} else {
155+
requested = total;
156+
}
157+
}
158+
}
159+
149160
/**
150161
* @warn javadoc description missing
151162
* @warn param producer not described
152-
* @param producer
163+
* @param p
153164
*/
154-
public void setProducer(Producer producer) {
165+
public void setProducer(Producer p) {
155166
long toRequest;
156-
boolean setProducer = false;
167+
boolean passToSubscriber = false;
157168
synchronized (this) {
158169
toRequest = requested;
159-
p = producer;
160-
if (op != null) {
170+
producer = p;
171+
if (subscriber != null) {
161172
// middle operator ... we pass thru unless a request has been made
162-
if (toRequest == Long.MIN_VALUE) {
173+
if (toRequest == NOT_SET) {
163174
// we pass-thru to the next producer as nothing has been requested
164-
setProducer = true;
175+
passToSubscriber = true;
165176
}
166-
167177
}
168178
}
169179
// do after releasing lock
170-
if (setProducer) {
171-
op.setProducer(p);
180+
if (passToSubscriber) {
181+
subscriber.setProducer(producer);
172182
} else {
173183
// we execute the request with whatever has been requested (or Long.MAX_VALUE)
174-
if (toRequest == Long.MIN_VALUE) {
175-
p.request(Long.MAX_VALUE);
184+
if (toRequest == NOT_SET) {
185+
producer.request(Long.MAX_VALUE);
176186
} else {
177-
p.request(toRequest);
187+
producer.request(toRequest);
178188
}
179189
}
180190
}

0 commit comments

Comments
 (0)