E
- the type of events (entry values).N
- the type of concrete entries in the model.public abstract class AbstractIndexedEventModel<E extends IndexedEvent<?>,N extends AbstractIndexedEventModel.Entry<E>> extends Object implements AutoCloseable
Abstract model for a list of indexed events.
This class handles all snapshot and transaction logic of IndexedEvent
class and
arranges incoming events into a list ordered by their source
id
and index
. Note, that TimeSeriesEvent
extends IndexedEvent
in
such a way, that this ordering also orders time-series event by time
. However,
there is a time-series aware class TimeSeriesEventModel
that provides a more convenient way to
subscribe to time-series.
This abstract class provides only protected methods to access the underlying list of events:
size
, get
, listIterator
,
and entryListIterator
,
because it is designed for extension and is not designed for a direct use.
For a concrete implementation use IndexedEventModel
class.
The model must be configured with symbol
to subscribe for and
attached
to a DXFeed
instance to start operation.
Model change notifications are provided by an abstract modelChanged
method
that must be overridden in concrete implementation classes.
This class also provides abstract createEntry
method that must be overridden to return a
fresh instance of an AbstractIndexedEventModel.Entry
class that is used inside this class to wrap incoming events and
may be augmented with an additional event-related information if needed.
Users of this model only see the list of events in a consistent state. This model delays incoming events which
are part of incomplete snapshot or ongoing transaction until snapshot is complete or transaction has ended.
These pending events cannot be seen neither via get methods nor via modelChanged
calls,
and so eventFlags
of events in the model are set to zero.
The eventFlags are only used and must be taken into account when processing indexed events directly via low-level
DXFeedSubscription
class.
Attached model is a potential memory leak. If the pointer to attached model is lost, then there is no way to detach this model from the feed and the model will not be reclaimed by the garbage collector as long as the corresponding feed is still used. Detached model can be reclaimed by the garbage collector, but detaching model requires knowing the pointer to the feed at the place of the call, which is not always convenient.
The convenient way to detach model from the feed is to call its close
method. Closed model
becomes permanently detached from all feeds, removes all its listeners and is guaranteed to be reclaimable by
the garbage collector as soon as all external references to it are cleared.
This class is not tread-safe and requires external synchronization.
The only thread-safe methods are attach
, detach
and close
.
You must query the state of attached
model only from
inside of the notification invocations or from within the thread that performs
those notifications.
Notification on model changes are invoked from a separate thread via the executor.
Default executor for all models is configured with DXEndpoint.executor
method. Each model can individually override its executor with setExecutor
method. The corresponding
modelChanged
notification is guaranteed to never be concurrent, even though it may happen from different
threads if executor is multi-threaded.
In practice, it means that is UI applications you must
install UI-thread-bound execution to your DXEndpoint
via
DXEndpoint.executor
method, so that you
can freely use all methods of this model from UI thread.
Custom executor can be used by backend applications that do not need to immediately update this model on
arrival of new events, but want to update the model at a later time, for example, from inside of a servlet request.
This approach is explained with code samples in
Threads and locks
section of DXFeedSubscription
class documentation.
Modifier and Type | Class and Description |
---|---|
static class |
AbstractIndexedEventModel.Entry<V>
Represents an internal entry in the model.
|
Modifier | Constructor and Description |
---|---|
protected |
AbstractIndexedEventModel(Class<? extends E> eventType)
Creates new model.
|
protected |
AbstractIndexedEventModel(DXFeed feed,
Class<? extends E> eventType)
Creates new model attached to the specified feed.
|
Modifier and Type | Method and Description |
---|---|
void |
attach(DXFeed feed)
Attaches model to the specified feed.
|
void |
clear()
Clears subscription symbol and, subsequently, all events in this model.
|
void |
close()
Closes this model and makes it permanently detached.
|
protected abstract N |
createEntry()
Creates new concrete entry to represent an event in this model.
|
void |
detach(DXFeed feed)
Detaches model from the specified feed.
|
protected ListIterator<N> |
entryListIterator()
Returns a list iterator over the entries in this model (in proper sequence).
|
protected E |
get(int index)
Returns the element at the specified position in this model.
|
Executor |
getExecutor()
Returns executor for processing event notifications on this model.
|
int |
getSizeLimit()
Returns size limit of this model.
|
Object |
getSymbol()
Returns model subscription symbol, or
null is not subscribed
(this is a default value). |
protected boolean |
isClosed() |
protected boolean |
isSnapshotEnd(E event)
Returns
true when this event ends the snapshot. |
protected ListIterator<E> |
listIterator()
Returns a list iterator over the elements in this model (in proper sequence).
|
protected ListIterator<E> |
listIterator(int index)
Returns a list iterator over the elements in this model (in proper
sequence), starting at the specified position in the model.
|
protected abstract void |
modelChanged(List<N> changedEntries)
Invoked on the change of this model.
|
void |
setExecutor(Executor executor)
Changes executor for processing event notifications on this model.
|
void |
setSizeLimit(int sizeLimit)
Changes size limit of this model.
|
void |
setSymbol(Object symbol)
Changes symbol for this model to subscribe for.
|
protected int |
size()
Returns the number of elements in this model.
|
protected AbstractIndexedEventModel(Class<? extends E> eventType)
setSymbol(java.lang.Object)
to specify subscription symbol and
attach(com.dxfeed.api.DXFeed)
method to specify feed to start receiving events.eventType
- the event type.NullPointerException
- if event type is null.protected AbstractIndexedEventModel(DXFeed feed, Class<? extends E> eventType)
setSymbol(java.lang.Object)
to specify subscription symbol.feed
- feed to attach to.eventType
- the event type.NullPointerException
- if event type is null.public void attach(DXFeed feed)
feed
- feed to attach to.public void detach(DXFeed feed)
feed
- feed to detach from.protected boolean isClosed()
public void close()
This method ensures that model can be safely garbage-collected when all outside references to it are lost.
close
in interface AutoCloseable
public Executor getExecutor()
null
if default executor of the attached DXFeed
is used.public void setExecutor(Executor executor)
executor
- executor for processing event notifications on this model,
or null
if default executor of the attached DXFeed
is used.public void clear()
setSymbol
(null)
.public Object getSymbol()
null
is not subscribed
(this is a default value).public void setSymbol(Object symbol)
symbol
- model subscription symbol, use null
to unsubscribe.public int getSizeLimit()
Integer.MAX_VALUE
by default (no limit).public void setSizeLimit(int sizeLimit)
sizeLimit
- size limit of this model.IllegalArgumentException
- if sizeLimit
is negative.protected int size()
sizeLimit
property.protected E get(int index)
index
- index of the element to return.IndexOutOfBoundsException
- if the index is out of range
(index < 0 || index >= size()).protected ListIterator<E> listIterator()
protected ListIterator<E> listIterator(int index)
index
- index of the first element to be returned from the
list iterator (by a call to next
).IndexOutOfBoundsException
- if the index is out of range
(index < 0 || index > size()
).protected ListIterator<N> entryListIterator()
protected abstract N createEntry()
protected abstract void modelChanged(List<N> changedEntries)
changedEntries
list
have changed
flag set to true
and the new values of
incoming events are available via Entry.getNewValue
method.
The changed flags is cleared
after return from this method by Entry.commitChange
method, which can also
be invoked during this method, if needed.changedEntries
- the list of changed entries.protected boolean isSnapshotEnd(E event)
true
when this event ends the snapshot. This implementations
return true when either SNAPSHOT_END
or SNAPSHOT_SNIP
flag is set in eventFlags
.Copyright © 2002–2023 Devexperts LLC. All rights reserved.