package com.futureplatforms.kirin.dependencies;


/**
 * Equivalent of java.util.Timer/java.util.TimerTask -- represents a task that
 * can be scheduled for one-time or repeated execution.
 * 
 * 
 * HOW TO USE:
 *   Extend this class and implement your delayed task in run()
 *   Task must be non-blocking, if you have a long-running task, separate it into 
 *   bite-size chunks that won't delay things!
 * 
 * @author douglashoskins
 *
 */
public abstract class TimerTask {
    /**
     * Implementing platforms should provide an implementation of PlatformTimerTask, which should
     * provide delayed execution functionality on that platform.
     * @author douglashoskins
     *
     */
    public static abstract class PlatformTimerTask {
        private TimerTask mTimerTask;
        private final void setRealTimerTask(TimerTask task) {
            mTimerTask = task;
        }
        
        /**
         * This method should be invoked by the PlatformTimerTask implementation 
         * after the specified delay or period
         */
        public final void run() {
            mTimerTask.run();
        }
        
        public abstract void cancel();
        
        /**
         * Platform specific implementations of schedule and scheduleRepeating.  These could
         * wrap around setTimeout and setInterval on a Javascript based system for instance.
         * @param delayMillis
         */
        public abstract void schedule(int delayMillis);
    }
    
    /**
     * Implementing platforms should provide an instance of TimerDelegate, which acts as 
     * a factory for PlatformTimerTasks
     * @author douglashoskins
     *
     */
    public static interface TimerDelegate {
        public PlatformTimerTask getPlatformTimerTask();
    }
    
    private PlatformTimerTask mPtt;
    
    public TimerTask() {
        this(StaticDependencies.getInstance());
    }
    
    public TimerTask(StaticDependencies deps) {
        mPtt = deps.getTimerDelegate().getPlatformTimerTask();
        // Pass this TimerTask into the native dependency, and our run method will be invoked later
        mPtt.setRealTimerTask(this);
    }
    
    public void cancel() {
        mPtt.cancel();
    }
    
    /**
     * Override this method to specify the deferred action to be taken.
     */
    public abstract void run();
    
    public void schedule(int delayMillis) {
        mPtt.schedule(delayMillis);
    }
}

@@ -773,24 +773,283 @@
 mTimerTask;%0A
+%0A        private static int TIMER_ID = Integer.MIN_VALUE;%0A%0A        private int id;%0A%0A        public PlatformTimerTask() %7B%0A            this.id = TIMER_ID;%0A            TIMER_ID++;%0A        %7D%0A%0A        protected int getTimerId() %7B%0A            return id;%0A        %7D%0A%0A
         priv
@@ -1933,23 +1933,16 @@
  public 
-static 
 interfac
@@ -1959,39 +1959,32 @@
 egate %7B%0A        
-public 
 PlatformTimerTas
@@ -2452,36 +2452,87 @@
 cancel();%0A    %7D%0A
+%0A
     
+public int testId() %7B return mPtt.getTimerId(); %7D%0A
 %0A    /**%0A     * 

Add IDs to TimerTask for debugging/testing