Top Description Inners Fields Constructors Methods
io.reactivex.internal.operators.flowable

public final Class FlowableCache<T>

extends AbstractFlowableWithUpstream<T, T>
implements FlowableSubscriber<T>
Class Inheritance
All Implemented Interfaces
io.reactivex.FlowableSubscriber, org.reactivestreams.Subscriber
Type Parameters
<T>
the source element type
Imports
java.util.concurrent.atomic.*, org.reactivestreams.*, io.reactivex.*, io.reactivex.internal.subscriptions.SubscriptionHelper, io.reactivex.internal.util.BackpressureHelper, io.reactivex.plugins.RxJavaPlugins

An observable which auto-connects to another observable, caches the elements from that observable but allows terminating the connection and completing the cache.

Nested and Inner Type Summary

Modifier and TypeClass and Description
pack-priv static class
FlowableCache.CacheSubscription<
the value type
T
>

Hosts the downstream consumer and its current requested and replay states.

pack-priv static class
FlowableCache.Node<
the element type
T
>

Represents a segment of the cached item list as part of a linked-node-list structure.

Field Summary

Modifier and TypeField and Description
pack-priv final int
capacityHint

The number of items per cached nodes.

pack-priv volatile boolean
done

True if the source has terminated.

pack-priv static final FlowableCache.CacheSubscription<T>[]
EMPTY

A shared instance of an empty array of subscribers to avoid creating a new empty array when all subscribers cancel.

pack-priv Throwable
error

If subscribers is TERMINATED, this holds the terminal error if not null.

pack-priv final FlowableCache.Node<T>
head

The starting point of the cached items.

pack-priv final AtomicBoolean
once

The subscription to the source should happen at most once.

pack-priv volatile long
size

The total number of elements in the list available for reads.

pack-priv final AtomicReference<FlowableCache.CacheSubscription<T>[]>
subscribers

The current known array of subscriber state to notify.

pack-priv FlowableCache.Node<T>
tail

The current tail of the linked structure holding the items.

pack-priv int
tailOffset

How many items have been put into the tail node so far.

pack-priv static final FlowableCache.CacheSubscription<T>[]
TERMINATED

A shared instance indicating the source has no more events and there is no need to remember subscribers anymore.

Inherited from io.reactivex.internal.operators.flowable.AbstractFlowableWithUpstream:
source

Constructor Summary

AccessConstructor and Description
public
FlowableCache(Flowable<T>
the source to subscribe to for the first incoming subscriber
source
,
int
the number of items expected (reduce allocation frequency)
capacityHint
)

Constructs an empty, non-connected cache.

Method Summary

Modifier and TypeMethod and Description
pack-priv void
add(FlowableCache.CacheSubscription<T>
the consumer to add
consumer
)

Atomically adds the consumer to the subscribers copy-on-write array if the source has not yet terminated.

pack-priv long

Returns:

the number of currently cached event count
cachedEventCount
()

Returns the number of events currently cached.

pack-priv boolean

Returns:

true if the cache has Subscribers
hasSubscribers
()

Returns true if there are observers subscribed to this observable.

pack-priv boolean

Returns:

true if already connected
isConnected
()

Check if this cached observable is connected to its source.

public void
onComplete()

Implements org.reactivestreams.Subscriber.onComplete.

Successful terminal state.
public void
onError(Throwable
the throwable signaled
t
)

Implements org.reactivestreams.Subscriber.onError.

Failed terminal state.
public void
onNext(T
the element signaled
t
)

Implements org.reactivestreams.Subscriber.onNext.

Data notification sent by the Publisher in response to requests to Subscription#request(long).
public void
onSubscribe(Subscription
the Subscription that allows requesting data via Subscription#request(long)
s
)

Implements io.reactivex.FlowableSubscriber.onSubscribe.

Implementors of this method should make sure everything that needs to be visible in onNext(Object) is established before calling Subscription#request(long).
pack-priv void
remove(FlowableCache.CacheSubscription<T>
the consumer to remove
consumer
)

Atomically removes the consumer from the subscribers copy-on-write array.

pack-priv void
replay(FlowableCache.CacheSubscription<T>
the consumer to continue replaying items to
consumer
)

Replays the contents of this cache to the given consumer based on its current state and number of items requested by it.

protected void
subscribeActual(Subscriber<? super T>
the incoming Subscriber, never null
t
)

Implements abstract io.reactivex.Flowable.subscribeActual.

Operator implementations (both source and intermediate) should implement this method that performs the necessary business logic and handles the incoming Subscribers.
Inherited from io.reactivex.internal.operators.flowable.AbstractFlowableWithUpstream:
source

Field Detail

capacityHintback to summary
pack-priv final int capacityHint

The number of items per cached nodes.

doneback to summary
pack-priv volatile boolean done

True if the source has terminated.

EMPTYback to summary
pack-priv static final FlowableCache.CacheSubscription<T>[] EMPTY

