KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > mx4j > timer > TimerTask


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 import java.util.Date JavaDoc;
12 import javax.management.timer.TimerNotification JavaDoc;
13
14 /**
15  * A subclass of TimeTask for the JMX Timer service.
16  *
17  * @version $Revision: 1.8 $
18  */

19 public abstract class TimerTask extends TimeTask
20 {
21    private TimerNotification JavaDoc m_notification;
22    private long m_date;
23    private long m_period;
24    private long m_occurrences;
25    private long m_initialOccurrences;
26    private int m_hash;
27    private boolean m_fixedRate;
28
29    public TimerTask(TimerNotification JavaDoc n, Date JavaDoc date, long period, long occurrences, boolean fixedRate)
30    {
31       m_notification = n;
32       m_date = date.getTime();
33       m_period = period;
34       m_occurrences = occurrences;
35       m_initialOccurrences = occurrences;
36       m_fixedRate = fixedRate;
37
38       // Pre calculate hash code so that it does not reflect the fact that occurrences decrease
39
m_hash = new Long JavaDoc(getDate()).hashCode() ^ new Long JavaDoc(getPeriod()).hashCode() ^ new Long JavaDoc(getInitialOccurrences()).hashCode();
40
41       setNextExecutionTime(getDate());
42    }
43
44    public TimerNotification JavaDoc getNotification()
45    {
46       return m_notification;
47    }
48
49    public boolean isFinished()
50    {
51       return super.isFinished();
52    }
53
54    public void setFinished(boolean value)
55    {
56       super.setFinished(value);
57    }
58
59    public long getPeriod()
60    {
61       return m_period;
62    }
63
64    public boolean isPeriodic()
65    {
66       boolean periodic = getPeriod() > 0 && (getInitialOccurrences() == 0 || getOccurrences() > 0);
67       return periodic;
68    }
69
70    public long getNextExecutionTime()
71    {
72       return super.getNextExecutionTime();
73    }
74
75    public void setNextExecutionTime(long time)
76    {
77       super.setNextExecutionTime(time);
78       --m_occurrences;
79    }
80
81    public int hashCode()
82    {
83       return m_hash;
84    }
85
86    public boolean equals(Object JavaDoc obj)
87    {
88       if (obj == null) return false;
89       if (obj == this) return true;
90
91       try
92       {
93          TimerTask other = (TimerTask)obj;
94          return getDate() == other.getDate() && getPeriod() == other.getPeriod() && getInitialOccurrences() == other.getInitialOccurrences();
95       }
96       catch (ClassCastException JavaDoc x)
97       {
98          return false;
99       }
100    }
101
102    public long getOccurrences()
103    {
104       return m_occurrences;
105    }
106
107    private long getInitialOccurrences()
108    {
109       return m_initialOccurrences;
110    }
111
112    public long getDate()
113    {
114       return m_date;
115    }
116
117    public boolean getFixedRate()
118    {
119       return m_fixedRate;
120    }
121 }
122
Popular Tags