Top Description Inners Fields Constructors Methods
rx.internal.operators

public final Class OnSubscribePublishMulticast<T>

extends AtomicInteger
implements Observable.OnSubscribe<T>, Observer<T>, Subscription
Class Inheritance
All Implemented Interfaces
rx.Subscription, rx.Observer, rx.Observable.OnSubscribe, rx.functions.Action1, rx.functions.Action, rx.functions.Function
Type Parameters
<T>
the input and output type
Imports
java.util.Queue, java.util.concurrent.atomic.*, rx.*, rx.exceptions.MissingBackpressureException, rx.internal.util.atomic.SpscAtomicArrayQueue, rx.internal.util.unsafe.*

Multicasts notifications coming through its input Subscriber view to its client Subscribers via lockstep backpressure mode.

The difference between this class and OperatorPublish is that this class doesn't consume the upstream if there are no child subscribers but waits for them to show up. Plus if the upstream source terminates, late subscribers will be immediately terminated with the same terminal event unlike OperatorPublish which just waits for the next connection.

The class extends AtomicInteger which is the work-in-progress gate for the drain-loop serializing subscriptions and child request changes.

Nested and Inner Type Summary

Modifier and TypeClass and Description
pack-priv static class
OnSubscribePublishMulticast.ParentSubscriber<
the input value type;
T
>

The subscriber that must be used for subscribing to the upstream source.

pack-priv static class
OnSubscribePublishMulticast.PublishProducer<
the output value type
T
>

A Producer and Subscription that wraps a child Subscriber and manages its backpressure requests along with its unsubscription from the parent class.

Field Summary

Modifier and TypeField and Description
pack-priv final boolean
delayError

Delays the error delivery to happen only after all values have been consumed.

pack-priv volatile boolean
done

Indicates the upstream has completed.

pack-priv static final OnSubscribePublishMulticast.PublishProducer<?>[]
EMPTY

Represents an empty array of subscriber wrapper, helps avoid allocating an empty array all the time.

pack-priv Throwable
error

Holds onto the upstream's exception if done is true and this field is non-null.

pack-priv final OnSubscribePublishMulticast.ParentSubscriber<T>
parent

The subscriber that can be 'connected' to the upstream source.

pack-priv final int
prefetch

The number of items to prefetch from the upstreams source.

pack-priv volatile Producer
producer

Holds the upstream producer if any, set through the parent subscriber.

pack-priv final Queue<T>
queue

The prefetch queue holding onto a fixed amount of items until all all child subscribers have requested something.

private static final long
pack-priv volatile OnSubscribePublishMulticast.PublishProducer<T>[]
subscribers

A copy-on-write array of currently subscribed child subscribers' wrapper structure.

pack-priv static final OnSubscribePublishMulticast.PublishProducer<?>[]
TERMINATED

Represents a final state for this class that prevents new subscribers from subscribing to it.

Constructor Summary

AccessConstructor and Description
public
OnSubscribePublishMulticast(int
the prefetch amount, > 0 required
prefetch
,
boolean
delay the error delivery after the normal items?
delayError
)

Constructor, initializes the fields

Method Summary

Modifier and TypeMethod and Description
pack-priv boolean

Returns:

true if successful, false if the terminal state has been reached in the meantime
add
(OnSubscribePublishMulticast.PublishProducer<T>
the wrapper
inner
)

Atomically adds the given wrapper of a child Subscriber to the subscribers array.

public void
call(Subscriber<? super T> t)

Implements rx.functions.Action1.call.

pack-priv boolean

Returns:

true if the class reached its terminal state
checkTerminated
(boolean
the source-done indicator
d
,
boolean
the queue-emptiness indicator
empty
)

Given the current source state, terminates all child subscribers.

pack-priv void
drain()

The serialization loop that determines the minimum request of all subscribers and tries to emit as many items from the queue if they are available.

public boolean
isUnsubscribed()

Implements rx.Subscription.isUnsubscribed.

Indicates whether this Subscription is currently unsubscribed.
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
t
)

Implements rx.Observer.onNext.

Provides the Observer with a new item to observe.
pack-priv void
remove(OnSubscribePublishMulticast.PublishProducer<T>
the wrapper to remove
inner
)

Atomically removes the given wrapper, if present, from the subscribers array.

pack-priv void
setProducer(Producer
the producer to set
p
)

Sets the main producer and issues the prefetch amount.

