AccumulativeRunnable.java revision 0
1008N/A/*
1008N/A * Copyright 2005-2006 Sun Microsystems, Inc. All Rights Reserved.
1008N/A * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
1008N/A *
1008N/A * This code is free software; you can redistribute it and/or modify it
1008N/A * under the terms of the GNU General Public License version 2 only, as
1008N/A * published by the Free Software Foundation. Sun designates this
1008N/A * particular file as subject to the "Classpath" exception as provided
1008N/A * by Sun in the LICENSE file that accompanied this code.
1008N/A *
1008N/A * This code is distributed in the hope that it will be useful, but WITHOUT
1008N/A * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
1008N/A * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
1008N/A * version 2 for more details (a copy is included in the LICENSE file that
1008N/A * accompanied this code).
1008N/A *
1008N/A * You should have received a copy of the GNU General Public License version
1008N/A * 2 along with this work; if not, write to the Free Software Foundation,
1008N/A * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
1008N/A *
1008N/A * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara,
1008N/A * CA 95054 USA or visit www.sun.com if you need additional information or
1008N/A * have any questions.
1008N/A */
1008N/Apackage sun.swing;
1008N/A
1008N/Aimport java.util.*;
1008N/Aimport java.lang.reflect.Array;
1008N/Aimport javax.swing.SwingUtilities;
1008N/A
1008N/A/**
1008N/A * An abstract class to be used in the cases where we need {@code Runnable}
1008N/A * to perform some actions on an appendable set of data.
1008N/A * The set of data might be appended after the {@code Runnable} is
1008N/A * sent for the execution. Usually such {@code Runnables} are sent to
1008N/A * the EDT.
1008N/A *
1008N/A * <p>
1008N/A * Usage example:
1008N/A *
1008N/A * <p>
1008N/A * Say we want to implement JLabel.setText(String text) which sends
1008N/A * {@code text} string to the JLabel.setTextImpl(String text) on the EDT.
1008N/A * In the event JLabel.setText is called rapidly many times off the EDT
1008N/A * we will get many updates on the EDT but only the last one is important.
1008N/A * (Every next updates overrides the previous one.)
1008N/A * We might want to implement this {@code setText} in a way that only
1008N/A * the last update is delivered.
1008N/A * <p>
1008N/A * Here is how one can do this using {@code AccumulativeRunnable}:
1008N/A * <pre>
1008N/A * AccumulativeRunnable<String> doSetTextImpl =
1008N/A * new AccumulativeRunnable<String>() {
1008N/A * @Override
1008N/A * protected void run(List&lt;String&gt; args) {
1008N/A * //set to the last string being passed
1008N/A * setTextImpl(args.get(args.size() - 1));
1008N/A * }
1008N/A * }
1008N/A * void setText(String text) {
1008N/A * //add text and send for the execution if needed.
1008N/A * doSetTextImpl.add(text);
1008N/A * }
1008N/A * </pre>
1008N/A *
1008N/A * <p>
1008N/A * Say we want want to implement addDirtyRegion(Rectangle rect)
1008N/A * which sends this region to the
1008N/A * handleDirtyRegions(List<Rect> regiouns) on the EDT.
1008N/A * addDirtyRegions better be accumulated before handling on the EDT.
1008N/A *
1008N/A * <p>
1008N/A * Here is how it can be implemented using AccumulativeRunnable:
1008N/A * <pre>
1008N/A * AccumulativeRunnable<Rectangle> doHandleDirtyRegions =
1008N/A * new AccumulativeRunnable<Rectangle>() {
1008N/A * @Override
1008N/A * protected void run(List&lt;Rectangle&gt; args) {
1008N/A * handleDirtyRegions(args);
1008N/A * }
1008N/A * };
1008N/A * void addDirtyRegion(Rectangle rect) {
1008N/A * doHandleDirtyRegions.add(rect);
1008N/A * }
1008N/A * </pre>
1008N/A *
1008N/A * @author Igor Kushnirskiy
1008N/A *
1008N/A * @param <T> the type this {@code Runnable} accumulates
1008N/A *
1008N/A * @since 1.6
1008N/A */
1008N/Apublic abstract class AccumulativeRunnable<T> implements Runnable {
1008N/A private List<T> arguments = null;
1008N/A
1008N/A /**
1008N/A * Equivalent to {@code Runnable.run} method with the
1008N/A * accumulated arguments to process.
1008N/A *
1389N/A * @param args accumulated argumets to process.
1389N/A */
1389N/A protected abstract void run(List<T> args);
1389N/A
1389N/A /**
1389N/A * {@inheritDoc}
1389N/A *
1389N/A * <p>
1389N/A * This implementation calls {@code run(List<T> args)} mehtod
1389N/A * with the list of accumulated arguments.
1389N/A */
1389N/A public final void run() {
1389N/A run(flush());
1389N/A }
1389N/A
1389N/A /**
1389N/A * appends arguments and sends this {@cod Runnable} for the
1389N/A * execution if needed.
1389N/A * <p>
1389N/A * This implementation uses {@see #submit} to send this
1008N/A * {@code Runnable} for execution.
1389N/A * @param args the arguments to accumulate
1389N/A */
1389N/A public final synchronized void add(T... args) {
1389N/A boolean isSubmitted = true;
1008N/A if (arguments == null) {
1008N/A isSubmitted = false;
1389N/A arguments = new ArrayList<T>();
1389N/A }
1389N/A Collections.addAll(arguments, args);
1389N/A if (!isSubmitted) {
1389N/A submit();
1389N/A }
1389N/A }
1389N/A
1389N/A /**
1389N/A * Sends this {@code Runnable} for the execution
1389N/A *
1389N/A * <p>
1389N/A * This method is to be executed only from {@code add} method.
1008N/A *
1008N/A * <p>
1389N/A * This implementation uses {@code SwingWorker.invokeLater}.
1389N/A */
1008N/A protected void submit() {
1008N/A SwingUtilities.invokeLater(this);
1008N/A }
1389N/A
1389N/A /**
1389N/A * Returns accumulated arguments and flashes the arguments storage.
1389N/A *
1389N/A * @return accumulated arguments
1389N/A */
1389N/A private final synchronized List<T> flush() {
1389N/A List<T> list = arguments;
1389N/A arguments = null;
1389N/A return list;
1389N/A }
1389N/A}
1389N/A