class SequentialSubscription implements Subscription, Cancellable {

    void switchTo(Subscription next) {
        requireNonNull(next);
        // No special concurrency considerations for sourceEmitted access is required in this method because we are
        // on the Subscriber thread in this method. We want to track the effective source requested for the purposes of
        // how much more request(n) is necessary below.
        long effectiveSourceRequested = sourceEmitted;
        for (;;) {
            final long currSourceRequested = sourceRequested;
            assert currSourceRequested != SWITCHING; // no concurrency on this method allowed
            if (currSourceRequested == CANCELLED) {
                next.cancel();
                break;
            } else if (sourceRequestedUpdater.compareAndSet(this, currSourceRequested, SWITCHING)) {
                assert currSourceRequested >= 0 || currSourceRequested == REQUESTED;
                final long currRequested = requested;
                if (currRequested < 0) { // invalid requestN is pending.
                    sourceRequested = CANCELLED;
                    next.request(currRequested);
                    break;
                }

                // sourceEmitted is stable here because we are on the Subscriber thread. We want to request the
                // difference between total requested and what has been emitted from the new subscription. We also
                // need to set the value of total requested below to make sure it is monotonically increasing.
                // effectiveSourceRequested ...[delta]... requested
                final long delta = currRequested - effectiveSourceRequested;
                assert delta >= 0;
                if (delta != 0) {
                    // There maybe concurrency with the Subscription thread, or synchronous delivery of data from
                    // request(n). In these cases we want to avoid "double request" from requested, so we track how much
                    // we have already requested and decrement it on future loop iterations.
                    effectiveSourceRequested = currRequested;
                    next.request(delta);
                }

                // Make the subscription visible before restoring the state of sourceRequested. If the Subscription
                // thread observes the sourceRequested change it will also observe the subscription change. The
                // Subscription thread also uses sourceRequested to make sure there is no concurrent invocation of
                // the switched Subscription.
                subscription = next;
                // We want to set sourceRequested to currRequested because we have already requested the delta between
                // the two above, and we want to ensure sourceRequested is always monotonically increasing
                // (besides control values) to prevent the Subscription thread from requesting from an old subscription.
                if (sourceRequestedUpdater.compareAndSet(this, SWITCHING, currRequested)) {
                    break;
                }
                // else the Subscription thread was active in the mean time, we need to process the pending event(s).
                // if the state is cancelled the Subscription thread defers to this thread to do the cancel on the
                // next loop invocation.
            }
        }
    }

      SequentialSubscription();
      SequentialSubscription(Subscription delegate);

}