public Subscriber<T>

Returns:

the subscriber instance
subscriber
()

Returns the input subscriber of this class that must be subscribed to the upstream source.

pack-priv OnSubscribePublishMulticast.PublishProducer<T>[]

Returns:

the last set of subscribers before the state change or an empty array
terminate
()

Atomically swaps in the terminated state.

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.AtomicInteger:
accumulateAndGetaddAndGetcompareAndExchangecompareAndExchangeAcquirecompareAndExchangeReleasecompareAndSetdecrementAndGetdoubleValuefloatValuegetgetAcquiregetAndAccumulategetAndAddgetAndDecrementgetAndIncrementgetAndSetgetAndUpdategetOpaquegetPlainincrementAndGetintValuelazySetlongValuesetsetOpaquesetPlainsetReleasetoStringupdateAndGetweakCompareAndSetweakCompareAndSetAcquireweakCompareAndSetPlainweakCompareAndSetReleaseweakCompareAndSetVolatile

Field Detail

delayErrorback to summary
pack-priv final boolean delayError

Delays the error delivery to happen only after all values have been consumed.

doneback to summary
pack-priv volatile boolean done

Indicates the upstream has completed.

EMPTYback to summary
pack-priv static final OnSubscribePublishMulticast.PublishProducer<?>[] EMPTY

Represents an empty array of subscriber wrapper, helps avoid allocating an empty array all the time.

errorback to summary
pack-priv Throwable error

Holds onto the upstream's exception if done is true and this field is non-null.

This field must be read after done or if subscribers == TERMINATED to establish a proper happens-before.

parentback to summary
pack-priv final OnSubscribePublishMulticast.ParentSubscriber<T> parent

The subscriber that can be 'connected' to the upstream source.

prefetchback to summary
pack-priv final int prefetch

The number of items to prefetch from the upstreams source.

producerback to summary
pack-priv volatile Producer producer

Holds the upstream producer if any, set through the parent subscriber.

queueback to summary
pack-priv final Queue<T> queue

The prefetch queue holding onto a fixed amount of items until all all child subscribers have requested something.

serialVersionUIDback to summary
private static final long serialVersionUID

Hides java.util.concurrent.atomic.AtomicInteger.serialVersionUID.

subscribersback to summary
pack-priv volatile OnSubscribePublishMulticast.PublishProducer<T>[] subscribers

A copy-on-write array of currently subscribed child subscribers' wrapper structure.

TERMINATEDback to summary
pack-priv static final OnSubscribePublishMulticast.PublishProducer<?>[] TERMINATED

Represents a final state for this class that prevents new subscribers from subscribing to it.

Constructor Detail

OnSubscribePublishMulticastback to summary
public OnSubscribePublishMulticast(int prefetch, boolean delayError)

Constructor, initializes the fields

Parameters
prefetch:int

the prefetch amount, > 0 required

delayError:boolean

delay the error delivery after the normal items?

Annotations
@SuppressWarnings:unchecked
Exceptions
IllegalArgumentException:
if prefetch <= 0

Method Detail

addback to summary
pack-priv boolean add(OnSubscribePublishMulticast.PublishProducer<T> inner)

Atomically adds the given wrapper of a child Subscriber to the subscribers array.

Parameters
inner:OnSubscribePublishMulticast.PublishProducer<T>

the wrapper

Returns:boolean

true if successful, false if the terminal state has been reached in the meantime

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

Implements rx.functions.Action1.call.

Annotations
@Override
checkTerminatedback to summary
pack-priv boolean checkTerminated(boolean d, boolean empty)

Given the current source state, terminates all child subscribers.

Parameters
d:boolean

the source-done indicator

empty:boolean

the queue-emptiness indicator

Returns:boolean

true if the class reached its terminal state

drainback to summary
pack-priv void drain()

The serialization loop that determines the minimum request of all subscribers and tries to emit as many items from the queue if they are available.

The execution of the drain-loop is guaranteed to be thread-safe.

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
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 t)

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
t:T

the item emitted by the Observable

Annotations
@Override
removeback to summary
pack-priv void remove(OnSubscribePublishMulticast.PublishProducer<T> inner)

Atomically removes the given wrapper, if present, from the subscribers array.

Parameters
inner:OnSubscribePublishMulticast.PublishProducer<T>

the wrapper to remove

