Top Description Inners Constructors Methods
rx.observables

public abstract Class SyncOnSubscribe<S, T>

extends Object
implements Observable.OnSubscribe<T>
Class Inheritance
All Implemented Interfaces
rx.Observable.OnSubscribe, rx.functions.Action1, rx.functions.Action, rx.functions.Function
Known Direct Subclasses
rx.observables.SyncOnSubscribe.SyncOnSubscribeImpl
Type Parameters
<S>
the type of the user-define state used in generateState(S) , next(S, Subscriber), and onUnsubscribe(S).
<T>
the type of Subscribers that will be compatible with this.
Imports
java.util.concurrent.atomic.AtomicLong, rx.*, .Observable.OnSubscribe, rx.exceptions.Exceptions, rx.functions.*, rx.internal.operators.BackpressureUtils, rx.plugins.RxJavaHooks

A utility class to create OnSubscribe<T> functions that responds correctly to back pressure requests from subscribers. This is an improvement over Observable.create(OnSubscribe) which does not provide any means of managing back pressure requests out-of-the-box.
Since
1.2

Nested and Inner Type Summary

Modifier and TypeClass and Description
pack-priv static class
SyncOnSubscribe.SubscriptionProducer<S,
the type of compatible Subscribers
T
>

Contains the producer loop that reacts to downstream requests of work.

pack-priv static class
SyncOnSubscribe.SyncOnSubscribeImpl<
the type of the user-defined state
S
,
the type of compatible Subscribers
T
>

An implementation of SyncOnSubscribe that delegates SyncOnSubscribe#next(Object, Observer), SyncOnSubscribe#generateState(), and SyncOnSubscribe#onUnsubscribe(Object) to provided functions/closures.

Constructor Summary

AccessConstructor and Description
public

Method Summary

Modifier and TypeMethod and Description
public final void
call(final Subscriber<? super T> subscriber)

Implements rx.functions.Action1.call.

public static <
the type of the associated state with each Subscriber
S
,
the type of the generated values
T
>
SyncOnSubscribe<S, T>

Returns:

a SyncOnSubscribe that emits data in a protocol compatible with back-pressure.
createSingleState
(Func0<? extends S>
generates the initial state value (see generateState())
generator
,
final Action2<? super S, ? super Observer<? super T>>
produces data to the downstream subscriber (see next(S, Subscriber))
next
)

Generates a synchronous SyncOnSubscribe that calls the provided next function to generate data to downstream subscribers.

public static <
the type of the associated state with each Subscriber
S
,
the type of the generated values
T
>
SyncOnSubscribe<S, T>

Returns:

a SyncOnSubscribe that emits data downstream in a protocol compatible with back-pressure.
createSingleState
(Func0<? extends S>
generates the initial state value (see generateState())
generator
,
final Action2<? super S, ? super Observer<? super T>>
produces data to the downstream subscriber (see next(S, Subscriber))
next
,
final Action1<? super S>
clean up behavior (see onUnsubscribe(S))
onUnsubscribe
)

Generates a synchronous SyncOnSubscribe that calls the provided next function to generate data to downstream subscribers.

public static <
the type of the associated state with each Subscriber
S
,
the type of the generated values
T
>
SyncOnSubscribe<S, T>

Returns:

a SyncOnSubscribe that emits data downstream in a protocol compatible with back-pressure.
createStateful
(Func0<? extends S>
generates the initial state value (see generateState())
generator
,
Func2<? super S, ? super Observer<? super T>, ? extends S>
produces data to the downstream subscriber (see next(S, Subscriber))
next
,
Action1<? super S>
clean up behavior (see onUnsubscribe(S))
onUnsubscribe
)

Generates a synchronous SyncOnSubscribe that calls the provided next function to generate data to downstream subscribers.

public static <
the type of the associated state with each Subscriber
S
,
the type of the generated values
T
>
SyncOnSubscribe<S, T>

Returns:

