sam.event
Class ProgressEventNotifier

java.lang.Object
  |
  +--sam.event.ProgressEventNotifier

public class ProgressEventNotifier
extends java.lang.Object

Class for dispatching ProgressEvent through registered ProgressEventListeners. Typical usage is:


 ProgressEventNotifier notifier = new ProgressEventNotifier();
 notifier.register( new ProgressEventListener() {
    public void progress(Event event) { System.out.println(event); }  // Or anything else
 } );
 OutputStream output = notifier.new EncodedOutputStream( new FileOutputStream("encoded.dat") );
 output = ... ; // which includes various filtering operations called "encoding"
 output = notifier.new PlainOutputStream(output);
 ... ; // Write plain bytes of data to output
 
Note that event posting is asynchronous in order to preserve computationnal resources of the main thread.

See Also:
Settings.Constants#PROGRESS_NOTIFICATION_PRIORITY

Nested Class Summary
 class ProgressEventNotifier.EncodedInputStream
          Instance inner-class for notifying the number of bytes read from an encoded InputStream.
 class ProgressEventNotifier.EncodedOutputStream
          Instance inner-class for notifying the number of encoded bytes written to an OutputStream.
 class ProgressEventNotifier.PlainInputStream
          Instance inner-class for notifying the number of bytes read from a plain InputStream.
 class ProgressEventNotifier.PlainOutputStream
          Instance inner-class for notifying the number of plain bytes written to an OutputStream.
 
Constructor Summary
ProgressEventNotifier()
          Builds a new ProgressEvent notifier.
 
Method Summary
protected  sam.event.ProgressEvent createEvent()
          Creates a new ProgressEvent describing the current progress state
protected  void finalize()
          On destruction the postmaster thread must be stopped.
protected  void notifyEncoded(long encodedIncrement)
          Notifies more bytes of encoded data to have been processed.
protected  void notifyListeners()
          Notifies all registered listeners of the current progress state Note that notification is asynchronous.
 void notifyNextEntry(sam.entry.Entry entry)
          Notifies a new entry being processed.
protected  void notifyPlain(long plainIncrement)
          Notifies more bytes of plain data to have been processed This is called automatically by inner input/output streams
 void register(sam.event.ProgressEventListener listener)
          Registers a new ProgressEvent listener
 void setTotalNumberOfEntries(int totalNumberOfEntries)
          Sets the total number of Entry that have to be processed
 void setTotalNumberOfPlainBytes(long totalNumberOfPlainBytes)
          Sets the total number of bytes that have to be processed
 void start()
          Starts the event postmaster thread.
 void terminate()
          Notifies the termination of the process.
 void unregister(sam.event.ProgressEventListener listener)
          Unregisters a previously registered ProgressEvent listener
 
Methods inherited from class java.lang.Object
clone, equals, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Constructor Detail

ProgressEventNotifier

public ProgressEventNotifier()
Builds a new ProgressEvent notifier. Note that a call to setTotalNumberOfPlainBytes and one to setTotalNumberOfEntries are required before proper use. This also starts one postmaster thread, which will be stopped once this ProgressEventNotifier is elected by the GC for finalization.

Method Detail

setTotalNumberOfPlainBytes

public void setTotalNumberOfPlainBytes(long totalNumberOfPlainBytes)
Sets the total number of bytes that have to be processed


setTotalNumberOfEntries

public void setTotalNumberOfEntries(int totalNumberOfEntries)
Sets the total number of Entry that have to be processed


register

public void register(sam.event.ProgressEventListener listener)
Registers a new ProgressEvent listener


unregister

public void unregister(sam.event.ProgressEventListener listener)
Unregisters a previously registered ProgressEvent listener


notifyListeners

protected void notifyListeners()
Notifies all registered listeners of the current progress state Note that notification is asynchronous.


start

public void start()
Starts the event postmaster thread. Note that calling this several times is safe although of no use.


terminate

public void terminate()
Notifies the termination of the process. This is a blocking call that waits for all ProgressEvent to have been handled.


notifyPlain

protected void notifyPlain(long plainIncrement)
Notifies more bytes of plain data to have been processed This is called automatically by inner input/output streams


notifyEncoded

protected void notifyEncoded(long encodedIncrement)
Notifies more bytes of encoded data to have been processed. This is called automatically by inner input/output streams


notifyNextEntry

public void notifyNextEntry(sam.entry.Entry entry)
Notifies a new entry being processed. This is to be called manually at each new entry. Note that even entries that are to be skipped or ignored shall be notified here.


createEvent

protected final sam.event.ProgressEvent createEvent()
Creates a new ProgressEvent describing the current progress state

See Also:
ProgressEvent

finalize

protected void finalize()
On destruction the postmaster thread must be stopped. Note that this implementation does not call Thread.stop()

Overrides:
finalize in class java.lang.Object