Annotations
@SuppressWarnings:unchecked
setProducerback to summary
pack-priv void setProducer(Producer p)

Sets the main producer and issues the prefetch amount.

Parameters
p:Producer

the producer to set

subscriberback to summary
public Subscriber<T> subscriber()

Returns the input subscriber of this class that must be subscribed to the upstream source.

Returns:Subscriber<T>

the subscriber instance

terminateback to summary
pack-priv OnSubscribePublishMulticast.PublishProducer<T>[] terminate()

Atomically swaps in the terminated state.

Returns:OnSubscribePublishMulticast.PublishProducer<T>[]

the last set of subscribers before the state change or an empty array

Annotations
@SuppressWarnings:unchecked
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.internal.operators back to summary

pack-priv final Class OnSubscribePublishMulticast.ParentSubscriber<T>

extends Subscriber<T>
Class Inheritance
Type Parameters
<T>
the input value type;

The subscriber that must be used for subscribing to the upstream source.

Field Summary

Modifier and TypeField and Description
pack-priv final OnSubscribePublishMulticast<T>
state

The reference to the parent state where the events are forwarded to.

Constructor Summary

AccessConstructor and Description
public

Method Summary

Modifier and TypeMethod and Description
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
t
)

Implements rx.Observer.onNext.

Provides the Observer with a new item to observe.
public void
setProducer(Producer
producer to be used by this subscriber or the other subscriber (or recursively its other subscriber) to make requests from
p
)

Overrides rx.Subscriber.setProducer.

If other subscriber is set (by calling constructor Subscriber(Subscriber) or Subscriber(Subscriber, boolean)) then this method calls setProducer on the other subscriber.
Inherited from rx.Subscriber:
addisUnsubscribedonStartrequestunsubscribe

Field Detail

stateback to summary
pack-priv final OnSubscribePublishMulticast<T> state

The reference to the parent state where the events are forwarded to.

Constructor Detail

ParentSubscriberback to summary
public ParentSubscriber(OnSubscribePublishMulticast<T> state)

Method Detail

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 t)

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
t:T

the item emitted by the Observable

Annotations
@Override
setProducerback to summary
public void setProducer(Producer p)

Overrides rx.Subscriber.setProducer.

Doc from rx.Subscriber.setProducer.

If other subscriber is set (by calling constructor Subscriber(Subscriber) or Subscriber(Subscriber, boolean)) then this method calls setProducer on the other subscriber. If the other subscriber is not set and no requests have been made to this subscriber then p.request(Long.MAX_VALUE) is called. If the other subscriber is not set and some requests have been made to this subscriber then p.request(n) is called where n is the accumulated requests to this subscriber.

Parameters
p:Producer

producer to be used by this subscriber or the other subscriber (or recursively its other subscriber) to make requests from

Annotations
@Override
rx.internal.operators back to summary

pack-priv final Class OnSubscribePublishMulticast.PublishProducer<T>

extends AtomicLong
implements Producer, Subscription
Class Inheritance
All Implemented Interfaces
rx.Subscription, rx.Producer
Type Parameters
<T>
the output value type

A Producer and Subscription that wraps a child Subscriber and manages its backpressure requests along with its unsubscription from the parent class.

The class extends AtomicLong that holds onto the child's requested amount.

Field Summary

Modifier and TypeField and Description
pack-priv final Subscriber<? super T>
actual

The actual subscriber to receive the events.

pack-priv final AtomicBoolean
once

Makes sure unsubscription happens only once.

pack-priv final OnSubscribePublishMulticast<T>
parent

The parent object to request draining or removal.

private static final long

Constructor Summary

AccessConstructor and Description
public

Method Summary

Modifier and TypeMethod and Description
public boolean
isUnsubscribed()

Implements rx.Subscription.isUnsubscribed.

Indicates whether this Subscription is currently unsubscribed.
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.
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

actualback to summary
pack-priv final Subscriber<? super T> actual

The actual subscriber to receive the events.

onceback to summary
pack-priv final AtomicBoolean once

Makes sure unsubscription happens only once.

parentback to summary
pack-priv final OnSubscribePublishMulticast<T> parent

The parent object to request draining or removal.

serialVersionUIDback to summary
private static final long serialVersionUID

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

Constructor Detail

PublishProducerback to summary
public PublishProducer(Subscriber<? super T> actual, OnSubscribePublishMulticast<T> parent)

Method Detail

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
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
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