edu.jhu.pha.ivoa
Class Task

java.lang.Object
  |
  +--edu.jhu.pha.ivoa.Task
All Implemented Interfaces:
java.lang.Runnable
Direct Known Subclasses:
DownloadTask

public abstract class Task
extends java.lang.Object
implements java.lang.Runnable

   Current Version
   ===============
   ID:            $Id: Task.html,v 1.3 2003/08/18 18:05:02 carliles Exp $
   Revision:      $Revision: 1.3 $
   Date/time:     $Date: 2003/08/18 18:05:02 $
   
An abstract ecapsulation of the concept of a Task, which is similar to a Thread, but which keeps finer-grained status information, and which isn't cluttered with a ridiculous number of deprecated methods. It also has bean-like aspects, namely the ability to notify PropertyChangeListeners when its status or message has changed. Its primary use is as a controller for code execution that you want to a) break out into its own thread, and b) add to TaskManager to keep the user updated on its status and to give the user the option of cancelling it. You must provide an implementation for the execute() method, and it's recommended that you not override the run() method, although you are free to do so. A simple way to use the class looks like
     Task t = new Task("Reading files", "0 lines read.")
     {
       public void execute()
       {
         try
         {
           int linesRead = 0;
           BufferedReader in =
             new BufferedReader(new FileReader("/etc/passwd"));

           for(String line = in.readLine(); line != null; line = in.readLine())
           {
             if(line != null)
             {
               System.out.println(line);
               setMessage((++linesRead) + " lines read.");
             }
           }
           System.out.flush();
         }
         catch(IOException e)
         {
           e.printStackTrace();
         }
       }
     };
   


Field Summary
static int CANCELLED
           
static int COMPLETED
           
static int ERROR
           
static java.lang.String MESSAGE_PROPERTY
          The value returned by PropertyChangeEvent.getPropertyName() when the value of the message field of this Task changes.
static int RUNNING
           
static java.lang.String STATUS_PROPERTY
          The value returned by PropertyChangeEvent.getPropertyName() when the value of the status field of this Task changes.
static java.lang.String[] STATUS_STRINGS
           
static int WAITING
           
 
Constructor Summary
Task(java.lang.String name)
           
Task(java.lang.String name, java.lang.String message)
           
Task(java.lang.String name, java.lang.String message, java.beans.PropertyChangeListener listener)
           
 
Method Summary
 void addPropertyChangeListener(java.beans.PropertyChangeListener listener)
           
 void cancel()
          Cancels this Task, and sets its status to CANCELLED.
abstract  void execute()
          This abstract method, which must be implemented by any subclasses, is where the work done by this Task should take place.
 java.lang.String getMessage()
           
 java.lang.String getName()
           
 int getStatus()
           
 java.lang.String getStatusAsString()
           
 boolean isInterrupted()
           
 void join()
          Causes the thread calling this method to block until this Task has completed.
static void main(java.lang.String[] args)
          For testing purposes.
 void removePropertyChangeListener(java.beans.PropertyChangeListener listener)
           
static java.lang.String revision()
           
 void run()
          Sets the status to RUNNING, then calls the execute() method of this Task.
 void start()
          Starts this Task.
 
Methods inherited from class java.lang.Object
equals, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Field Detail

WAITING

public static final int WAITING
See Also:
Constant Field Values

RUNNING

public static final int RUNNING
See Also:
Constant Field Values

COMPLETED

public static final int COMPLETED
See Also:
Constant Field Values

CANCELLED

public static final int CANCELLED
See Also:
Constant Field Values

ERROR

public static final int ERROR
See Also:
Constant Field Values

STATUS_STRINGS

public static final java.lang.String[] STATUS_STRINGS

MESSAGE_PROPERTY

public static final java.lang.String MESSAGE_PROPERTY
The value returned by PropertyChangeEvent.getPropertyName() when the value of the message field of this Task changes.

See Also:
Constant Field Values

STATUS_PROPERTY

public static final java.lang.String STATUS_PROPERTY
The value returned by PropertyChangeEvent.getPropertyName() when the value of the status field of this Task changes.

See Also:
Constant Field Values
Constructor Detail

Task

public Task(java.lang.String name)
Parameters:
name - The name of this Task.

Task

public Task(java.lang.String name,
            java.lang.String message)
Parameters:
name - The name of this Task.
message - The initial value of the message field for this Task.

Task

public Task(java.lang.String name,
            java.lang.String message,
            java.beans.PropertyChangeListener listener)
Parameters:
name - The name of this Task.
message - The initial value of the message field for this Task.
listener - A PropertyChangeListener to be added to the list of PropertyChangeListeners of this Task.
Method Detail

main

public static void main(java.lang.String[] args)
                 throws java.lang.Exception
For testing purposes.

Throws:
java.lang.Exception

getName

public java.lang.String getName()

getStatus

public int getStatus()

getStatusAsString

public java.lang.String getStatusAsString()
Returns:
A String representation of this Task's status.

getMessage

public java.lang.String getMessage()

addPropertyChangeListener

public void addPropertyChangeListener(java.beans.PropertyChangeListener listener)

removePropertyChangeListener

public void removePropertyChangeListener(java.beans.PropertyChangeListener listener)

start

public void start()
Starts this Task.


cancel

public void cancel()
Cancels this Task, and sets its status to CANCELLED.


join

public void join()
          throws java.lang.InterruptedException
Causes the thread calling this method to block until this Task has completed.

Throws:
java.lang.InterruptedException

isInterrupted

public boolean isInterrupted()

run

public void run()
Sets the status to RUNNING, then calls the execute() method of this Task. After execute() returns, if status is still RUNNING, sets the status to COMPLETED.

Specified by:
run in interface java.lang.Runnable

execute

public abstract void execute()
This abstract method, which must be implemented by any subclasses, is where the work done by this Task should take place.


revision

public static java.lang.String revision()
Returns:
CVS Revision number.