A shared instance of an empty array of subscribers to avoid creating a new empty array when all subscribers cancel.

Annotations
@SuppressWarnings:rawtypes
errorback to summary
pack-priv Throwable error

If subscribers is TERMINATED, this holds the terminal error if not null.

onceback to summary
pack-priv final AtomicBoolean once

The subscription to the source should happen at most once.

sizeback to summary
pack-priv volatile long size

The total number of elements in the list available for reads.

subscribersback to summary
pack-priv final AtomicReference<FlowableCache.CacheSubscription<T>[]> subscribers

The current known array of subscriber state to notify.

tailback to summary
pack-priv FlowableCache.Node<T> tail

The current tail of the linked structure holding the items.

tailOffsetback to summary
pack-priv int tailOffset

How many items have been put into the tail node so far.

TERMINATEDback to summary
pack-priv static final FlowableCache.CacheSubscription<T>[] TERMINATED

A shared instance indicating the source has no more events and there is no need to remember subscribers anymore.

Annotations
@SuppressWarnings:rawtypes

Constructor Detail

FlowableCacheback to summary
public FlowableCache(Flowable<T> source, int capacityHint)

Constructs an empty, non-connected cache.

Parameters
source:Flowable<T>

the source to subscribe to for the first incoming subscriber

capacityHint:int

the number of items expected (reduce allocation frequency)

Annotations
@SuppressWarnings:unchecked

Method Detail

addback to summary
pack-priv void add(FlowableCache.CacheSubscription<T> consumer)

Atomically adds the consumer to the subscribers copy-on-write array if the source has not yet terminated.

Parameters
consumer:FlowableCache.CacheSubscription<T>

the consumer to add

cachedEventCountback to summary
pack-priv long cachedEventCount()

Returns the number of events currently cached.

Returns:long

the number of currently cached event count

hasSubscribersback to summary
pack-priv boolean hasSubscribers()

Returns true if there are observers subscribed to this observable.

Returns:boolean

true if the cache has Subscribers

isConnectedback to summary
pack-priv boolean isConnected()

Check if this cached observable is connected to its source.

Returns:boolean

true if already connected

onCompleteback to summary
public void onComplete()

Implements org.reactivestreams.Subscriber.onComplete.

Doc from org.reactivestreams.Subscriber.onComplete.

Successful terminal state.

No further events will be sent even if Subscription#request(long) is invoked again.

Annotations
@SuppressWarnings:unchecked
@Override
onErrorback to summary
public void onError(Throwable t)

Implements org.reactivestreams.Subscriber.onError.

Doc from org.reactivestreams.Subscriber.onError.

Failed terminal state.

No further events will be sent even if Subscription#request(long) is invoked again.

Parameters
t:Throwable

the throwable signaled

Annotations
@SuppressWarnings:unchecked
@Override
onNextback to summary
public void onNext(T t)

Implements org.reactivestreams.Subscriber.onNext.

Doc from org.reactivestreams.Subscriber.onNext.

Data notification sent by the Publisher in response to requests to Subscription#request(long).

Parameters
t:T

the element signaled

Annotations
@Override
onSubscribeback to summary
public void onSubscribe(Subscription s)

Implements io.reactivex.FlowableSubscriber.onSubscribe.

Doc from io.reactivex.FlowableSubscriber.onSubscribe.

Implementors of this method should make sure everything that needs to be visible in onNext(Object) is established before calling Subscription#request(long). In practice this means no initialization should happen after the request() call and additional behavior is thread safe in respect to onNext. Invoked after calling Publisher#subscribe(Subscriber).

No data will start flowing until Subscription#request(long) is invoked.

It is the responsibility of this Subscriber instance to call Subscription#request(long) whenever more data is wanted.

The Publisher will send notifications only in response to Subscription#request(long).

Parameters
s:Subscription

Doc from org.reactivestreams.Subscriber.onSubscribe.

the Subscription that allows requesting data via Subscription#request(long)

Annotations
@Override
removeback to summary
pack-priv void remove(FlowableCache.CacheSubscription<T> consumer)

Atomically removes the consumer from the subscribers copy-on-write array.

Parameters
consumer:FlowableCache.CacheSubscription<T>

the consumer to remove

Annotations
@SuppressWarnings:unchecked
replayback to summary
pack-priv void replay(FlowableCache.CacheSubscription<T> consumer)

Replays the contents of this cache to the given consumer based on its current state and number of items requested by it.

Parameters
consumer:FlowableCache.CacheSubscription<T>

the consumer to continue replaying items to

subscribeActualback to summary
protected void subscribeActual(Subscriber<? super T> t)

Implements abstract io.reactivex.Flowable.subscribeActual.

Doc from io.reactivex.Flowable.subscribeActual.

Operator implementations (both source and intermediate) should implement this method that performs the necessary business logic and handles the incoming Subscribers.

