Mp3dings 
20100427

de.mospace.lang
Class RunnableQueue

java.lang.Object
  extended by de.mospace.lang.RunnableQueue
All Implemented Interfaces:
Runnable

public class RunnableQueue
extends Object
implements Runnable

A queue for Runnables that adhere to the RunnableQueue.Job interface, enqueued jobs are run one after the other in the order in which they have been enqueued.

See Also:
Runnable

Nested Class Summary
static interface RunnableQueue.Job
          A Runnable that can be enqueued to a RunnableQueue.
 
Constructor Summary
RunnableQueue()
          Creates a new RunnableQueue with no jobs.
RunnableQueue(RunnableQueue.Job[] commands)
          Creates a new RunnableQueue with the specified jobs.
 
Method Summary
 void addChangeListener(ChangeListener l)
          Registers a listener that will be notified whenever this runnable queue starts or stops executing.
 void clear()
          Removes all jobs from the queue.
 boolean dequeue(RunnableQueue.Job p)
          Removes the first occurrence in this queue of the specified job.
 void enqueue(RunnableQueue.Job p)
          Adds a new job to the end of this queue.
protected  void fireStateChanged()
          Notifies all registered listeners that this runnable queue has started or stopped executing.
 RunnableQueue.Job getNextRunnable()
          Returns the job that will next start executing.
 RunnableQueue.Job getRunningRunnable()
          Returns the currently running job.
 void handleInput(String input)
          Forwards text input to the currently running job if it does not have another source of input.
 void interrupt()
          Stops execution of this queue.
 boolean isRunning()
          Returns whether this queue is currently running.
 void registerExceptionHandler(ExceptionHandler xh)
          Registers an exception handler for all jobs in this queue and for all threads started with this queue's start() method.
 void removeChangeListener(ChangeListener l)
          Requests that the specified listener no longer be notified when this runnable queue starts or stops executing.
 void run()
          Executes this queue.
 void start()
          Starts to execute this queue in a new thread.
 void stop()
          Stops the currently running job in this queue.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Constructor Detail

RunnableQueue

public RunnableQueue(RunnableQueue.Job[] commands)
Creates a new RunnableQueue with the specified jobs.

Parameters:
commands - an array of non-null jobs to initialize the queue with

RunnableQueue

public RunnableQueue()
Creates a new RunnableQueue with no jobs.

Method Detail

run

public void run()
Executes this queue. Jobs will be run one after the other in the order in which they have been enqueued.

Specified by:
run in interface Runnable
Throws:
IllegalStateException - when this queue is already running

registerExceptionHandler

public void registerExceptionHandler(ExceptionHandler xh)
Registers an exception handler for all jobs in this queue and for all threads started with this queue's start() method.

Parameters:
xh - an exception handler
See Also:
RunnableQueue.Job.registerExceptionHandler(de.mospace.lang.ExceptionHandler)

getNextRunnable

public RunnableQueue.Job getNextRunnable()
Returns the job that will next start executing.

Returns:
the job that will next start executing or null if no job is awaiting execution in the queue

getRunningRunnable

public RunnableQueue.Job getRunningRunnable()
Returns the currently running job.

Returns:
the currently running job from this queue or null if no job is running.

enqueue

public void enqueue(RunnableQueue.Job p)
Adds a new job to the end of this queue. If this queue is currently running then this job will be executed once the currently running job and the jobs enqueued before it have terminated. If this queue is not running then this job will not be executed unless run() or start() is called.

Parameters:
p - the job to add to the queue

dequeue

public boolean dequeue(RunnableQueue.Job p)
Removes the first occurrence in this queue of the specified job. If this queue does not contain the element, it is unchanged.

Parameters:
p - the job to dequeue
Returns:
true if the job was in the queue, false otherwise
See Also:
List.remove(Object)

isRunning

public boolean isRunning()
Returns whether this queue is currently running.

Returns:
whether this queue is currently running

start

public void start()
Starts to execute this queue in a new thread.

Throws:
IllegalStateException - when this queue is already running

stop

public void stop()
Stops the currently running job in this queue. If there are further jobs in the queue then these will be executed.

See Also:
RunnableQueue.Job.stop()

interrupt

public void interrupt()
Stops execution of this queue. The currently running job will be stopped and further jobs in the queue will not be executed and remain queued.

See Also:
RunnableQueue.Job.stop()

clear

public void clear()
Removes all jobs from the queue. Does not stop a currently running job.


handleInput

public void handleInput(String input)
                 throws IOException
Forwards text input to the currently running job if it does not have another source of input.

Parameters:
input - text input for the currently running job
Throws:
IOException
See Also:
RunnableQueue.Job.hasInput()

addChangeListener

public void addChangeListener(ChangeListener l)
Registers a listener that will be notified whenever this runnable queue starts or stops executing. A listener can differentiate the two cases by calling event.getSource().isRunning(), this will return true iff the queue has started.

Parameters:
l - a change listener to be notified of future changes in this queue's state

removeChangeListener

public void removeChangeListener(ChangeListener l)
Requests that the specified listener no longer be notified when this runnable queue starts or stops executing.

Parameters:
l - the change listener to remove

fireStateChanged

protected void fireStateChanged()
Notifies all registered listeners that this runnable queue has started or stopped executing. A listener can differentiate the two cases by calling event.getSource().isRunning(), this will return true iff the queue has started.


Mp3dings 
20100427