KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > avalon > cornerstone > services > scheduler > PeriodicTimeTrigger


1 /*
2  * Copyright 1999-2004 The Apache Software Foundation
3  * Licensed under the Apache License, Version 2.0 (the "License");
4  * you may not use this file except in compliance with the License.
5  * You may obtain a copy of the License at
6  *
7  * http://www.apache.org/licenses/LICENSE-2.0
8  *
9  * Unless required by applicable law or agreed to in writing, software
10  * distributed under the License is distributed on an "AS IS" BASIS,
11  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
12  * implied.
13  *
14  * See the License for the specific language governing permissions and
15  * limitations under the License.
16  */

17
18 package org.apache.avalon.cornerstone.services.scheduler;
19
20 /**
21  * Goes off every <tt>period</tt> milliseconds after waiting for
22  * <tt>offset</tt> milliseconds from the moment the trigger was
23  * <tt>reset</tt>.
24  *
25  * @author <a HREF="mailto:dev@avalon.apache.org">Avalon Development Team</a>
26  */

27 public class PeriodicTimeTrigger
28     implements TimeTrigger
29 {
30     protected final long m_offset;
31     protected final long m_period;
32     private long m_triggerTime;
33
34     /**
35      * Creates a periodic trigger. It goes off the first time after
36      * <tt>offset</tt> milliseconds from the time it was
37      * <tt>reset</tt> and then every <tt>period</tt>
38      * milliseconds. The trigger is <tt>reset</tt> as
39      * part of its construction.
40      *
41      * @param offset initial delay in milliseconds, -1 means fire immediately
42      * @param period after initial delay in milliseconds, -1 means fire only once after initial delay
43      */

44     public PeriodicTimeTrigger( final int offset, final int period )
45     {
46         m_offset = offset;
47         m_period = period;
48
49         reset();
50     }
51
52     /**
53      * Returns the next time after the given <tt>moment</tt> when
54      * this trigger goes off.
55      *
56      * @param moment base point in milliseconds
57      * @return the time in milliseconds when this trigger goes off
58      */

59     public long getTimeAfter( final long moment )
60     {
61         if( moment <= m_triggerTime )
62         {
63             return m_triggerTime;
64         }
65         else
66         {
67             if( -1 == m_period )
68             {
69                 return -1;
70             }
71
72             final long over = moment - m_triggerTime;
73             final long remainder = over % m_period;
74
75             return moment + ( m_period - remainder );
76         }
77     }
78
79     public long getOffset()
80     {
81         return m_offset;
82     }
83
84     public long getPeriod()
85     {
86         return m_period;
87     }
88
89     /**
90      * Reset the original TimeTrigger.
91      * This will recalculate the activation time for this trigger.
92      */

93     public void reset()
94     {
95         final long current = System.currentTimeMillis();
96
97         if( -1 == m_offset )
98         {
99             m_triggerTime = current;
100         }
101         else
102         {
103             m_triggerTime = current + m_offset;
104         }
105     }
106
107     public String JavaDoc toString()
108     {
109         final StringBuffer JavaDoc sb = new StringBuffer JavaDoc();
110         sb.append( "PeriodicTimeTrigger[ " );
111
112         sb.append( "trigger time=" );
113         sb.append( m_triggerTime );
114         sb.append( " " );
115
116         sb.append( "offset=" );
117         sb.append( m_offset );
118         sb.append( " " );
119
120         if( -1 != m_period )
121         {
122             sb.append( "period=" );
123             sb.append( m_period );
124             sb.append( " " );
125         }
126
127         sb.append( "]" );
128
129         return sb.toString();
130     }
131 }
132
133
134
135
Popular Tags