jdk/src/java.desktop/share/classes/sun/swing/AccumulativeRunnable.java
author martin
Thu, 30 Oct 2014 07:31:41 -0700
changeset 28059 e576535359cc
parent 25859 3317bb8137f4
child 31164 a61c96d50ddd
permissions -rw-r--r--
8067377: My hobby: caning, then then canning, the the can-can Summary: Fix ALL the stutters! Reviewed-by: rriggs, mchung, lancea
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     1
/*
19199
4dfcc94aa1f2 8022190: Fix varargs lint warnings in the JDK
darcy
parents: 9035
diff changeset
     2
 * Copyright (c) 2005, 2013, Oracle and/or its affiliates. All rights reserved.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     3
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
90ce3da70b43 Initial load
duke
parents:
diff changeset
     4
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
     5
 * This code is free software; you can redistribute it and/or modify it
90ce3da70b43 Initial load
duke
parents:
diff changeset
     6
 * under the terms of the GNU General Public License version 2 only, as
5506
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 2
diff changeset
     7
 * published by the Free Software Foundation.  Oracle designates this
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     8
 * particular file as subject to the "Classpath" exception as provided
5506
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 2
diff changeset
     9
 * by Oracle in the LICENSE file that accompanied this code.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    10
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    11
 * This code is distributed in the hope that it will be useful, but WITHOUT
90ce3da70b43 Initial load
duke
parents:
diff changeset
    12
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
90ce3da70b43 Initial load
duke
parents:
diff changeset
    13
 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
90ce3da70b43 Initial load
duke
parents:
diff changeset
    14
 * version 2 for more details (a copy is included in the LICENSE file that
90ce3da70b43 Initial load
duke
parents:
diff changeset
    15
 * accompanied this code).
90ce3da70b43 Initial load
duke
parents:
diff changeset
    16
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    17
 * You should have received a copy of the GNU General Public License version
90ce3da70b43 Initial load
duke
parents:
diff changeset
    18
 * 2 along with this work; if not, write to the Free Software Foundation,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    19
 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    20
 *
5506
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 2
diff changeset
    21
 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 2
diff changeset
    22
 * or visit www.oracle.com if you need additional information or have any
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 2
diff changeset
    23
 * questions.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    24
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    25
package sun.swing;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    26
90ce3da70b43 Initial load
duke
parents:
diff changeset
    27
import java.util.*;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    28
import java.lang.reflect.Array;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    29
import javax.swing.SwingUtilities;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    30
90ce3da70b43 Initial load
duke
parents:
diff changeset
    31
/**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    32
 * An abstract class to be used in the cases where we need {@code Runnable}
90ce3da70b43 Initial load
duke
parents:
diff changeset
    33
 * to perform  some actions on an appendable set of data.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    34
 * The set of data might be appended after the {@code Runnable} is
90ce3da70b43 Initial load
duke
parents:
diff changeset
    35
 * sent for the execution. Usually such {@code Runnables} are sent to
90ce3da70b43 Initial load
duke
parents:
diff changeset
    36
 * the EDT.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    37
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    38
 * <p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    39
 * Usage example:
90ce3da70b43 Initial load
duke
parents:
diff changeset
    40
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    41
 * <p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    42
 * Say we want to implement JLabel.setText(String text) which sends
90ce3da70b43 Initial load
duke
parents:
diff changeset
    43
 * {@code text} string to the JLabel.setTextImpl(String text) on the EDT.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    44
 * In the event JLabel.setText is called rapidly many times off the EDT
90ce3da70b43 Initial load
duke
parents:
diff changeset
    45
 * we will get many updates on the EDT but only the last one is important.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    46
 * (Every next updates overrides the previous one.)
90ce3da70b43 Initial load
duke
parents:
diff changeset
    47
 * We might want to implement this {@code setText} in a way that only
90ce3da70b43 Initial load
duke
parents:
diff changeset
    48
 * the last update is delivered.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    49
 * <p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    50
 * Here is how one can do this using {@code AccumulativeRunnable}:
90ce3da70b43 Initial load
duke
parents:
diff changeset
    51
 * <pre>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    52
 * AccumulativeRunnable<String> doSetTextImpl =
90ce3da70b43 Initial load
duke
parents:
diff changeset
    53
 * new  AccumulativeRunnable<String>() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    54
 *     @Override
90ce3da70b43 Initial load
duke
parents:
diff changeset
    55
 *     protected void run(List&lt;String&gt; args) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    56
 *         //set to the last string being passed
90ce3da70b43 Initial load
duke
parents:
diff changeset
    57
 *         setTextImpl(args.get(args.size() - 1));
90ce3da70b43 Initial load
duke
parents:
diff changeset
    58
 *     }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    59
 * }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    60
 * void setText(String text) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    61
 *     //add text and send for the execution if needed.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    62
 *     doSetTextImpl.add(text);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    63
 * }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    64
 * </pre>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    65
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    66
 * <p>
28059
e576535359cc 8067377: My hobby: caning, then then canning, the the can-can
martin
parents: 25859
diff changeset
    67
 * Say we want to implement addDirtyRegion(Rectangle rect)
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    68
 * which sends this region to the
90ce3da70b43 Initial load
duke
parents:
diff changeset
    69
 * handleDirtyRegions(List<Rect> regiouns) on the EDT.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    70
 * addDirtyRegions better be accumulated before handling on the EDT.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    71
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    72
 * <p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    73
 * Here is how it can be implemented using AccumulativeRunnable:
90ce3da70b43 Initial load
duke
parents:
diff changeset
    74
 * <pre>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    75
 * AccumulativeRunnable<Rectangle> doHandleDirtyRegions =
90ce3da70b43 Initial load
duke
parents:
diff changeset
    76
 *     new AccumulativeRunnable<Rectangle>() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    77
 *         @Override
90ce3da70b43 Initial load
duke
parents:
diff changeset
    78
 *         protected void run(List&lt;Rectangle&gt; args) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    79
 *             handleDirtyRegions(args);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    80
 *         }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    81
 *     };
90ce3da70b43 Initial load
duke
parents:
diff changeset
    82
 *  void addDirtyRegion(Rectangle rect) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    83
 *      doHandleDirtyRegions.add(rect);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    84
 *  }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    85
 * </pre>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    86
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    87
 * @author Igor Kushnirskiy
90ce3da70b43 Initial load
duke
parents:
diff changeset
    88
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    89
 * @param <T> the type this {@code Runnable} accumulates
90ce3da70b43 Initial load
duke
parents:
diff changeset
    90
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    91
 * @since 1.6
90ce3da70b43 Initial load
duke
parents:
diff changeset
    92
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    93
public abstract class AccumulativeRunnable<T> implements Runnable {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    94
    private List<T> arguments = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    95
90ce3da70b43 Initial load
duke
parents:
diff changeset
    96
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    97
     * Equivalent to {@code Runnable.run} method with the
90ce3da70b43 Initial load
duke
parents:
diff changeset
    98
     * accumulated arguments to process.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    99
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   100
     * @param args accumulated argumets to process.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   101
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   102
    protected abstract void run(List<T> args);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   103
90ce3da70b43 Initial load
duke
parents:
diff changeset
   104
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   105
     * {@inheritDoc}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   106
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   107
     * <p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   108
     * This implementation calls {@code run(List<T> args)} mehtod
90ce3da70b43 Initial load
duke
parents:
diff changeset
   109
     * with the list of accumulated arguments.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   110
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   111
    public final void run() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   112
        run(flush());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   113
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   114
90ce3da70b43 Initial load
duke
parents:
diff changeset
   115
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   116
     * appends arguments and sends this {@cod Runnable} for the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   117
     * execution if needed.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   118
     * <p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   119
     * This implementation uses {@see #submit} to send this
90ce3da70b43 Initial load
duke
parents:
diff changeset
   120
     * {@code Runnable} for execution.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   121
     * @param args the arguments to accumulate
90ce3da70b43 Initial load
duke
parents:
diff changeset
   122
     */
