KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > mx4j > timer > TimeTask


1 /*
2  * Copyright (C) The MX4J Contributors.
3  * All rights reserved.
4  *
5  * This software is distributed under the terms of the MX4J License version 1.0.
6  * See the terms of the MX4J License in the documentation provided with this software.
7  */

8
9 package mx4j.timer;
10
11 /**
12  * A task that is executed at a specified time. <p>
13  * Subclasses implement the periodicity, if needed.
14  * Two TimeTasks are compared with their neext execution time.
15  *
16  * @version $Revision: 1.3 $
17  */

18 public abstract class TimeTask implements Comparable JavaDoc, Runnable JavaDoc
19 {
20    private long executionTime;
21    private boolean finished;
22
23    /**
24     * Constructor for subclasses
25     */

26    protected TimeTask()
27    {
28    }
29
30    /**
31     * The method to implement to have this TimeTask to do something.
32     */

33    public abstract void run();
34
35    /**
36     * Returns whether this task is periodic. By default return false.
37     *
38     * @see #getPeriod
39     */

40    protected boolean isPeriodic()
41    {
42       return false;
43    }
44
45    /**
46     * Returns the period of this task. By default returns 0.
47     *
48     * @see #isPeriodic
49     */

50    protected long getPeriod()
51    {
52       return 0;
53    }
54
55    /**
56     * Returns whether this task is a fixed rate or fixed delay task. By default
57     * return false
58     */

59    public boolean getFixedRate()
60    {
61       return false;
62    }
63
64    /**
65     * Returns the next time at which the task will be executed, ie the {@link #run} method is called.
66     *
67     * @see #setNextExecutionTime
68     */

69    protected long getNextExecutionTime()
70    {
71       return executionTime;
72    }
73
74    /**
75     * Sets the next execution time.
76     *
77     * @see #getNextExecutionTime
78     */

79    protected void setNextExecutionTime(long time)
80    {
81       executionTime = time;
82    }
83
84    /**
85     * Marks this task as finished or not. When a task is finished, its
86     * {@link #run} method will not be called anymore.
87     *
88     * @see #isFinished
89     */

90    protected void setFinished(boolean value)
91    {
92       finished = value;
93    }
94
95    /**
96     * Returns whethere this task is finished.
97     *
98     * @see #setFinished
99     */

100    protected boolean isFinished()
101    {
102       return finished;
103    }
104
105    /**
106     * Compares 2 TimeTasks by comparing their next execution times
107     *
108     * @see #getNextExecutionTime
109     */

110    public int compareTo(Object JavaDoc obj)
111    {
112       if (obj == null) return 1;
113       if (obj == this) return 0;
114
115       TimeTask other = (TimeTask)obj;
116       long et = getNextExecutionTime();
117       long oet = other.getNextExecutionTime();
118       if (et > oet)
119          return 1;
120       else if (et < oet) return -1;
121       return 0;
122    }
123 }
124
Popular Tags