public final class OperatorSwitch<T> implements Operator<T, Observable<? extends T>> {
@Override
public Subscriber<? super Observable<? extends T>> call(final Subscriber<? super T> child) {
final SerializedSubscriber<T> s = new SerializedSubscriber<T>(child);
final SerialSubscription ssub = new SerialSubscription();
child.add(ssub);
return new Subscriber<Observable<? extends T>>(child) {
final Object guard = new Object();
final NotificationLite<?> nl = NotificationLite.instance();
/** Guarded by guard. */
int index;
/** Guarded by guard. */
boolean active;
/** Guarded by guard. */
boolean mainDone;
/** Guarded by guard. */
List<Object> queue;
/** Guarded by guard. */
boolean emitting;
@Override
public void onNext(Observable<? extends T> t) {
final int id;
synchronized (guard) {
id = ++index;
active = true;
}
Subscriber<T> sub = new Subscriber<T>() {
@Override
public void onNext(T t) {
emit(t, id);
}
@Override
public void onError(Throwable e) {
error(e, id);
}
@Override
public void onCompleted() {
complete(id);
}
};
ssub.set(sub);
t.unsafeSubscribe(sub);
}
@Override