org.crosswire.common.progress
Class Job

java.lang.Object
  extended by org.crosswire.common.progress.Job
All Implemented Interfaces:
Progress

public final class Job
extends Object
implements Progress

A Generic method of keeping track of Threads and monitoring their progress.

Author:
Joe Walker [joe at eireneh dot com]
See Also:
for license details.
The copyright to this program is held by it's authors.

Nested Class Summary
(package private)  class Job.PredictTask
          So we can fake progress for Jobs that don't tell us how they are doing
 
Field Summary
private  boolean cancelable
          Does this job allow interruptions?
private  Map current
          The timings as measured this time
private  boolean finished
          Have we just finished?
private  String jobName
          A short descriptive phrase
private  List listeners
          People that want to know about "cancelable" changes
private static Logger log
          The log stream
private  int percentEnd
          The percentage at the end of this section
private  Map predicted
          The timings loaded from where they were saved after the last run
private  int predictedLength
          How long to we predict this job is going to last?
private  URI predictURI
          The URI to which we load and save timings
private  long sectionEnd
          When do we expect this section to end
private  String sectionName
          Description of what we are doing
private  long sectionStart
          When did this section start?
private  long start
          When did this job start?
private  int totalWork
          Total amount of work to do.
private  Timer updater
          The timer that lets us post fake progress events
private  int work
          The officially reported progress
private  Thread workerThread
          Optional thread to monitor progress
 
Fields inherited from interface org.crosswire.common.progress.Progress
UNKNOWN
 
Constructor Summary
protected Job(String description, URI predictURI, Thread worker, int totalWork)
          Create a new Job.
 
Method Summary
 void addWorkListener(WorkListener li)
          Add a listener to the list
 void beginJob(String name, int workToDo)
          Start the task.
 void cancel()
          Cancel the job (if possible).
 void done()
          Called to indicate that we are finished doing work.
protected  void fireStateChanged()
           
private  int getAgeFromMap(Map props, String message)
          Predict a percentage complete
 String getJobName()
           
 String getSectionName()
          We have moved onto another section so update the section title.
 int getTotalWork()
           
 int getWork()
           
protected  void guessProgress()
          Get estimated the percent progress, extrapolating between sections
private  void ignoreTimings()
          Typically called from in a catch block, this ensures that we don't save the timing file because we have a messed up run.
 boolean isCancelable()
          Might the job be cancelable?
 boolean isFinished()
          Used to determine whether job is done or cancelled or reached totalWork.
private  void loadPredictions()
          Load the predictive timings if any
private  void predictSection(String message)
          Predict a percentage complete
 void removeWorkListener(WorkListener li)
          Remote a listener from the list
private  void savePredictions()
          Save the known timings to a properties file.
 void setCancelable(boolean newInterruptable)
          Indicates whether the job is cancelable or not.
 void setProgress(int work, String statedesc)
          We have moved onto another section so update the percentage complete and the section title.
 void setSectionName(String statedesc)
          We have moved onto another section so update the section title.
 void setWork(int work)
          Indicate progress toward the whole.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Field Detail

totalWork

private int totalWork
Total amount of work to do.


cancelable

private boolean cancelable
Does this job allow interruptions?


finished

private boolean finished
Have we just finished?


work

private int work
The officially reported progress


sectionEnd

private long sectionEnd
When do we expect this section to end


sectionStart

private long sectionStart
When did this section start?


percentEnd

private int percentEnd
The percentage at the end of this section


jobName

private String jobName
A short descriptive phrase


workerThread

private Thread workerThread
Optional thread to monitor progress


sectionName

private String sectionName
Description of what we are doing


start

private long start
When did this job start?


current

private Map current
The timings as measured this time


predicted

private Map predicted
The timings loaded from where they were saved after the last run


predictedLength

private int predictedLength
How long to we predict this job is going to last?


predictURI

private URI predictURI
The URI to which we load and save timings


updater

private Timer updater
The timer that lets us post fake progress events


listeners

private List listeners
People that want to know about "cancelable" changes


log

private static final Logger log
The log stream

Constructor Detail

Job

protected Job(String description,
              URI predictURI,
              Thread worker,
              int totalWork)
Create a new Job. This will automatically fire a workProgressed event to all WorkListeners, with the work property of this job set to 0.

Parameters:
description - Short description of this job
predictURI - Optional URI to save/load prediction times from
worker - Optional thread to use in request to stop worker
totalWork - the size of the work to do
Method Detail

beginJob

public void beginJob(String name,
                     int workToDo)
Description copied from interface: Progress
Start the task.

Specified by:
beginJob in interface Progress
Parameters:
name - the initial name of the job.
workToDo - the total amount that is to be worked. If UNKNOWN then the progress is to be guessed.

getTotalWork

public int getTotalWork()
Specified by:
getTotalWork in interface Progress
Returns:
the total amount of work to be done

setSectionName

public void setSectionName(String statedesc)
Description copied from interface: Progress
We have moved onto another section so update the section title.

Specified by:
setSectionName in interface Progress
Parameters:
statedesc - the name of the section

setWork

public void setWork(int work)
Description copied from interface: Progress
Indicate progress toward the whole.

Specified by:
setWork in interface Progress
Parameters:
work - a part of the whole.

setProgress

public void setProgress(int work,
                        String statedesc)
We have moved onto another section so update the percentage complete and the section title.


done

public void done()
Description copied from interface: Progress
Called to indicate that we are finished doing work.

Specified by:
done in interface Progress

getSectionName

public String getSectionName()
Description copied from interface: Progress
We have moved onto another section so update the section title.

Specified by:
getSectionName in interface Progress

cancel

public void cancel()
Description copied from interface: Progress
Cancel the job (if possible). If isCancelable() is false, then the job will be canceled if cancelable becomes true. There is no guarantee that

Specified by:
cancel in interface Progress

isCancelable

public boolean isCancelable()
Description copied from interface: Progress
Might the job be cancelable?

Specified by:
isCancelable in interface Progress

setCancelable

public void setCancelable(boolean newInterruptable)
Description copied from interface: Progress
Indicates whether the job is cancelable or not.

Specified by:
setCancelable in interface Progress
Parameters:
newInterruptable - The state to set.

isFinished

public boolean isFinished()
Description copied from interface: Progress
Used to determine whether job is done or cancelled or reached totalWork.

Specified by:
isFinished in interface Progress

getWork

public int getWork()
Specified by:
getWork in interface Progress
Returns:
the amount of work done so far, possibly estimated

getJobName

public String getJobName()
Specified by:
getJobName in interface Progress
Returns:
the job name

ignoreTimings

private void ignoreTimings()
Typically called from in a catch block, this ensures that we don't save the timing file because we have a messed up run.


addWorkListener

public void addWorkListener(WorkListener li)
Add a listener to the list


removeWorkListener

public void removeWorkListener(WorkListener li)
Remote a listener from the list


fireStateChanged

protected void fireStateChanged()

getAgeFromMap

private int getAgeFromMap(Map props,
                          String message)
Predict a percentage complete


guessProgress

protected void guessProgress()
Get estimated the percent progress, extrapolating between sections


predictSection

private void predictSection(String message)
Predict a percentage complete


loadPredictions

private void loadPredictions()
Load the predictive timings if any


savePredictions

private void savePredictions()
Save the known timings to a properties file.


Copyright ยจ 2003-2007