public class DefaultRecordLoop extends Object implements RecordLoop
RecordLoop
.
If this class is used directly, records are fetched from the record source and
supplied to listeners in a single thread. Subclasses may override some of the steps
in command execution to enable concurrent processing.
This class is mostly thread safe, with a few caveats :
RecordSource
implementations are not thread safe. Manipulating the record
source directly while it is used by the DefaultRecordLoop may lead to
unpredictable results.RecordSource
implementations should not call methods of DefaultRecordLoop
that is using it.RecordLoop.Command, RecordLoop.Event, RecordLoop.State
Constructor and Description |
---|
DefaultRecordLoop() |
Modifier and Type | Method and Description |
---|---|
void |
addLoopListener(LoopListener listener)
Adds a listener that will be receiving
LoopEvent s fired by this loop. |
void |
addRecordListener(RecordListener listener)
Adds a listener that will be processing records supplied by this loop.
|
void |
dispose()
Release all resources referenced by this loop.
|
boolean |
doNotCount(Object record)
If the specified record is being currently supplied to listeners, tells this loop not to count
it towards the number of records that will be reported by getTotalCountableSupplied().
|
void |
execute(RecordLoop.Command command,
Object... parameters)
Executes the specified command on this loop.
|
long |
getConsumed()
|
long |
getCountableConsumed()
|
Object |
getLastRecord()
Returns the last record successfully fetched by this loop from the record source.
|
List<LoopListener> |
getLoopListeners()
Returns a list of listeners receiving
LoopEvent s fired by this loop. |
LoopEvent |
getProgress()
Creates and returns
PROGRESS event describing the current state
of this record loop. |
List<RecordListener> |
getRecordListeners()
Returns a list of listeners processing records supplied by this loop.
|
RecordSource |
getRecordSource()
Returns RecordSource used by this loop.
|
RecordLoop.State |
getState()
Returns the current state of this loop.
|
long |
getSupplied()
|
long |
getTotalConsumed()
Returns the number of records consumed by listeners since the last
START event. |
long |
getTotalCountableConsumed()
Returns the number of countable records consumed by listeners since the last
START event. |
long |
getTotalSupplied()
Returns the number of records supplied to listeners since the last
START event. |
boolean |
isEnabled(RecordLoop.Command command,
Object... parameters)
Returns true if the specified command is enabled in the current state of this
loop, and a call to execute(command, parameters) might succeed.
|
boolean |
isInterruptRequested()
Returns true if the flag requesting stop or pause in the loop is set.
|
void |
removeLoopListener(LoopListener listener)
Removes the listener receiving
LoopEvent s fired by this loop. |
void |
removeRecordListener(RecordListener listener)
Removes the listener processing records supplied by this loop.
|
void |
setConfiguration(Object config)
Supplies an object used to configure this loop.
|
void |
setProgressByRecords(long numberOfRecords)
Sets progress reporting interval.
|
void |
setProgressByTime(long milliseconds)
Sets progress reporting interval.
|
void |
setRecordSource(RecordSource source)
Sets RecordSource used by this loop.
|
void |
setStopOnEOF(boolean stopOnEOF)
Sets "stop on end-of-source" flag.
|
void |
setStopOnRewind(boolean stopOnRewind)
Sets "stop on rewind" flag.
|
public void addRecordListener(RecordListener listener)
addRecordListener
in interface RecordLoop
public void removeRecordListener(RecordListener listener)
removeRecordListener
in interface RecordLoop
public List<RecordListener> getRecordListeners()
getRecordListeners
in interface RecordLoop
public void addLoopListener(LoopListener listener)
LoopEvent
s fired by this loop.addLoopListener
in interface RecordLoop
public void removeLoopListener(LoopListener listener)
LoopEvent
s fired by this loop.removeLoopListener
in interface RecordLoop
public List<LoopListener> getLoopListeners()
LoopEvent
s fired by this loop.getLoopListeners
in interface RecordLoop
public boolean isInterruptRequested()
isInterruptRequested
in interface RecordLoop
public RecordSource getRecordSource()
getRecordSource
in interface RecordLoop
public LoopEvent getProgress()
PROGRESS
event describing the current state
of this record loop. This method should be used in preference to individual convenience
getters (isInterruptRequested()
, getSupplied()
,
getTotalSupplied()
, getConsumed()
, getTotalConsumed()
,
getCountableConsumed()
, getTotalCountableConsumed()
) since it provides
a snapshot of this loop in a consistent state.getProgress
in interface RecordLoop
public RecordLoop.State getState()
getState
in interface RecordLoop
public long getSupplied()
LOOPING
phase, or zero the loop moved through
IDLE
state since then.getSupplied
in interface RecordLoop
public long getTotalSupplied()
START
event.getTotalSupplied
in interface RecordLoop
public long getConsumed()
LOOPING
phase, or zero the loop moved through
IDLE
state since then.getConsumed
in interface RecordLoop
public long getTotalConsumed()
START
event.getTotalConsumed
in interface RecordLoop
public long getCountableConsumed()
LOOPING
phase, or zero the loop moved through
IDLE
state since then.getCountableConsumed
in interface RecordLoop
public long getTotalCountableConsumed()
START
event.getTotalCountableConsumed
in interface RecordLoop
public Object getLastRecord()
START
event.getLastRecord
in interface RecordLoop
public void setRecordSource(RecordSource source)
CONFIGURE
event is fired once the source has been set.
Equivalent to execute(SET_SOURCE, source).setRecordSource
in interface RecordLoop
IllegalStateException
- if this loop is in LOOPING state.public void setConfiguration(Object config)
CONFIGURE
event is fired once configuration is complete.
Equivalent to execute(CONFIG, config).setConfiguration
in interface RecordLoop
IllegalStateException
- if this loop is in LOOPING state.public void setProgressByRecords(long numberOfRecords)
PROGRESS
event will be fired every numberOfRecords records when looping.
Setting the interval to zero (default) means no reporting.setProgressByRecords
in interface RecordLoop
public void setProgressByTime(long milliseconds)
PROGRESS
event will be fired every milliseconds milliseconds when looping.
Setting the interval to zero (default) means no reporting.setProgressByTime
in interface RecordLoop
public void setStopOnRewind(boolean stopOnRewind)
REWIND
command.
If set to true (default), this loop will transition to IDLE
before rewinding the source if REWIND
command is issued while
the loop is in READY
state. FINISH
event will be sent to LoopListener
s during the transition.public void setStopOnEOF(boolean stopOnEOF)
RecordLoop.Command.GO
or RecordLoop.Command.GO_N
commands.
If set to true (default), this loop will transition to IDLE
state (through READY
) if the record source is exhausted while executing
RecordLoop.Command.GO
or RecordLoop.Command.GO_N
commands. If set to false,
the loop will pause in READY
state.public boolean doNotCount(Object record)
doNotCount
in interface RecordLoop
public void dispose()
dispose
in interface RecordLoop
public boolean isEnabled(RecordLoop.Command command, Object... parameters)
RecordLoop.execute(Command, Object...)
for the description of
parameters used by different commands.
Since not all RecordSource
implementations are able to figure out whether loading
the requested record is possible before trying it, the fact that this method returned
true does not exclude the possibility of NoLoopRecordException being
thrown by the subsequent call to execute(command, parameters).
isEnabled
in interface RecordLoop
public void execute(RecordLoop.Command command, Object... parameters)
Commands:
Command | Parameters | Action | Enabled when |
---|---|---|---|
REWIND | [boolean stop] | Rewind the record source. If stop is true and the current state is READY, this loop goes to IDLE state before rewinding the source. If the stop parameter is omitted, the value of the "stop on rewind" flag is used. | IDLE, READY |
PREVIOUS | Supply the previous record from the record source to listeners. | IDLE, READY | |
PAUSE | Requests pause in event processing. No new records will be fetched from the source; once those already fetched are processed, this loop moves to READY state. | LOOPING | |
NEXT | Supply the next record from the record source to listeners. | IDLE, READY | |
GO_N | [int nRecords, boolean stop] | Supply the next nRecords records from the source to listeners. If stop is true, this loop goes to IDLE state once the specified number of records is processed (or the end of source is reached. Otherwise, the loop remaines in READY state. If the stop parameter is omitted, the value of the "stop on end-of-source" flag is used. | IDLE, READY |
GO | [boolean stop] | Supply records to listeners until the end of the source is reached. If stop is true, this loop goes to IDLE state once all remaining records in the source are processed. Otherwise, the loop remaines in READY state. If the stop parameter is omitted, the value of the "stop on end-of-source" flag is used. | IDLE, READY |
JUMP | [RecordTag tag] [false, int index] [true, int offset] |
Supply the specified record to listeners. If the first parameter is false, the second parameter is interpreted as an absolute index of the requested record. If the first parameter is true, the second parameter is interpreted as an offset with respect to the current record. | IDLE, READY |
REFRESH | Reload the current record from the source and supply it to listeners. | IDLE, READY | |
STOP | Request interrupt if in LOOPING; once interrupted, moves to IDLE state | IDLE, READY, LOOPING | |
CONFIG | [Object config] | Set loop configuration | IDLE, READY |
SET_SOURCE | [RecordSource source] | Set record source | IDLE, READY |
This method blocks until command execution is finished.
If any of the commands that supply records to listeners attempts to load a non-existing record,
an instance of NoSuchRecordException
thrown by the source will be reported by SUSPEND and FINISH
events fired by this loop at the end of the command execution, and will remain referenced by this loop
until the next command is issued. It can be retrieved by calling getProgress().getException().
IOException or UnsupportedOperationException thrown by the source while retrieving a
record is handled in the same way.
All other types of unchecked exceptions thrown by the source or by listeners terminate the program (this behavior can be changed in subclasses by overriding handleClientError(Throwable) and handleSourceError methods).
The method acquires state lock briefly to check whether the command can be executed, and either rejects the command by throwing IllegalStateException, or launches the command and modifies the state accordingly. No alien code (including loop listener notifications) is called while the lock is held.
execute
in interface RecordLoop
IllegalStateException
- if the specified command cannot be executed by this loop in the current state.Copyright © 2000-2012 FreeHEP. All Rights Reserved.