Hi,

find attached a few patches which allow the use of longs in the PeriodicTimeTrigger.
The problem with using int's is that ordering is lost when using very long schedules, as only the precision of int's is garanteed.
Note that everywhere in the code longs are used - with the notable exception of the TimeScheduledEntry comparator, which is where the
crucial change is.
I also attach unit test for PeriodicTimeTrigger.


Attachments:
patch1 - PeriodicTimeTrigger
patch2 - TimeScheduledEntry
patch3 - PeriodicTimeTriggerTestCase


Cheers, m.







Index: PeriodicTimeTrigger.java
===================================================================
RCS file: 
/home/cvspublic/avalon-cornerstone/src/java/org/apache/avalon/cornerstone/services/scheduler/PeriodicTimeTrigger.java,v
retrieving revision 1.11
diff -u -r1.11 PeriodicTimeTrigger.java
--- PeriodicTimeTrigger.java    4 Apr 2003 10:49:25 -0000       1.11
+++ PeriodicTimeTrigger.java    7 Jun 2003 17:42:37 -0000
@@ -84,6 +84,24 @@
     }
 
     /**
+     * Creates a periodic trigger. It goes off the first time after
+     * <tt>offset</tt> milliseconds from the time it was
+     * <tt>reset</tt> and then every <tt>period</tt>
+     * milliseconds. The trigger is <tt>reset</tt> as
+     * part of its construction.
+     *
+     * @param offset initial delay in milliseconds, -1 means fire immediately
+     * @param period after initial delay in milliseconds, -1 means fire only once 
after initial delay
+     */
+    public PeriodicTimeTrigger( final long offset, final long period )
+    {
+        m_offset = offset;
+        m_period = period;
+
+        reset();
+    }
+    
+    /**
      * Returns the next time after the given <tt>moment</tt> when
      * this trigger goes off.
      *
Index: TimeScheduledEntry.java
===================================================================
RCS file: 
/home/cvspublic/avalon-cornerstone/src/java/org/apache/avalon/cornerstone/blocks/scheduler/TimeScheduledEntry.java,v
retrieving revision 1.13
diff -u -r1.13 TimeScheduledEntry.java
--- TimeScheduledEntry.java     8 Apr 2003 11:21:14 -0000       1.13
+++ TimeScheduledEntry.java     7 Jun 2003 17:43:42 -0000
@@ -145,12 +145,21 @@
      * Triggers are compared based on next time to run
      *
      * @param object the other trigger
-     * @return -'ve value if other trigger occurs before this trigger
+     * @return +1 value if other trigger occurs before this trigger
+     *          -1 value if other trigger occurs after this trigger
      */
     public int compareTo( final Object object )
     {
         final TimeScheduledEntry other = (TimeScheduledEntry)object;
-        return (int)-( other.m_time - m_time );
+        if ( other.m_time < m_time)
+        {
+            return +1;
+        } else if ( other.m_time > m_time)
+        {
+            return -1;
+        } else {
+            return 0;
+        }
     }
 
     public String toString()
