capture
[rrq/gorite.git] / com / intendico / gorite / addon / TimeTrigger.java
1 /*********************************************************************
2 Copyright 2012, Ralph Ronnquist.
3
4 This file is part of GORITE.
5
6 GORITE is free software: you can redistribute it and/or modify it
7 under the terms of the Lesser GNU General Public License as published
8 by the Free Software Foundation, either version 3 of the License, or
9 (at your option) any later version.
10
11 GORITE is distributed in the hope that it will be useful, but WITHOUT
12 ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
13 FITNESS FOR A PARTICULAR PURPOSE.  See the GNU Lesser General Public
14 License for more details.
15
16 You should have received a copy of the Lesser GNU General Public
17 License along with GORITE.  If not, see <http://www.gnu.org/licenses/>.
18 **********************************************************************/
19
20 package com.intendico.gorite.addon;
21
22 import com.intendico.gorite.Data;
23 import java.util.Observable;
24 import java.util.Timer;
25 import java.util.TimerTask;
26
27 /**
28  * This class defines an Observable that schedules itself for issuing
29  * a notfication to Observers at a future time.
30  */
31 public class TimeTrigger extends Observable {
32
33     /**
34      * Utility method to establish a TimeTrigger as a {@link Data}
35      * element value, and use as execution trigger after the given
36      * delay. The indicated {@link com.intendico.gorite.Data.Element
37      * Data.Element} should be unset initially, to be set by this
38      * method to a TimeTrigger for the given delay. Upon subsequent
39      * calls, the TimeTrigger is rescheduled until its deadline has
40      * passed, at which time the given {@link
41      * com.intendico.gorite.Data.Element Data.Element} is forgotten.
42      * @return true if a trigger has been set and false otherwise
43      */
44     public static boolean isPending(Data data,String element,long delay) {
45         TimeTrigger tt = (TimeTrigger) data.getValue( element );
46         if ( tt == null ) {
47             tt = new TimeTrigger( delay );
48             data.setValue( element, tt );
49             data.setTrigger( tt );
50         }
51         if ( tt.reschedule() )
52             return true;
53         data.forget( element );
54         return false;
55     }
56
57     /**
58      * A global {@link Timer} for scheduling future events on.
59      */
60     public static Timer timer = new Timer( true );
61
62     /**
63      * The deadline time for this TimeTrigger.
64      */
65     public long deadline;
66
67     /**
68      * Constructor for a given future time.
69      */
70     public TimeTrigger(long delay) {
71         deadline = System.currentTimeMillis() + delay;
72     }
73
74     /**
75      * Reset for triggering at a new future time.
76      */
77     public void reset(long delay) {
78         deadline = System.currentTimeMillis() + delay;
79     }
80
81     /**
82      * This method tests whether the trigger time is reached or not,
83      * and if not, it sets up a {@link TimerTask} for notifying
84      * observers when the trigger time is reached.
85      */
86     public boolean reschedule() {
87         long delay = deadline - System.currentTimeMillis();
88         if ( delay <= 0 )
89             return false;
90         timer.schedule( new TimerTask() {
91             public void run() {
92                 setChanged();
93                 notifyObservers( this );
94             }
95         }, delay );
96         return true;
97     }
98 }