1 /*
2 Copyright (C) 2007 Dirk Huenniger
3
4 This library is free software; you can redistribute it and/or
5 modify it under the terms of the GNU Lesser General Public
6 License as published by the Free Software Foundation; either
7 version 2.1 of the License, or (at your option) any later version.
8
9 This library is distributed in the hope that it will be useful,
10 but WITHOUT ANY WARRANTY; without even the implied warranty of
11 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12 Lesser General Public License for more details.
13
14 You should have received a copy of the GNU Lesser General Public
15 License along with this library; if not, write to the Free Software
16 Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
17 */
18 package org.indi.reactor;
19
20 import java.util.Date;
21
22 /**
23 * A simplifyed version of the TimerHandler class taking a given delay in
24 * miliseconds instead of an expiration Date and a class implementing
25 * TimerCallback interface to be called back after the given delay.
26 *
27 * @author dirk
28 *
29 */
30 public class SimpleTimer extends TimerHandler {
31 /**
32 * A class containing the callback method to be called after the given
33 * delay.
34 */
35 private final TimerCallback callback;
36
37 /**
38 * Class constructor
39 *
40 * @param callback
41 * the object containing the callback method to be called
42 * after the given delay
43 * @param timeout
44 * the delay to wait before calling the callback method in
45 * milliseconds.
46 */
47 public SimpleTimer(TimerCallback callback, long timeout) {
48 super(SimpleTimer.shift(new Date(), timeout));
49 this.callback = callback;
50 }
51
52 /**
53 * Shift a given Date object by a certain number of miliseconds
54 *
55 * @param date
56 * the date object to be shifted
57 * @param mills
58 * the number of milliseconds to shift the timout by
59 * @return the shifted Date.
60 */
61 public static Date shift(Date date, long mills) {
62 date.setTime(date.getTime() + mills);
63 return date;
64 }
65
66 /**
67 * Called by the reactor when the delay has passed. Implemented such that
68 * the onTimer method of the TimerCallback passed to the constuctor is
69 * called.
70 */
71 @Override
72 public void onTimer() {
73 this.callback.onTimer();
74 }
75 }