litesoft
@ 804
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 |
// This Source Code is in the Public Domain per: http://litesoft.org/License.txt package org.litesoft.core.delayed.nonpublic; import org.litesoft.core.delayed.*; import org.litesoft.core.typeutils.*; import org.litesoft.core.util.*; public abstract class AbstractDelayedTimedRunnableRunner implements DelayedTimedRunnableRunner { protected TimedRunnable mTimedRunnable = null; private UnexpectedExceptionHandler mUnexpectedExceptionHandler; public AbstractDelayedTimedRunnableRunner( UnexpectedExceptionHandler pUnexpectedExceptionHandler ) { Objects.assertNotNull( "UnexpectedExceptionHandler", mUnexpectedExceptionHandler = pUnexpectedExceptionHandler ); } public DelayedTimedRunnableRunner initialize( TimedRunnable pTimedRunnable, UnexpectedExceptionHandler pUnexpectedExceptionHandler ) { Objects.assertNotNull( "TimedRunnable", mTimedRunnable = pTimedRunnable ); if ( pUnexpectedExceptionHandler != null ) { mUnexpectedExceptionHandler = pUnexpectedExceptionHandler; } return this; } public DelayedTimedRunnableRunner scheduleFor( long pWhenTimeInMilliSecs ) { cancel(); if ( LLscheduleFor( pWhenTimeInMilliSecs ) ) { LLrun(); } return this; } private boolean LLscheduleFor( long pWhenTimeInMilliSecs ) { long zNowInMilliSecs = UtilsCommon.now().getTime(); if ( pWhenTimeInMilliSecs <= zNowInMilliSecs ) { return true; } LLscheduleFor( pWhenTimeInMilliSecs, zNowInMilliSecs ); return false; } public void cancel() { validateInitialized(); LLcancel(); } protected void LLrun() { TimedRunnable runnable = mTimedRunnable; while ( runnable != null ) { TimedRunnable.Again zAgain; try { zAgain = runnable.runOnce(); } catch ( RuntimeException e ) { try { mUnexpectedExceptionHandler.onUnexpectedException( "DelayedTimedRunnableRunner", e ); } catch ( RuntimeException e1 ) { e1.printStackTrace(); // Whatever } return; } if ( (zAgain == null) || !LLscheduleFor( zAgain.getOnOrAfter() ) ) { runnable = null; } } } abstract protected void LLscheduleFor( long pWhenTimeInMilliSecs, long pNowInMilliSecs ); abstract protected void LLcancel(); private void validateInitialized() { if ( mTimedRunnable == null ) { throw new IllegalStateException( "initialize not called!" ); } } } |