Skip to content

Commit 5454879

Browse files
akarnokdakarnokd
authored andcommitted
Going lite
1 parent 265090c commit 5454879

File tree

1 file changed

+42
-46
lines changed

1 file changed

+42
-46
lines changed

rxjava-core/src/main/java/rx/subjects/BehaviorSubject.java

Lines changed: 42 additions & 46 deletions
Original file line numberDiff line numberDiff line change
@@ -19,10 +19,10 @@
1919
import java.util.ArrayList;
2020
import java.util.List;
2121
import java.util.concurrent.atomic.AtomicReference;
22-
import rx.Notification;
2322
import rx.Observer;
2423
import rx.Subscriber;
2524
import rx.functions.Action0;
25+
import rx.operators.NotificationLite;
2626
import rx.subscriptions.Subscriptions;
2727

2828
/**
@@ -90,18 +90,18 @@ public static <T> BehaviorSubject<T> create(T defaultValue) {
9090
private static <T> BehaviorSubject<T> create(T defaultValue, boolean hasDefault) {
9191
State<T> state = new State<T>();
9292
if (hasDefault) {
93-
state.set(Notification.createOnNext(defaultValue));
93+
state.set(NotificationLite.instance().next(defaultValue));
9494
}
9595
return new BehaviorSubject<T>(new BehaviorOnSubscribe<T>(state), state);
9696
}
9797

9898
static final class State<T> {
99-
final AtomicReference<Notification<T>> latest = new AtomicReference<Notification<T>>();
99+
final AtomicReference<Object> latest = new AtomicReference<Object>();
100100
final AtomicReference<BehaviorState> observers = new AtomicReference<BehaviorState>(BehaviorState.EMPTY);
101-
void set(Notification<T> value) {
101+
void set(Object value) {
102102
this.latest.set(value);
103103
}
104-
Notification<T> get() {
104+
Object get() {
105105
return latest.get();
106106
}
107107
BehaviorObserver<T>[] observers() {
@@ -133,11 +133,11 @@ void remove(BehaviorObserver<T> o) {
133133
}
134134
} while (true);
135135
}
136-
BehaviorObserver<T>[] next(Notification<T> n) {
136+
BehaviorObserver<T>[] next(Object n) {
137137
set(n);
138138
return observers.get().observers;
139139
}
140-
BehaviorObserver<T>[] terminate(Notification<T> n) {
140+
BehaviorObserver<T>[] terminate(Object n) {
141141
set(n);
142142
do {
143143
BehaviorState oldState = observers.get();
@@ -149,6 +149,9 @@ BehaviorObserver<T>[] terminate(Notification<T> n) {
149149
}
150150
} while (true);
151151
}
152+
boolean isActive() {
153+
return !observers.get().terminated;
154+
}
152155
}
153156
static final class BehaviorState {
154157
final boolean terminated;
@@ -228,6 +231,7 @@ public void call() {
228231

229232

230233
private final State<T> state;
234+
private final NotificationLite<T> nl = NotificationLite.instance();
231235

232236
protected BehaviorSubject(OnSubscribe<T> onSubscribe, State<T> state) {
233237
super(onSubscribe);
@@ -236,9 +240,9 @@ protected BehaviorSubject(OnSubscribe<T> onSubscribe, State<T> state) {
236240

237241
@Override
238242
public void onCompleted() {
239-
Notification<T> last = state.get();
240-
if (last == null || last.isOnNext()) {
241-
Notification<T> n = Notification.<T>createOnCompleted();
243+
Object last = state.get();
244+
if (last == null || state.isActive()) {
245+
Object n = nl.completed();
242246
for (BehaviorObserver<T> bo : state.terminate(n)) {
243247
bo.emitNext(n);
244248
}
@@ -247,9 +251,9 @@ public void onCompleted() {
247251

248252
@Override
249253
public void onError(Throwable e) {
250-
Notification<T> last = state.get();
251-
if (last == null || last.isOnNext()) {
252-
Notification<T> n = Notification.<T>createOnError(e);
254+
Object last = state.get();
255+
if (last == null || state.isActive()) {
256+
Object n = nl.error(e);
253257
for (BehaviorObserver<T> bo : state.terminate(n)) {
254258
bo.emitNext(n);
255259
}
@@ -258,9 +262,9 @@ public void onError(Throwable e) {
258262

259263
@Override
260264
public void onNext(T v) {
261-
Notification<T> last = state.get();
262-
if (last == null || last.isOnNext()) {
263-
Notification<T> n = Notification.createOnNext(v);
265+
Object last = state.get();
266+
if (last == null || state.isActive()) {
267+
Object n = nl.next(v);
264268
for (BehaviorObserver<T> bo : state.next(n)) {
265269
bo.emitNext(n);
266270
}
@@ -273,60 +277,52 @@ public void onNext(T v) {
273277

274278
private static final class BehaviorObserver<T> {
275279
final Observer<? super T> actual;
280+
final NotificationLite<T> nl = NotificationLite.instance();
276281
/** Guarded by this. */
277282
boolean first = true;
278283
/** Guarded by this. */
279284
boolean emitting;
280285
/** Guarded by this. */
281-
List<Notification<T>> queue;
286+
List<Object> queue;
282287
/** Accessed only from serialized state. */
283288
boolean done;
284-
volatile boolean fastPath;
289+
/* volatile */boolean fastPath;
285290
public BehaviorObserver(Observer<? super T> actual) {
286291
this.actual = actual;
287292
}
288-
void emitNext(Notification<T> n) {
289-
if (fastPath) {
290-
accept(n);
291-
return;
292-
}
293-
List<Notification<T>> localQueue;
294-
synchronized (this) {
295-
first = false;
296-
if (emitting) {
297-
if (queue == null) {
298-
queue = new ArrayList<Notification<T>>();
293+
void emitNext(Object n) {
294+
if (!fastPath) {
295+
synchronized (this) {
296+
first = false;
297+
if (emitting) {
298+
if (queue == null) {
299+
queue = new ArrayList<Object>();
300+
}
301+
queue.add(n);
302+
return;
299303
}
300-
queue.add(n);
301-
return;
302304
}
303-
emitting = true;
304-
localQueue = queue;
305-
queue = null;
305+
fastPath = true;
306306
}
307-
fastPath = true;
308-
emitLoop(localQueue, n);
307+
nl.accept(actual, n);
309308
}
310-
void emitFirst(Notification<T> n) {
311-
List<Notification<T>> localQueue;
309+
void emitFirst(Object n) {
312310
synchronized (this) {
313311
if (!first || emitting) {
314312
return;
315313
}
316314
first = false;
317315
emitting = true;
318-
localQueue = queue;
319-
queue = null;
320316
}
321-
emitLoop(localQueue, n);
317+
emitLoop(null, n);
322318
}
323-
void emitLoop(List<Notification<T>> localQueue, Notification<T> current) {
319+
void emitLoop(List<Object> localQueue, Object current) {
324320
boolean once = true;
325321
boolean skipFinal = false;
326322
try {
327323
do {
328324
if (localQueue != null) {
329-
for (Notification<T> n : localQueue) {
325+
for (Object n : localQueue) {
330326
accept(n);
331327
}
332328
}
@@ -352,12 +348,12 @@ void emitLoop(List<Notification<T>> localQueue, Notification<T> current) {
352348
}
353349
}
354350
}
355-
void accept(Notification<T> n) {
351+
void accept(Object n) {
356352
if (n != null && !done) {
357-
if (!n.isOnNext()) {
353+
if (nl.isCompleted(n) || nl.isError(n)) {
358354
done = true;
359355
}
360-
n.accept(actual);
356+
nl.accept(actual, n);
361357
}
362358
}
363359
}

0 commit comments

Comments
 (0)