APIdia Launch Partner
Top Description Fields Constructors Methods
sun.swing

public abstract Class AccumulativeRunnable<T>

extends Object
implements Runnable
Class Inheritance
All Implemented Interfaces
java.lang.Runnable
Known Direct Subclasses
javax.swing.SwingWorker.DoSubmitAccumulativeRunnable
Type Parameters
<T>
the type this Runnable accumulates
Imports
java.util.*, java.lang.reflect.Array, javax.swing.SwingUtilities

An abstract class to be used in the cases where we need Runnable to perform some actions on an appendable set of data. The set of data might be appended after the Runnable is sent for the execution. Usually such Runnables are sent to the EDT.

Usage example:

Say we want to implement JLabel.setText(String text) which sends text string to the JLabel.setTextImpl(String text) on the EDT. In the event JLabel.setText is called rapidly many times off the EDT we will get many updates on the EDT but only the last one is important. (Every next updates overrides the previous one.) We might want to implement this setText in a way that only the last update is delivered.

Here is how one can do this using AccumulativeRunnable:

AccumulativeRunnable<String> doSetTextImpl =
 new  AccumulativeRunnable<String>() {
   @Override
   protected void run(List<String> args) {
        //set to the last string being passed
        setTextImpl(args.get(args.size() - 1));
    }
}
void setText(String text) {
    //add text and send for the execution if needed.
    doSetTextImpl.add(text);
}

Say we want to implement addDirtyRegion(Rectangle rect) which sends this region to the handleDirtyRegions(List<Rect> regions) on the EDT. addDirtyRegions better be accumulated before handling on the EDT.

Here is how it can be implemented using AccumulativeRunnable:

AccumulativeRunnable<Rectangle> doHandleDirtyRegions =
   new AccumulativeRunnable<Rectangle>() {
       @Override
       protected void run(List<Rectangle> args) {
            handleDirtyRegions(args);
        }
    };
 void addDirtyRegion(Rectangle rect) {
     doHandleDirtyRegions.add(rect);
 }
Author
Igor Kushnirskiy
Since
1.6

Field Summary

Modifier and TypeField and Description
private List<T>

Constructor Summary

AccessConstructor and Description
public

Method Summary

Modifier and TypeMethod and Description
public final synchronized void
add(T...
the arguments to accumulate
args
)

appends arguments and sends this Runnable for the execution if needed.

private synchronized List<T>

Returns:

accumulated arguments
flush
()

Returns accumulated arguments and flashes the arguments storage.

protected abstract void
run(List<T>
accumulated arguments to process.
args
)

Equivalent to Runnable.run method with the accumulated arguments to process.

public final void
run()

Implements java.lang.Runnable.run.

Runs this operation.

protected void
submit()

Sends this Runnable for the execution

This method is to be executed only from add method.

Inherited from java.lang.Object:
cloneequalsfinalizegetClasshashCodenotifynotifyAlltoStringwaitwaitwait

Field Detail

argumentsback to summary
private List<T> arguments

Constructor Detail

AccumulativeRunnableback to summary
public AccumulativeRunnable()

Method Detail

addback to summary
public final synchronized void add(T... args)

appends arguments and sends this Runnable for the execution if needed.

This implementation uses submit to send this Runnable for execution.

Parameters
args:T[]

the arguments to accumulate

Annotations
@SafeVarargs
@SuppressWarnings:varargs
flushback to summary
private synchronized List<T> flush()

Returns accumulated arguments and flashes the arguments storage.

Returns:List<T>

accumulated arguments

runback to summary
protected abstract void run(List<T> args)

Equivalent to Runnable.run method with the accumulated arguments to process.

Parameters
args:List<T>

accumulated arguments to process.

runback to summary
public final void run()

Implements java.lang.Runnable.run.

Doc from java.lang.Runnable.run.

Runs this operation.

This implementation calls run(List<T> args) method with the list of accumulated arguments.

submitback to summary
protected void submit()

Sends this Runnable for the execution

This method is to be executed only from add method.

This implementation uses SwingWorker.invokeLater.