package textbender.o.awt; // Copyright 2005, Michael Allan. Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Textbender Software"), to deal in the Textbender Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicence, and/or sell copies of the Textbender Software, and to permit persons to whom the Textbender Software is furnished to do so, subject to the following conditions: The preceding copyright notice and this permission notice shall be included in all copies or substantial portions of the Textbender Software. THE TEXTBENDER SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE TEXTBENDER SOFTWARE OR THE USE OR OTHER DEALINGS IN THE TEXTBENDER SOFTWARE. import java.awt.event.*; import java.util.*; import javax.swing.Timer; import textbender.g.lang.*; /** A buffer for delayed and coalesced invocation of runnables. * Use as a damper against rapid-fire events etc. */ @ThreadRestricted( "AWT event dispatch" ) public final class RunBuffer implements RunDelayer { // Needs a retrofit to align it with new Executor etc. of java.util.concurrent. /** Constructs a RunBuffer. * * @param interInvocationMilliseconds delay between invocations of buffered runnables */ public RunBuffer( int interInvocationMilliseconds ) { assert java.awt.EventQueue.isDispatchThread(); timer = new Timer( interInvocationMilliseconds, runner ); } // - R u n - D e l a y e r ------------------------------------------------------------ public int delayMillisecondsMinimum() { return timer.getDelay(); } /** Ensures the run is scheduled. * If the same runnable was already scheduled but not yet executed, * then no action is taken. *

* All runs will execute on the AWT event dispatch thread. *

*/ public void schedule( Runnable runnable ) { assert java.awt.EventQueue.isDispatchThread(); runnableMap.put( runnable, runnable ); if( !timer.isRunning() ) timer.start(); } //// P r i v a t e /////////////////////////////////////////////////////////////////////// /** Runnables awaiting invocation, each mapped to itself. */ private final Map runnableMap = new LinkedHashMap(); // LinkedHashMap for its FIFO order. But a Map for its coalescing, though something like old g/._/util/Dispatch*.java (with its boolean enqueued variable) might be alot faster. OPT private final ActionListener runner = new ActionListener() { public void actionPerformed( ActionEvent e ) { assert java.awt.EventQueue.isDispatchThread(); final Runnable runnable; { Iterator runnableIterator = runnableMap.values().iterator(); runnable = (Runnable)runnableIterator.next(); runnableIterator.remove(); } if( runnableMap.size() == 0 ) timer.stop(); runnable.run(); } }; private final Timer timer; }