Subversion Repository Public Repository

litesoft

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
// This Source Code is in the Public Domain per: http://unlicense.org
package org.litesoft.core.delayed;

import org.litesoft.commonfoundation.base.*;
import org.litesoft.commonfoundation.typeutils.*;
import org.litesoft.core.delayed.nonpublic.*;
import org.litesoft.core.util.*;

public class TimedRunner
{
    private final MillisecTimeSource mMillisecTimeSource;
    private final MyTimedRunnable mMyTimedRunnable;

    public TimedRunner( DelayedTimedRunnableRunner pRunner, UnexpectedExceptionHandler pUnexpectedExceptionHandler )
    {
        mMillisecTimeSource = pRunner.getMillisecTimeSource();
        mMyTimedRunnable = new MyTimedRunnable( pRunner, pUnexpectedExceptionHandler ).initialize();
    }

    /**
     * @return null means Not Scheduled, TRUE means canceled, FALSE means Already Running
     *
     * @throws IllegalArgumentException if <code>pTimedRunnable</code> is <code>null</code>
     */
    public Boolean cancel( TimedRunnable pTimedRunnable )
    {
        if ( pTimedRunnable == null )
        {
            throw new IllegalArgumentException();
        }
        return mMyTimedRunnable.cancel( pTimedRunnable );
    }

    /**
     * Schedules <code>pTimedRunnable</code> to run after the specified delay.
     *
     * @param pDelayInMilliSecs if <= 0 then run NOW!, otherwise put on the Queue to run no sooner then indicated
     *
     * @return When to run time in MilliSecs or null if pTimedRunnable was null
     */
    public Long runIn( TimedRunnable pTimedRunnable, long pDelayInMilliSecs )
    {
        return runOnOrAfter( pTimedRunnable, mMillisecTimeSource.now() + pDelayInMilliSecs );
    }

    /**
     * Schedules <code>pTimedRunnable</code> to run at specified time (or later).
     *
     * @param pWhenTimeInMilliSecs if <= Now then run NOW!, otherwise put on the Queue to run no sooner then indicated
     *
     * @return When to run time in MilliSecs or null if pTimedRunnable was null
     */
    public Long runOnOrAfter( TimedRunnable pTimedRunnable, long pWhenTimeInMilliSecs )
    {
        if ( pTimedRunnable != null )
        {
            mMyTimedRunnable.runOnOrAfter( pTimedRunnable, pWhenTimeInMilliSecs );
            return pWhenTimeInMilliSecs;
        }
        return null;
    }

    private static class MyTimedRunnable implements TimedRunnable
    {
        private final DelayedTimedRunnableRunner mRunner;
        private final UnexpectedExceptionHandler mUnexpectedExceptionHandler;
        private DoubleLinkedListTimedRunnableNode mHead = null;
        private TimedRunnable mRunning = null;

        public MyTimedRunnable( DelayedTimedRunnableRunner pRunner, UnexpectedExceptionHandler pUnexpectedExceptionHandler )
        {
            Objects.assertNotNull( "Runner", mRunner = pRunner );
            Objects.assertNotNull( "UnexpectedExceptionHandler", mUnexpectedExceptionHandler = pUnexpectedExceptionHandler );
        }

        public MyTimedRunnable initialize()
        {
            mRunner.initialize( this, mUnexpectedExceptionHandler );
            return this;
        }

        @Override
        public Again runOnce()
        {
            while ( true )
            {
                synchronized ( this )
                {
                    if ( mHead == null )
                    {
                        return null;
                    }
                    long zRunWhen = mHead.getRunWhen();
                    if ( mRunner.getMillisecTimeSource().now() < zRunWhen )
                    {
                        return new RunAgainOnOrAfter( zRunWhen );
                    }
                    mRunning = mHead.getTimedRunnable();
                    mHead = mHead.remove();
                }
                Again zAgain = null;
                try
                {
                    zAgain = mRunning.runOnce();
                }
                catch ( RuntimeException e )
                {
                    mUnexpectedExceptionHandler.onUnexpectedException( "TimedRunnableManager", e );
                }
                synchronized ( this )
                {
                    if ( zAgain != null )
                    {
                        mHead = DoubleLinkedListTimedRunnableNode.addTo( mHead, zAgain.getOnOrAfter( mRunner.getMillisecTimeSource() ), mRunning );
                    }
                    mRunning = null;
                }
            }
        }

        public synchronized Boolean cancel( TimedRunnable pTimedRunnable )
        {
            if ( pTimedRunnable == mRunning )
            {
                return Boolean.FALSE;
            }
            DoubleLinkedListTimedRunnableNode found = DoubleLinkedListTimedRunnableNode.locate( mHead, pTimedRunnable );
            if ( found != null )
            {
                if ( found != mHead )
                {
                    found.remove();
                }
                else if ( null == (mHead = mHead.remove()) )
                {
                    mRunner.cancel();
                }
                else
                {
                    LLrescheduleFor( mHead.getRunWhen() );
                }
                return Boolean.TRUE;
            }
            return null;
        }

        public synchronized void runOnOrAfter( TimedRunnable pTimedRunnable, long pWhenTimeInMilliSecs )
        {
            DoubleLinkedListTimedRunnableNode savedHead = mHead;
            mHead = DoubleLinkedListTimedRunnableNode.addTo( mHead, pWhenTimeInMilliSecs, pTimedRunnable );
            if ( savedHead != mHead )
            {
                LLrescheduleFor( pWhenTimeInMilliSecs );
            }
        }

        private void LLrescheduleFor( long pRunWhen ) // sync'd
        {
            mRunner.scheduleFor( pRunWhen );
        }
    }
}

Commits for litesoft/trunk/Java/core/Anywhere/src/org/litesoft/core/delayed/TimedRunner.java

Diff revisions: vs.
Revision Author Commited Message
947 Diff Diff GeorgeS picture GeorgeS Fri 06 Jun, 2014 23:36:56 +0000

Correct Spelling of package!

942 Diff Diff GeorgeS picture GeorgeS Mon 02 Jun, 2014 23:41:46 +0000

Extracting commonfoundation

939 Diff Diff GeorgeS picture GeorgeS Mon 02 Jun, 2014 21:30:31 +0000

Extracting commonfoundation

858 Diff Diff GeorgeS picture GeorgeS Sun 04 Nov, 2012 18:40:40 +0000
851 Diff Diff GeorgeS picture GeorgeS Mon 08 Oct, 2012 00:05:32 +0000

Breaking the code as Temporal changes are implemented...

819 Diff Diff GeorgeS picture GeorgeS Sat 18 Aug, 2012 18:09:40 +0000
804 Diff Diff GeorgeS picture GeorgeS Wed 15 Aug, 2012 12:48:51 +0000
151 Diff Diff GeorgeS picture GeorgeS Thu 17 Mar, 2011 04:16:22 +0000
49 Diff Diff GeorgeS picture GeorgeS Mon 12 Apr, 2010 02:59:10 +0000

License Text

2 GeorgeS picture GeorgeS Sun 07 Feb, 2010 12:50:58 +0000