a SyncOnSubscribe that emits data downstream in a protocol compatible with back-pressure.
createStateful
(Func0<? extends S>
generates the initial state value (see generateState())
generator
,
Func2<? super S, ? super Observer<? super T>, ? extends S>
produces data to the downstream subscriber (see next(S, Subscriber))
next
)

Generates a synchronous SyncOnSubscribe that calls the provided next function to generate data to downstream subscribers.

public static <
the type of the generated values
T
>
SyncOnSubscribe<Void, T>

Returns:

a SyncOnSubscribe that emits data downstream in a protocol compatible with back-pressure.
createStateless
(final Action1<? super Observer<? super T>>
produces data to the downstream subscriber (see next(S, Subscriber))
next
)

Generates a synchronous SyncOnSubscribe that calls the provided next function to generate data to downstream subscribers.

public static <
the type of the generated values
T
>
SyncOnSubscribe<Void, T>

Returns:

a SyncOnSubscribe that emits data downstream in a protocol compatible with back-pressure.
createStateless
(final Action1<? super Observer<? super T>>
produces data to the downstream subscriber (see next(S, Subscriber))
next
,
final Action0
clean up behavior (see onUnsubscribe(S))
onUnsubscribe
)

Generates a synchronous SyncOnSubscribe that calls the provided next function to generate data to downstream subscribers.

protected abstract S

Returns:

the initial state value
generateState
()

Executed once when subscribed to by a subscriber (via call(Subscriber)) to produce a state value.

protected abstract S

Returns:

