1 /*
2  * File    : TimerEvent.java
3  * Created : 21-Nov-2003
4  * By      : parg
5  *
6  * Azureus - a Java Bittorrent client
7  *
8  * This program is free software; you can redistribute it and/or modify
9  * it under the terms of the GNU General Public License as published by
10  * the Free Software Foundation; either version 2 of the License, or
11  * (at your option) any later version.
12  *
13  * This program is distributed in the hope that it will be useful,
14  * but WITHOUT ANY WARRANTY; without even the implied warranty of
15  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
16  * GNU General Public License for more details ( see the LICENSE file ).
17  *
18  * You should have received a copy of the GNU General Public License
19  * along with this program; if not, write to the Free Software
20  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
21  */
22 
23 package org.gudy.azureus2.core3.util;
24 
25 /**
26  * @author parg
27  *
28  */
29 
30 public class
31 TimerEvent
32 	extends		ThreadPoolTask
33 	implements 	Comparable<TimerEvent>
34 {
35 	private String					name;
36 
37 	private Timer					timer;
38 	private long					created;
39 	private long					when;
40 	private TimerEventPerformer	performer;
41 
42 	private boolean		absolute;
43 	private boolean		cancelled;
44 	private boolean		has_run;
45 
46 	private long			unique_id	= 1;
47 
48 	protected
TimerEvent( Timer _timer, long _unique_id, long _created, long _when, boolean _absolute, TimerEventPerformer _performer )49 	TimerEvent(
50 		Timer					_timer,
51 		long					_unique_id,
52 		long					_created,
53 		long					_when,
54 		boolean					_absolute,
55 		TimerEventPerformer		_performer )
56 	{
57 		timer		= _timer;
58 		unique_id	= _unique_id;
59 		when		= _when;
60 		absolute	= _absolute;
61 		performer	= _performer;
62 
63 		created 	= _created;
64 
65 		if ( Constants.IS_CVS_VERSION ){
66 
67 				// sanity check - seems we sometimes use 0 to denote 'now'
68 
69 			if ( when != 0 && when <= 7*24*60*60*1000L ){
70 
71 				new Exception( "You sure you want to schedule an event in the past? Time should be absolute!" ).printStackTrace();
72 
73 			}else if ( when > 3000L*365*24*60*60*1000 ){
74 
75 				new Exception( "You sure you want to schedule an event so far in the future?! (" + when + ")" ).printStackTrace();
76 			}
77 		}
78 	}
79 
80 	public void
setName( String _name )81 	setName(
82 		String		_name )
83 	{
84 		name	= _name;
85 	}
86 
87 	public String
getName()88 	getName()
89 	{
90 		return( name );
91 	}
92 
93 	public long
getCreatedTime()94 	getCreatedTime()
95 	{
96 		return( created );
97 	}
98 
99 	public long
getWhen()100 	getWhen()
101 	{
102 		return( when );
103 	}
104 
105 	protected void
setWhen( long new_when )106 	setWhen(
107 		long	new_when )
108 	{
109 		when	= new_when;
110 	}
111 
112 	protected AERunnable
getRunnable()113 	getRunnable()
114 	{
115 		return( this );
116 	}
117 
118 	protected TimerEventPerformer
getPerformer()119 	getPerformer()
120 	{
121 		return( performer );
122 	}
123 
124 	protected boolean
isAbsolute()125 	isAbsolute()
126 	{
127 		return( absolute );
128 	}
129 
130 	public void
runSupport()131 	runSupport()
132 	{
133 		performer.perform( this );
134 	}
135 
136 	public synchronized void
cancel()137 	cancel()
138 	{
139 		cancelled	= true;
140 
141 		timer.cancelEvent( this );
142 	}
143 
144 	public synchronized boolean
isCancelled()145 	isCancelled()
146 	{
147 		return( cancelled );
148 	}
149 
150 	protected void
setHasRun()151 	setHasRun()
152 	{
153 		has_run	= true;
154 	}
155 
156 	public boolean
hasRun()157 	hasRun()
158 	{
159 		return( has_run );
160 	}
161 
162 	protected long
getUniqueId()163 	getUniqueId()
164 	{
165 		return( unique_id );
166 	}
167 
168 	public int
compareTo( TimerEvent other )169 	compareTo(
170 		TimerEvent		other )
171 	{
172 		long	res =  when - other.when;
173 
174 		if ( res == 0 ){
175 
176 			res = unique_id - other.unique_id;
177 
178 			if ( res == 0 ){
179 
180 				return(  0 );
181 			}
182 		}
183 
184 		return res < 0 ? -1 : 1;
185 	}
186 
187 	public void
interruptTask()188 	interruptTask()
189 	{
190 	}
191 
192 	public String
getString()193 	getString()
194 	{
195 		if ( performer instanceof TimerEventPeriodic ){
196 
197 			TimerEventPeriodic	tep = (TimerEventPeriodic)performer;
198 
199 			return( "when=" + getWhen() + ",run=" + hasRun() + ", can=" + isCancelled() + "/" + tep.isCancelled() + ",freq=" + tep.getFrequency() + ",target=" + tep.getPerformer()+ (name==null?"":",name=" + name ));
200 
201 		}else{
202 
203 			return( "when=" + getWhen() + ",run=" + hasRun() + ", can=" + isCancelled() + ",target=" + getPerformer() + (name==null?"":",name=" + name ));
204 		}
205 	}
206 }
207