com.devexperts.qd
Class SubscriptionBuffer

java.lang.Object
  extended by com.devexperts.qd.SubscriptionBuffer
All Implemented Interfaces:
SubscriptionConsumer, SubscriptionIterator, SubscriptionProvider, SubscriptionVisitor

public class SubscriptionBuffer
extends java.lang.Object
implements SubscriptionConsumer, SubscriptionProvider, SubscriptionIterator, SubscriptionVisitor

The SubscriptionBuffer class is an universal buffer for subscription, able to receive and retrieve subscription via all appropriate interfaces and via random access. It uses cyclic buffers for efficient subscription storage.

NOTE: the SubscriptionBuffer is not thread-safe; it is designed to be used in a single-thread mode.


Field Summary
static SubscriptionBuffer VOID
          The instance of SubscriptionBuffer that is always empty.
 
Constructor Summary
SubscriptionBuffer()
           
 
Method Summary
 void clear()
          Clears this subscription buffer.
protected static void copy(java.lang.Object src, java.lang.Object dst, int head, int tail, int length)
          Copies cyclic array buffer from source to destination.
protected  void ensureCapacity(DataRecord record)
          Ensures that this buffer has capacity to visit and store specified record.
 void examineRecord(int index, SubscriptionVisitor visitor)
          Examines record by its index within this buffer via specivied visitor.
 boolean examineSubscription(SubscriptionVisitor visitor)
          Examines accumulated subscription via specified subscription visitor.
 SubscriptionIterator examiningIterator()
          Returns subscription iterator that examines this subscription buffer.
 int getCipher()
          Returns cipher for the current record returned by last call to SubscriptionIterator.nextRecord().
 int getCipher(int index)
          Returns cipher of the record by its index within this buffer.
 DataRecord getRecord(int index)
          Returns record by its index within this buffer.
 java.lang.String getSymbol()
          Returns symbol for the current record returned by last call to SubscriptionIterator.nextRecord().
 java.lang.String getSymbol(int index)
          Returns symbol of the record by its index within this buffer.
 long getTime()
          Returns time for the current record returned by last call to SubscriptionIterator.nextRecord().
 long getTime(int index)
          Returns time of the record by its index within this buffer.
 boolean hasCapacity()
          Returns whether visitor has capacity to efficiently visit next record.
 boolean isEmpty()
          Returns true if this subscription buffer has no records.
 DataRecord nextRecord()
          Returns next record.
protected  void notifyListener()
          Notifies subscription listener used in SubscriptionProvider part of this subscription buffer.
static boolean process(SubscriptionIterator iterator, SubscriptionVisitor visitor)
          Processes subscription from specified iterator via specified visitor.
 void processSubscription(SubscriptionIterator iterator)
          Processes subscription from specified subscription iterator.
 boolean retrieveSubscription(SubscriptionVisitor visitor)
          Retrieves accumulated subscription into specified subscription visitor.
 void setSubscriptionListener(SubscriptionListener listener)
          Sets new subscription listener to receive notifications about subscription.
 int size()
          Returns number of records in this subscription buffer.
 void visitRecord(DataRecord record, int cipher, java.lang.String symbol)
          Visits next record using time = 0.
 void visitRecord(DataRecord record, int cipher, java.lang.String symbol, long time)
          Visits next record.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Field Detail

VOID

public static final SubscriptionBuffer VOID
The instance of SubscriptionBuffer that is always empty.

Constructor Detail

SubscriptionBuffer

public SubscriptionBuffer()
Method Detail

process

public static boolean process(SubscriptionIterator iterator,
                              SubscriptionVisitor visitor)
Processes subscription from specified iterator via specified visitor. Returns true if some subscription may still remain in the iterator or false if all subscription were processed.


size

public int size()
Returns number of records in this subscription buffer.


isEmpty

public boolean isEmpty()
Returns true if this subscription buffer has no records.


clear

public void clear()
Clears this subscription buffer.


getCipher

public int getCipher(int index)
Returns cipher of the record by its index within this buffer. Returns 0 if not encoded.

Throws:
java.lang.IndexOutOfBoundsException - if the index is out of range (index < 0 || index >= size()).

getSymbol

public java.lang.String getSymbol(int index)
Returns symbol of the record by its index within this buffer. Returns null if encoded.

Throws:
java.lang.IndexOutOfBoundsException - if the index is out of range (index < 0 || index >= size()).

getTime

public long getTime(int index)
Returns time of the record by its index within this buffer. Returns 0 if not historical.

Throws:
java.lang.IndexOutOfBoundsException - if the index is out of range (index < 0 || index >= size()).

getRecord

