src/share/classes/com/sun/tools/javac/api/MultiTaskListener.java

Sun, 17 Feb 2013 16:44:55 -0500

author
dholmes
date
Sun, 17 Feb 2013 16:44:55 -0500
changeset 1571
af8417e590f4
parent 1339
0e5899f09dab
child 2525
2eb010b6cb22
permissions
-rw-r--r--

Merge

     1 /*
     2  * Copyright (c) 2011, 2012, Oracle and/or its affiliates. All rights reserved.
     3  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
     4  *
     5  * This code is free software; you can redistribute it and/or modify it
     6  * under the terms of the GNU General Public License version 2 only, as
     7  * published by the Free Software Foundation.  Oracle designates this
     8  * particular file as subject to the "Classpath" exception as provided
     9  * by Oracle in the LICENSE file that accompanied this code.
    10  *
    11  * This code is distributed in the hope that it will be useful, but WITHOUT
    12  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
    13  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
    14  * version 2 for more details (a copy is included in the LICENSE file that
    15  * accompanied this code).
    16  *
    17  * You should have received a copy of the GNU General Public License version
    18  * 2 along with this work; if not, write to the Free Software Foundation,
    19  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
    20  *
    21  * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
    22  * or visit www.oracle.com if you need additional information or have any
    23  * questions.
    24  */
    26 package com.sun.tools.javac.api;
    28 import java.util.Arrays;
    29 import java.util.Collection;
    31 import com.sun.source.util.TaskEvent;
    32 import com.sun.source.util.TaskListener;
    33 import com.sun.tools.javac.util.Context;
    35 /**
    36  * TODO.
    37  *
    38  * <p><b>This is NOT part of any supported API.
    39  * If you write code that depends on this, you do so at your own risk.
    40  * This code and its internal interfaces are subject to change or
    41  * deletion without notice.</b>
    42  */
    43 public class MultiTaskListener implements TaskListener {
    44     /** The context key for the MultiTaskListener. */
    45     public static final Context.Key<MultiTaskListener> taskListenerKey =
    46         new Context.Key<MultiTaskListener>();
    48     /** Get the MultiTaskListener instance for this context. */
    49     public static MultiTaskListener instance(Context context) {
    50         MultiTaskListener instance = context.get(taskListenerKey);
    51         if (instance == null)
    52             instance = new MultiTaskListener(context);
    53         return instance;
    54     }
    56     protected MultiTaskListener(Context context) {
    57         context.put(taskListenerKey, this);
    58         ccw = ClientCodeWrapper.instance(context);
    59     }
    61     /**
    62      * The current set of registered listeners.
    63      * This is a mutable reference to an immutable array.
    64      */
    65     TaskListener[] listeners = { };
    67     ClientCodeWrapper ccw;
    69     public Collection<TaskListener> getTaskListeners() {
    70         return Arrays.asList(listeners);
    71     }
    73     public boolean isEmpty() {
    74         return (listeners.length == 0);
    75     }
    77     public void add(TaskListener listener) {
    78         for (TaskListener l: listeners) {
    79             if (ccw.unwrap(l) == listener)
    80                 throw new IllegalStateException();
    81         }
    82         listeners = Arrays.copyOf(listeners, listeners.length + 1);
    83         listeners[listeners.length - 1] = ccw.wrap(listener);
    84     }
    86     public void remove(TaskListener listener) {
    87         for (int i = 0; i < listeners.length; i++) {
    88             if (ccw.unwrap(listeners[i]) == listener) {
    89                 TaskListener[] newListeners = new TaskListener[listeners.length - 1];
    90                 System.arraycopy(listeners, 0, newListeners, 0, i);
    91                 System.arraycopy(listeners, i + 1, newListeners, i, newListeners.length - i);
    92                 listeners = newListeners;
    93                 break;
    94             }
    95         }
    96     }
    98     @Override
    99     public void started(TaskEvent e) {
   100         // guard against listeners being updated by a listener
   101         TaskListener[] ll = this.listeners;
   102         for (TaskListener l: ll)
   103             l.started(e);
   104     }
   106     @Override
   107     public void finished(TaskEvent e) {
   108         // guard against listeners being updated by a listener
   109         TaskListener[] ll = this.listeners;
   110         for (TaskListener l: ll)
   111             l.finished(e);
   112     }
   114     @Override
   115     public String toString() {
   116         return Arrays.toString(listeners);
   117     }
   118 }

mercurial