Many resources are needed to download a project. Please understand that we have to compensate our server costs. Thank you in advance. Project price only 1 $
You can buy this project and download/modify it how often you want.
/**
* Copyright 2014 Netflix, Inc.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package rx.observables;
import java.util.Arrays;
import java.util.concurrent.atomic.*;
import rx.*;
import rx.Observable.OnSubscribe;
import rx.annotations.Experimental;
import rx.exceptions.CompositeException;
import rx.functions.*;
import rx.internal.operators.BackpressureUtils;
/**
* Abstract base class for the {@link OnSubscribe} interface that helps you build Observable sources one
* {@code onNext} at a time, and automatically supports unsubscription and backpressure.
*
*
Usage rules
* When you implement the {@code next()} method, you
*
*
should either
*
*
create the next value and signal it via {@link SubscriptionState#onNext state.onNext()},
*
signal a terminal condition via {@link SubscriptionState#onError state.onError()}, or
* {@link SubscriptionState#onCompleted state.onCompleted()}, or
*
signal a stop condition via {@link SubscriptionState#stop state.stop()} indicating no further values
* will be sent.
*
*
*
may
*
*
call {@link SubscriptionState#onNext state.onNext()} and either
* {@link SubscriptionState#onError state.onError()} or
* {@link SubscriptionState#onCompleted state.onCompleted()} together, and
*
block or sleep.
*
*
*
should not
*
*
do nothing or do async work and not produce any event or request stopping. If neither of
* the methods are called, an {@link IllegalStateException} is forwarded to the {@code Subscriber} and
* the Observable is terminated;
*
call the {@code state.on}foo() methods more than once (yields
* {@link IllegalStateException}).
*
*
*
*
* The {@link SubscriptionState} object features counters that may help implement a state machine:
*
*
A call counter, accessible via {@link SubscriptionState#calls state.calls()} tells how many times the
* {@code next()} was run (zero based).
*
You can use a phase counter, accessible via {@link SubscriptionState#phase state.phase}, that helps track
* the current emission phase, in a {@code switch()} statement to implement the state machine. (It is named
* {@code phase} to avoid confusion with the per-subscriber state.)
*
You can arbitrarily change the current phase with
* {@link SubscriptionState#advancePhase state.advancePhase()},
* {@link SubscriptionState#advancePhaseBy(int) state.advancedPhaseBy(int)} and
* {@link SubscriptionState#phase(int) state.phase(int)}.
*
*
* When you implement {@code AbstractOnSubscribe}, you may override {@link AbstractOnSubscribe#onSubscribe} to
* perform special actions (such as registering {@code Subscription}s with {@code Subscriber.add()}) and return
* additional state for each subscriber subscribing. You can access this custom state with the
* {@link SubscriptionState#state state.state()} method. If you need to do some cleanup, you can override the
* {@link #onTerminated} method.
*
* For convenience, a lambda-accepting static factory method, {@link #create}, is available.
* Another convenience is {@link #toObservable} which turns an {@code AbstractOnSubscribe}
* instance into an {@code Observable} fluently.
*
*
Examples
* Note: these examples use the lambda-helper factories to avoid boilerplate.
*
*
*
* @param the value type
* @param the per-subscriber user-defined state type
* @since (if this graduates from Experimental/Beta to supported, replace this parenthetical with the release number)
* @Experimental
*/
@Experimental
public abstract class AbstractOnSubscribe implements OnSubscribe {
/**
* Called when a Subscriber subscribes and lets the implementor create a per-subscriber custom state.
*
* Override this method to have custom state per-subscriber. The default implementation returns
* {@code null}.
*
* @param subscriber the subscriber who is subscribing
* @return the custom state
*/
protected S onSubscribe(Subscriber super T> subscriber) {
return null;
}
/**
* Called after the terminal emission or when the downstream unsubscribes.
*
* This is called only once and no {@code onNext} call will run concurrently with it. The default
* implementation does nothing.
*
* @param state the user-provided state
*/
protected void onTerminated(S state) {
}
/**
* Override this method to create an emission state-machine.
*
* @param state the per-subscriber subscription state
*/
protected abstract void next(SubscriptionState state);
@Override
public final void call(final Subscriber super T> subscriber) {
final S custom = onSubscribe(subscriber);
final SubscriptionState state = new SubscriptionState(this, subscriber, custom);
subscriber.add(new SubscriptionCompleter(state));
subscriber.setProducer(new SubscriptionProducer(state));
}
/**
* Convenience method to create an Observable from this implemented instance.
*
* @return the created observable
*/
public final Observable toObservable() {
return Observable.create(this);
}
/** Function that returns null. */
private static final Func1