Index: PeriodicTimeTriggerTestCase.java
===================================================================
RCS file: PeriodicTimeTriggerTestCase.java
diff -N PeriodicTimeTriggerTestCase.java
--- /dev/null   1 Jan 1970 00:00:00 -0000
+++ PeriodicTimeTriggerTestCase.java    7 Jun 2003 17:44:03 -0000
@@ -0,0 +1,132 @@
+/*
+
+ ============================================================================
+                   The Apache Software License, Version 1.1
+ ============================================================================
+
+ Copyright (C) 1999-2003 The Apache Software Foundation. All rights reserved.
+
+ Redistribution and use in source and binary forms, with or without modifica-
+ tion, are permitted provided that the following conditions are met:
+
+ 1. Redistributions of  source code must  retain the above copyright  notice,
+    this list of conditions and the following disclaimer.
+
+ 2. Redistributions in binary form must reproduce the above copyright notice,
+    this list of conditions and the following disclaimer in the documentation
+    and/or other materials provided with the distribution.
+
+ 3. The end-user documentation included with the redistribution, if any, must
+    include  the following  acknowledgment:  "This product includes  software
+    developed  by the  Apache Software Foundation  (http://www.apache.org/)."
+    Alternately, this  acknowledgment may  appear in the software itself,  if
+    and wherever such third-party acknowledgments normally appear.
+
+ 4. The names "Jakarta", "Apache Avalon", "Avalon Cornerstone", "Avalon
+    Framework" and "Apache Software Foundation"  must not be used to endorse
+    or promote products derived  from this  software without  prior written
+    permission. For written permission, please contact [EMAIL PROTECTED]
+
+ 5. Products  derived from this software may not  be called "Apache", nor may
+    "Apache" appear  in their name,  without prior written permission  of the
+    Apache Software Foundation.
+
+ THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED WARRANTIES,
+ INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND
+ FITNESS  FOR A PARTICULAR  PURPOSE ARE  DISCLAIMED.  IN NO  EVENT SHALL  THE
+ APACHE SOFTWARE  FOUNDATION  OR ITS CONTRIBUTORS  BE LIABLE FOR  ANY DIRECT,
+ INDIRECT, INCIDENTAL, SPECIAL,  EXEMPLARY, OR CONSEQUENTIAL  DAMAGES (INCLU-
+ DING, BUT NOT LIMITED TO, PROCUREMENT  OF SUBSTITUTE GOODS OR SERVICES; LOSS
+ OF USE, DATA, OR  PROFITS; OR BUSINESS  INTERRUPTION)  HOWEVER CAUSED AND ON
+ ANY  THEORY OF LIABILITY,  WHETHER  IN CONTRACT,  STRICT LIABILITY,  OR TORT
+ (INCLUDING  NEGLIGENCE OR  OTHERWISE) ARISING IN  ANY WAY OUT OF THE  USE OF
+ THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
+
+ This software  consists of voluntary contributions made  by many individuals
+ on  behalf of the Apache Software  Foundation. For more  information on the
+ Apache Software Foundation, please see <http://www.apache.org/>.
+
+*/
+package org.apache.avalon.cornerstone.services.scheduler.test;
+
+import java.util.Calendar;
+
+import junit.framework.TestCase;
+
+import org.apache.avalon.cornerstone.services.scheduler.PeriodicTimeTrigger;
+
+/**
+ * TestCase for PeriodicTimeTrigger.
+ *
+ * @author <a href="mailto:mauro at aquilonia.org">Mauro Talevi</a>
+ */
+public final class PeriodicTimeTriggerTestCase extends TestCase
+{
+       protected static final long SECOND = 1000;
+
+       public PeriodicTimeTriggerTestCase(final String name)
+       {
+               super(name);
+       }
+
+       public void testImmediate()
+       {
+               final PeriodicTimeTrigger trigger = new PeriodicTimeTrigger(-1, -1);
+               final Calendar now = Calendar.getInstance();
+               long time = now.getTime().getTime();
+               long next = trigger.getTimeAfter(time);
+        assertEquals( "Immediate", next, -1);
+
+       }
+
+       public void testOffset()
+       {
+               final PeriodicTimeTrigger trigger = new PeriodicTimeTrigger(SECOND, 
-1);
+        final Calendar now = Calendar.getInstance();
+        long time = now.getTime().getTime();
+        long next = trigger.getTimeAfter( time );
+        final long delta = next - time;
+        assertEquals( "Offset", delta, SECOND);
+
+       }
+
+       public void testPeriod()
+       {
+               final PeriodicTimeTrigger trigger = new PeriodicTimeTrigger(-1, 
SECOND);
+        final Calendar now = Calendar.getInstance();
+        long time = now.getTime().getTime();
+        long next = trigger.getTimeAfter( time );
+
+        for( int i = 0; i < 5; i++ )
+        {
+            final long delta = next - time;
+            assertEquals( "Period", delta, 0 );
+            time = next;
+            next = trigger.getTimeAfter( time );
+        }
+       }
+
+    public void testOffsetAndPeriod()
+    {
+        final PeriodicTimeTrigger trigger = new PeriodicTimeTrigger(SECOND, SECOND);
+        final Calendar now = Calendar.getInstance();
+        long time = now.getTime().getTime();
+        long next = trigger.getTimeAfter( time );
+
+        for( int i = 0; i < 5; i++ )
+        {
+            final long delta = next - time;
+            if ( i == 0 ) 
+            {
+                assertEquals( "OffsetAndPeriod", delta, SECOND );
+            } else 
+            {
+                assertEquals( "OffsetAndPeriod", delta, 0 );
+                 
+            }
+            time = next;
+            next = trigger.getTimeAfter( time );
+        }
+    }
+
+}
\ No newline at end of file

---------------------------------------------------------------------
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]

Reply via email to