public DataRecord getRecord(int index)
Returns record by its index within this buffer.

Throws:
java.lang.IndexOutOfBoundsException - if the index is out of range (index < 0 || index >= size()).

examineRecord

public void examineRecord(int index,
                          SubscriptionVisitor visitor)
Examines record by its index within this buffer via specivied visitor.

NOTE: unlike bulk transfer methods, this method does not check SubscriptionVisitor.hasCapacity() method of specified visitor.

Throws:
java.lang.IndexOutOfBoundsException - if the index is out of range (index < 0 || index >= size()).

examineSubscription

public boolean examineSubscription(SubscriptionVisitor visitor)
Examines accumulated subscription via specified subscription visitor. Unlike retrieval, examination does not consume processed subscription. Returns true if not all accumulated subscription were examined or false if all accumulated subscription were examined.


examiningIterator

public SubscriptionIterator examiningIterator()
Returns subscription iterator that examines this subscription buffer.

NOTE: returned iterator possesses no special protection against concurrent modifications of this buffer and it will fail in unexpected way in such event; it should be used up before any such modifications.


processSubscription

public void processSubscription(SubscriptionIterator iterator)
Description copied from interface: SubscriptionConsumer
Processes subscription from specified subscription iterator.

Specified by:
processSubscription in interface SubscriptionConsumer

retrieveSubscription

public boolean retrieveSubscription(SubscriptionVisitor visitor)
Description copied from interface: SubscriptionProvider
Retrieves accumulated subscription into specified subscription visitor. Returns true if some subscription still remains in the provider or false if all accumulated subscription were retrieved.

Specified by:
retrieveSubscription in interface SubscriptionProvider

setSubscriptionListener

public void setSubscriptionListener(SubscriptionListener listener)
Description copied from interface: SubscriptionProvider
Sets new subscription listener to receive notifications about subscription. Only one listener at a time is supported; the former listener is discarded. Use null to set empty subscription listener (no notifications).

NOTE: if there is accumulated subscription available, then specified listener will be notified by this method.

Specified by:
setSubscriptionListener in interface SubscriptionProvider

getCipher

public int getCipher()
Description copied from interface: SubscriptionIterator
Returns cipher for the current record returned by last call to SubscriptionIterator.nextRecord(). Returns 0 if not encoded or if no current record is being iterated.

Specified by:
getCipher in interface SubscriptionIterator

getSymbol

public java.lang.String getSymbol()
Description copied from interface: SubscriptionIterator
Returns symbol for the current record returned by last call to SubscriptionIterator.nextRecord(). Returns null if encoded or if no current record is being iterated.

Specified by:
getSymbol in interface SubscriptionIterator

getTime

public long getTime()
Description copied from interface: SubscriptionIterator
Returns time for the current record returned by last call to SubscriptionIterator.nextRecord(). Returns 0 if not historical or if no current record is being iterated.

Specified by:
getTime in interface SubscriptionIterator

nextRecord

public DataRecord nextRecord()
Description copied from interface: SubscriptionIterator
Returns next record. Returns null if no more records available.

Specified by:
nextRecord in interface SubscriptionIterator

hasCapacity

public boolean hasCapacity()
Description copied from interface: SubscriptionVisitor
Returns whether visitor has capacity to efficiently visit next record. This method may be used to advise subscription provider that it is desirable to stop current string of visiting and to keep remaining subscription. However, at present, subscription provider is not obliged to adhere to this method contract.

NOTE: subscription visitor must process all subscription that is passed to it via visitXXX calls no matter whether it has capacity to do it efficiently.

Specified by:
hasCapacity in interface SubscriptionVisitor

visitRecord

public void visitRecord(DataRecord record,
                        int cipher,
                        java.lang.String symbol,
                        long time)
Description copied from interface: SubscriptionVisitor
Visits next record.

Specified by:
visitRecord in interface SubscriptionVisitor

visitRecord

public void visitRecord(DataRecord record,
                        int cipher,
                        java.lang.String symbol)
Visits next record using time = 0.


notifyListener

protected void notifyListener()
Notifies subscription listener used in SubscriptionProvider part of this subscription buffer. Does not check actual subscription availability.


ensureCapacity

protected void ensureCapacity(DataRecord record)
Ensures that this buffer has capacity to visit and store specified record. If required, reallocates all internal buffers, copies data and rearranges heads and tails to start with 0.


copy

protected static void copy(java.lang.Object src,
                           java.lang.Object dst,
                           int head,
                           int tail,
                           int length)
Copies cyclic array buffer from source to destination. Uses source, source head index, source tail index and source length. The destination buffer will start with 0 and end with original size.