8151
88b01a6d5f51 7006578: Project Coin: Retrofit JDK libraries with @SafeVarargs
darcy
parents: 5506
diff changeset
   123
    @SafeVarargs
19199
4dfcc94aa1f2 8022190: Fix varargs lint warnings in the JDK
darcy
parents: 9035
diff changeset
   124
    @SuppressWarnings("varargs") // Copying args is safe
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   125
    public final synchronized void add(T... args) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   126
        boolean isSubmitted = true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   127
        if (arguments == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   128
            isSubmitted = false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   129
            arguments = new ArrayList<T>();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   130
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   131
        Collections.addAll(arguments, args);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   132
        if (!isSubmitted) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   133
            submit();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   134
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   135
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   136
90ce3da70b43 Initial load
duke
parents:
diff changeset
   137
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   138
     * Sends this {@code Runnable} for the execution
90ce3da70b43 Initial load
duke
parents:
diff changeset
   139
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   140
     * <p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   141
     * This method is to be executed only from {@code add} method.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   142
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   143
     * <p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   144
     * This implementation uses {@code SwingWorker.invokeLater}.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   145
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   146
    protected void submit() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   147
        SwingUtilities.invokeLater(this);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   148
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   149
90ce3da70b43 Initial load
duke
parents:
diff changeset
   150
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   151
     * Returns accumulated arguments and flashes the arguments storage.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   152
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   153
     * @return accumulated arguments
90ce3da70b43 Initial load
duke
parents:
diff changeset
   154
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   155
    private final synchronized List<T> flush() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   156
        List<T> list = arguments;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   157
        arguments = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   158
        return list;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   159
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   160
}