the next iteration's state value
next
(S
the state value (from generateState() on the first invocation or the previous invocation of this method.
state
,
Observer<? super T>
the observer of data emitted by
observer
)

Called to produce data to the downstream subscribers.

protected void
onUnsubscribe(S
the last state value prior from generateState() or next(S, Observer<T>) before unsubscribe.
state
)

Clean up behavior that is executed after the downstream subscriber's subscription is unsubscribed.

Inherited from java.lang.Object:
cloneequalsfinalizegetClasshashCodenotifynotifyAlltoStringwaitwaitwait

Constructor Detail

SyncOnSubscribeback to summary
public SyncOnSubscribe()

Method Detail

callback to summary
public final void call(final Subscriber<? super T> subscriber)

Implements rx.functions.Action1.call.

Annotations
@Override
createSingleStateback to summary
public static <S, T> SyncOnSubscribe<S, T> createSingleState(Func0<? extends S> generator, final Action2<? super S, ? super Observer<? super T>> next)

Generates a synchronous SyncOnSubscribe that calls the provided next function to generate data to downstream subscribers.

Parameters
<S>
the type of the associated state with each Subscriber
<T>
the type of the generated values
generator:Func0<? extends S>

generates the initial state value (see generateState())

next:Action2<? super S, ? super Observer<? super T>>

produces data to the downstream subscriber (see next(S, Subscriber))

Returns:SyncOnSubscribe<S, T>

a SyncOnSubscribe that emits data in a protocol compatible with back-pressure.

Since
1.3
createSingleStateback to summary
public static <S, T> SyncOnSubscribe<S, T> createSingleState(Func0<? extends S> generator, final Action2<? super S, ? super Observer<? super T>> next, final Action1<? super S> onUnsubscribe)

Generates a synchronous SyncOnSubscribe that calls the provided next function to generate data to downstream subscribers. This overload creates a SyncOnSubscribe without an explicit clean up step.

Parameters
<S>
the type of the associated state with each Subscriber
<T>
the type of the generated values
generator:Func0<? extends S>

generates the initial state value (see generateState())

next:Action2<? super S, ? super Observer<? super T>>

produces data to the downstream subscriber (see next(S, Subscriber))

onUnsubscribe:Action1<? super S>

clean up behavior (see onUnsubscribe(S))

Returns:SyncOnSubscribe<S, T>

a SyncOnSubscribe that emits data downstream in a protocol compatible with back-pressure.

Since
1.3
createStatefulback to summary
public static <S, T> SyncOnSubscribe<S, T> createStateful(Func0<? extends S> generator, Func2<? super S, ? super Observer<? super T>, ? extends S> next, Action1<? super S> onUnsubscribe)

Generates a synchronous SyncOnSubscribe that calls the provided next function to generate data to downstream subscribers.

Parameters
<S>
the type of the associated state with each Subscriber
<T>
the type of the generated values
generator:Func0<? extends S>

generates the initial state value (see generateState())

next:Func2<? super S, ? super Observer<? super T>, ? extends S>

produces data to the downstream subscriber (see next(S, Subscriber))

onUnsubscribe:Action1<? super S>

clean up behavior (see onUnsubscribe(S))

Returns:SyncOnSubscribe<S, T>

a SyncOnSubscribe that emits data downstream in a protocol compatible with back-pressure.

Since
1.3
createStatefulback to summary
public static <S, T> SyncOnSubscribe<S, T> createStateful(Func0<? extends S> generator, Func2<? super S, ? super Observer<? super T>, ? extends S> next)

Generates a synchronous SyncOnSubscribe that calls the provided next function to generate data to downstream subscribers.

Parameters
<S>
the type of the associated state with each Subscriber
<T>
the type of the generated values
generator:Func0<? extends S>

generates the initial state value (see generateState())

next:Func2<? super S, ? super Observer<? super T>, ? extends S>

produces data to the downstream subscriber (see next(S, Subscriber))

Returns:SyncOnSubscribe<S, T>

a SyncOnSubscribe that emits data downstream in a protocol compatible with back-pressure.

Since
1.3
createStatelessback to summary
public static <T> SyncOnSubscribe<Void, T> createStateless(final Action1<? super Observer<? super T>> next)

Generates a synchronous SyncOnSubscribe that calls the provided next function to generate data to downstream subscribers. This overload creates a "state-less" SyncOnSubscribe which does not have an explicit state value. This should be used when the next function closes over it's state.

Parameters
<T>
the type of the generated values
next:Action1<? super Observer<? super T>>

produces data to the downstream subscriber (see next(S, Subscriber))

Returns:SyncOnSubscribe<Void, T>

a SyncOnSubscribe that emits data downstream in a protocol compatible with back-pressure.

Since
1.3
createStatelessback to summary
public static <T> SyncOnSubscribe<Void, T> createStateless(final Action1<? super Observer<? super T>> next, final Action0 onUnsubscribe)

Generates a synchronous SyncOnSubscribe that calls the provided next function to generate data to downstream subscribers. This overload creates a "state-less" SyncOnSubscribe which does not have an explicit state value. This should be used when the next function closes over it's state.

Parameters
<T>
the type of the generated values
next:Action1<? super Observer<? super T>>

produces data to the downstream subscriber (see next(S, Subscriber))

onUnsubscribe:Action0

clean up behavior (see onUnsubscribe(S))

Returns:SyncOnSubscribe<Void, T>

a SyncOnSubscribe that emits data downstream in a protocol compatible with back-pressure.

Since
1.3
generateStateback to summary
protected abstract S generateState()

Executed once when subscribed to by a subscriber (via call(Subscriber)) to produce a state value. This value is passed into next(S state, Observer <T> observer) on the first iteration. Subsequent iterations of next will receive the state returned by the previous invocation of next.

Returns:S

the initial state value

nextback to summary
protected abstract S next(S state, Observer<? super T> observer)

Called to produce data to the downstream subscribers. To emit data to a downstream subscriber call observer.onNext(t). To signal an error condition call observer.onError(throwable) or throw an Exception. To signal the end of a data stream call observer.onCompleted(). Implementations of this method must follow the following rules.

  • Must not call observer.onNext(t) more than 1 time per invocation.
  • Must not call observer.onNext(t) concurrently.
The value returned from an invocation of this method will be passed in as the state argument of the next invocation of this method.
Parameters
state:S

the state value (from generateState() on the first invocation or the previous invocation of this method.

observer:Observer<? super T>

the observer of data emitted by

Returns:S

the next iteration's state value

onUnsubscribeback to summary
protected void onUnsubscribe(S state)

Clean up behavior that is executed after the downstream subscriber's subscription is unsubscribed. This method will be invoked exactly once.

Parameters
state:S

the last state value prior from generateState() or next(S, Observer<T>) before unsubscribe.

rx.observables back to summary

pack-priv final Class SyncOnSubscribe.SubscriptionProducer<S, T>

extends AtomicLong
implements Producer, Subscription, Observer<T>
Class Inheritance
All Implemented Interfaces
rx.Observer, rx.Subscription, rx.Producer
Type Parameters
<T>
the type of compatible Subscribers

Contains the producer loop that reacts to downstream requests of work.

Field Summary

Modifier and TypeField and Description
private final Subscriber<? super T>
private boolean
private boolean
private final SyncOnSubscribe<S, T>
private static final long
private S

Constructor Summary

AccessConstructor and Description
pack-priv
SubscriptionProducer(final Subscriber<? super T> subscriber, SyncOnSubscribe<S, T> parent, S state)

Method Summary

Modifier and TypeMethod and Description
private void
private void
private void
public boolean
isUnsubscribed()

Implements rx.Subscription.isUnsubscribed.

Indicates whether this Subscription is currently unsubscribed.
private void
nextIteration(final SyncOnSubscribe<S, T> parent)

public void
onCompleted()

Implements rx.Observer.onCompleted.

Notifies the Observer that the Observable has finished sending push-based notifications.
public void
onError(Throwable
the exception encountered by the Observable
e
)

Implements rx.Observer.onError.

Notifies the Observer that the Observable has experienced an error condition.
public void
onNext(T
the item emitted by the Observable
value
)

Implements rx.Observer.onNext.

Provides the Observer with a new item to observe.
public void
request(long
the maximum number of items you want this Producer to produce, or Long.MAX_VALUE if you want the Producer to produce items at its own pace
n
)

Implements rx.Producer.request.

Request a certain maximum number of items from this Producer.
private void
slowPath(long n)

private boolean
public void
unsubscribe()

Implements rx.Subscription.unsubscribe.

Stops the receipt of notifications on the Subscriber that was registered when this Subscription was received.
Inherited from java.util.concurrent.atomic.AtomicLong:
accumulateAndGetaddAndGetcompareAndExchangecompareAndExchangeAcquirecompareAndExchangeReleasecompareAndSetdecrementAndGetdoubleValuefloatValuegetgetAcquiregetAndAccumulategetAndAddgetAndDecrementgetAndIncrementgetAndSetgetAndUpdategetOpaquegetPlainincrementAndGetintValuelazySetlongValuesetsetOpaquesetPlainsetReleasetoStringupdateAndGetweakCompareAndSetweakCompareAndSetAcquireweakCompareAndSetPlainweakCompareAndSetReleaseweakCompareAndSetVolatile

Field Detail

actualSubscriberback to summary
private final Subscriber<? super T> actualSubscriber
hasTerminatedback to summary
private boolean hasTerminated
onNextCalledback to summary
private boolean onNextCalled
parentback to summary
private final SyncOnSubscribe<S, T> parent
serialVersionUIDback to summary
private static final long serialVersionUID

Hides java.util.concurrent.atomic.AtomicLong.serialVersionUID.

stateback to summary
private S state

Constructor Detail

SubscriptionProducerback to summary
pack-priv SubscriptionProducer(final Subscriber<? super T> subscriber, SyncOnSubscribe<S, T> parent, S state)

Method Detail

doUnsubscribeback to summary
private void doUnsubscribe()
fastPathback to summary
private void fastPath()
handleThrownErrorback to summary
private void handleThrownError(Subscriber<? super T> a, Throwable ex)
isUnsubscribedback to summary
public boolean isUnsubscribed()

Implements rx.Subscription.isUnsubscribed.

Doc from rx.Subscription.isUnsubscribed.

Indicates whether this Subscription is currently unsubscribed.

Returns:boolean

true if this Subscription is currently unsubscribed, false otherwise

Annotations
@Override
nextIterationback to summary
private void nextIteration(final SyncOnSubscribe<S, T> parent)
onCompletedback to summary
public void onCompleted()

Implements rx.Observer.onCompleted.

Doc from rx.Observer.onCompleted.

Notifies the Observer that the Observable has finished sending push-based notifications.

The Observable will not call this method if it calls onError.

Annotations
@Override
onErrorback to summary
public void onError(Throwable e)

Implements rx.Observer.onError.

Doc from rx.Observer.onError.

Notifies the Observer that the Observable has experienced an error condition.

If the Observable calls this method, it will not thereafter call onNext or onCompleted.

Parameters
e:Throwable

the exception encountered by the Observable

Annotations
@Override
onNextback to summary
public void onNext(T value)

Implements rx.Observer.onNext.

Doc from rx.Observer.onNext.

Provides the Observer with a new item to observe.

The Observable may call this method 0 or more times.

The Observable will not call this method again after it calls either onCompleted or onError.

Parameters
value:T

the item emitted by the Observable

Annotations
@Override
requestback to summary
public void request(long n)

Implements rx.Producer.request.

Doc from rx.Producer.request.

Request a certain maximum number of items from this Producer. This is a way of requesting backpressure. To disable backpressure, pass Long.MAX_VALUE to this method.

Requests are additive but if a sequence of requests totals more than Long.MAX_VALUE then Long.MAX_VALUE requests will be actioned and the extras may be ignored. Arriving at Long.MAX_VALUE by addition of requests cannot be assumed to disable backpressure. For example, the code below may result in Long.MAX_VALUE requests being actioned only.

request(100);
request(Long.MAX_VALUE-1);
Parameters
n:long

the maximum number of items you want this Producer to produce, or Long.MAX_VALUE if you want the Producer to produce items at its own pace

Annotations
@Override
slowPathback to summary
private void slowPath(long n)
tryUnsubscribeback to summary
private boolean tryUnsubscribe()
unsubscribeback to summary
public void unsubscribe()

Implements rx.Subscription.unsubscribe.

Doc from rx.Subscription.unsubscribe.

Stops the receipt of notifications on the Subscriber that was registered when this Subscription was received.

This allows deregistering an Subscriber before it has finished receiving all events (i.e. before onCompleted is called).

Annotations
@Override
rx.observables back to summary

pack-priv final Class SyncOnSubscribe.SyncOnSubscribeImpl<S, T>

extends SyncOnSubscribe<S, T>
Class Inheritance
Type Parameters
<S>
the type of the user-defined state
<T>
the type of compatible Subscribers

An implementation of SyncOnSubscribe that delegates SyncOnSubscribe#next(Object, Observer), SyncOnSubscribe#generateState(), and SyncOnSubscribe#onUnsubscribe(Object) to provided functions/closures.

Field Summary

Modifier and TypeField and Description
private final Func0<? extends S>
private final Func2<? super S, ? super Observer<? super T>, ? extends S>
private final Action1<? super S>

Constructor Summary

AccessConstructor and Description
pack-priv
SyncOnSubscribeImpl(Func0<? extends S> generator, Func2<? super S, ? super Observer<? super T>, ? extends S> next, Action1<? super S> onUnsubscribe)

public
SyncOnSubscribeImpl(Func0<? extends S> generator, Func2<? super S, ? super Observer<? super T>, ? extends S> next)

public
SyncOnSubscribeImpl(Func2<S, Observer<? super T>, S> next, Action1<? super S> onUnsubscribe)

public
SyncOnSubscribeImpl(Func2<S, Observer<? super T>, S> nextFunc)

Method Summary

Modifier and TypeMethod and Description
protected S
generateState()

Implements abstract rx.observables.SyncOnSubscribe.generateState.

Executed once when subscribed to by a subscriber (via call(Subscriber)) to produce a state value.
protected S
next(S
the state value (from generateState() on the first invocation or the previous invocation of this method.
state
,
Observer<? super T>
the observer of data emitted by
observer
)

Implements abstract rx.observables.SyncOnSubscribe.next.

Called to produce data to the downstream subscribers.
protected void
onUnsubscribe(S
the last state value prior from generateState() or next(S, Observer<T>) before unsubscribe.
state
)

Overrides rx.observables.SyncOnSubscribe.onUnsubscribe.

Clean up behavior that is executed after the downstream subscriber's subscription is unsubscribed.
Inherited from rx.observables.SyncOnSubscribe:
callcreateSingleStatecreateSingleStatecreateStatefulcreateStatefulcreateStatelesscreateStateless

Field Detail

generatorback to summary
private final Func0<? extends S> generator
nextback to summary
private final Func2<? super S, ? super Observer<? super T>, ? extends S> next
onUnsubscribeback to summary
private final Action1<? super S> onUnsubscribe

Constructor Detail

SyncOnSubscribeImplback to summary
pack-priv SyncOnSubscribeImpl(Func0<? extends S> generator, Func2<? super S, ? super Observer<? super T>, ? extends S> next, Action1<? super S> onUnsubscribe)
SyncOnSubscribeImplback to summary
public SyncOnSubscribeImpl(Func0<? extends S> generator, Func2<? super S, ? super Observer<? super T>, ? extends S> next)
SyncOnSubscribeImplback to summary
public SyncOnSubscribeImpl(Func2<S, Observer<? super T>, S> next, Action1<? super S> onUnsubscribe)
SyncOnSubscribeImplback to summary
public SyncOnSubscribeImpl(Func2<S, Observer<? super T>, S> nextFunc)

Method Detail

generateStateback to summary
protected S generateState()

Implements abstract rx.observables.SyncOnSubscribe.generateState.

Doc from rx.observables.SyncOnSubscribe.generateState.

Executed once when subscribed to by a subscriber (via call(Subscriber)) to produce a state value. This value is passed into next(S state, Observer <T> observer) on the first iteration. Subsequent iterations of next will receive the state returned by the previous invocation of next.

Returns:S

the initial state value

Annotations
@Override
nextback to summary
protected S next(S state, Observer<? super T> observer)

Implements abstract rx.observables.SyncOnSubscribe.next.

Doc from rx.observables.SyncOnSubscribe.next.

Called to produce data to the downstream subscribers. To emit data to a downstream subscriber call observer.onNext(t). To signal an error condition call observer.onError(throwable) or throw an Exception. To signal the end of a data stream call observer.onCompleted(). Implementations of this method must follow the following rules.

  • Must not call observer.onNext(t) more than 1 time per invocation.
  • Must not call observer.onNext(t) concurrently.
The value returned from an invocation of this method will be passed in as the state argument of the next invocation of this method.
Parameters
state:S

the state value (from generateState() on the first invocation or the previous invocation of this method.

observer:Observer<? super T>

the observer of data emitted by

Returns:S

the next iteration's state value

Annotations
@Override
onUnsubscribeback to summary
protected void onUnsubscribe(S state)

Overrides rx.observables.SyncOnSubscribe.onUnsubscribe.

Doc from rx.observables.SyncOnSubscribe.onUnsubscribe.

Clean up behavior that is executed after the downstream subscriber's subscription is unsubscribed. This method will be invoked exactly once.

Parameters
state:S

the last state value prior from generateState() or next(S, Observer<T>) before unsubscribe.

Annotations
@Override