There is no need to call any of the plugin hooks on the current Flowable instance or the Subscriber; all hooks and basic safeguards have been applied by subscribe(Subscriber) before this method gets called.

Parameters
t:Subscriber<? super T>

the incoming Subscriber, never null

Annotations
@Override
io.reactivex.internal.operators.flowable back to summary

pack-priv final Class FlowableCache.CacheSubscription<T>

extends AtomicInteger
implements Subscription
Class Inheritance
All Implemented Interfaces
org.reactivestreams.Subscription
Type Parameters
<T>
the value type

Hosts the downstream consumer and its current requested and replay states. this holds the work-in-progress counter for the serialized replay.

Field Summary

Modifier and TypeField and Description
pack-priv final Subscriber<? super T>
pack-priv long
pack-priv FlowableCache.Node<T>
pack-priv int
pack-priv final FlowableCache<T>
pack-priv final AtomicLong
private static final long

Constructor Summary

AccessConstructor and Description
pack-priv
CacheSubscription(Subscriber<? super T>
the actual consumer
downstream
,
FlowableCache<T>
the parent that holds onto the cached items
parent
)

Constructs a new instance with the actual downstream consumer and the parent cache object.

Method Summary

Modifier and TypeMethod and Description
public void
cancel()

Implements org.reactivestreams.Subscription.cancel.

Request the Publisher to stop sending data and clean up resources.
public void
request(long
the strictly positive number of elements to requests to the upstream Publisher
n
)

Implements org.reactivestreams.Subscription.request.

No events will be sent by a Publisher until demand is signaled via this method.
Inherited from java.util.concurrent.atomic.AtomicInteger:
accumulateAndGetaddAndGetcompareAndExchangecompareAndExchangeAcquirecompareAndExchangeReleasecompareAndSetdecrementAndGetdoubleValuefloatValuegetgetAcquiregetAndAccumulategetAndAddgetAndDecrementgetAndIncrementgetAndSetgetAndUpdategetOpaquegetPlainincrementAndGetintValuelazySetlongValuesetsetOpaquesetPlainsetReleasetoStringupdateAndGetweakCompareAndSetweakCompareAndSetAcquireweakCompareAndSetPlainweakCompareAndSetReleaseweakCompareAndSetVolatile

Field Detail

downstreamback to summary
pack-priv final Subscriber<? super T> downstream
indexback to summary
pack-priv long index
nodeback to summary
pack-priv FlowableCache.Node<T> node
offsetback to summary
pack-priv int offset
parentback to summary
pack-priv final FlowableCache<T> parent
requestedback to summary
pack-priv final AtomicLong requested
serialVersionUIDback to summary
private static final long serialVersionUID

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

Constructor Detail

CacheSubscriptionback to summary
pack-priv CacheSubscription(Subscriber<? super T> downstream, FlowableCache<T> parent)

Constructs a new instance with the actual downstream consumer and the parent cache object.

Parameters
downstream:Subscriber<? super T>

the actual consumer

parent:FlowableCache<T>

the parent that holds onto the cached items

Method Detail

cancelback to summary
public void cancel()

Implements org.reactivestreams.Subscription.cancel.

Doc from org.reactivestreams.Subscription.cancel.

Request the Publisher to stop sending data and clean up resources.

Data may still be sent to meet previously signalled demand after calling cancel.

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

Implements org.reactivestreams.Subscription.request.

Doc from org.reactivestreams.Subscription.request.

No events will be sent by a Publisher until demand is signaled via this method.

It can be called however often and whenever needed—but if the outstanding cumulative demand ever becomes Long.MAX_VALUE or more, it may be treated by the Publisher as "effectively unbounded".

Whatever has been requested can be sent by the Publisher so only signal demand for what can be safely handled.

A Publisher can send less than is requested if the stream ends but then must emit either Subscriber#onError(Throwable) or Subscriber#onComplete().

Parameters
n:long

the strictly positive number of elements to requests to the upstream Publisher

Annotations
@Override
io.reactivex.internal.operators.flowable back to summary

pack-priv final Class FlowableCache.Node<T>

extends Object
Class Inheritance
Type Parameters
<T>
the element type

Represents a segment of the cached item list as part of a linked-node-list structure.

Field Summary

Modifier and TypeField and Description
pack-priv volatile FlowableCache.Node<T>
next

The next node if not null.

pack-priv final T[]
values

The array of values held by this node.

Constructor Summary

AccessConstructor and Description
pack-priv
Node(int capacityHint)

Method Summary

Inherited from java.lang.Object:
cloneequalsfinalizegetClasshashCodenotifynotifyAlltoStringwaitwaitwait

Field Detail

nextback to summary
pack-priv volatile FlowableCache.Node<T> next

The next node if not null.

valuesback to summary
pack-priv final T[] values

The array of values held by this node.

Constructor Detail

Nodeback to summary
pack-priv Node(int capacityHint)
Annotations
@SuppressWarnings:unchecked