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

changeset 1210
62e611704863
child 1339
0e5899f09dab
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/src/share/classes/com/sun/tools/javac/api/MultiTaskListener.java	Tue Feb 28 10:33:49 2012 -0800
     1.3 @@ -0,0 +1,120 @@
     1.4 +/*
     1.5 + * Copyright (c) 2011, 2012, Oracle and/or its affiliates. All rights reserved.
     1.6 + * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
     1.7 + *
     1.8 + * This code is free software; you can redistribute it and/or modify it
     1.9 + * under the terms of the GNU General Public License version 2 only, as
    1.10 + * published by the Free Software Foundation.  Oracle designates this
    1.11 + * particular file as subject to the "Classpath" exception as provided
    1.12 + * by Oracle in the LICENSE file that accompanied this code.
    1.13 + *
    1.14 + * This code is distributed in the hope that it will be useful, but WITHOUT
    1.15 + * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
    1.16 + * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
    1.17 + * version 2 for more details (a copy is included in the LICENSE file that
    1.18 + * accompanied this code).
    1.19 + *
    1.20 + * You should have received a copy of the GNU General Public License version
    1.21 + * 2 along with this work; if not, write to the Free Software Foundation,
    1.22 + * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
    1.23 + *
    1.24 + * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
    1.25 + * or visit www.oracle.com if you need additional information or have any
    1.26 + * questions.
    1.27 + */
    1.28 +
    1.29 +package com.sun.tools.javac.api;
    1.30 +
    1.31 +import java.util.Arrays;
    1.32 +import java.util.Collection;
    1.33 +
    1.34 +import com.sun.source.util.TaskEvent;
    1.35 +import com.sun.source.util.TaskListener;
    1.36 +import com.sun.tools.javac.util.Context;
    1.37 +
    1.38 +/**
    1.39 + * TODO.
    1.40 + *
    1.41 + * <p><b>This is NOT part of any supported API.
    1.42 + * If you write code that depends on this, you do so at your own risk.
    1.43 + * This code and its internal interfaces are subject to change or
    1.44 + * deletion without notice.</b>
    1.45 + */
    1.46 +public class MultiTaskListener implements TaskListener {
    1.47 +    /** The context key for the MultiTaskListener. */
    1.48 +    public static final Context.Key<MultiTaskListener> taskListenerKey =
    1.49 +        new Context.Key<MultiTaskListener>();
    1.50 +
    1.51 +    /** Get the MultiTaskListener instance for this context. */
    1.52 +    public static MultiTaskListener instance(Context context) {
    1.53 +        MultiTaskListener instance = context.get(taskListenerKey);
    1.54 +        if (instance == null)
    1.55 +            instance = new MultiTaskListener(context);
    1.56 +        return instance;
    1.57 +    }
    1.58 +
    1.59 +    protected MultiTaskListener(Context context) {
    1.60 +        context.put(taskListenerKey, this);
    1.61 +        ccw = ClientCodeWrapper.instance(context);
    1.62 +    }
    1.63 +
    1.64 +    /**
    1.65 +     * The current set of registered listeners.
    1.66 +     * This is a mutable reference to an immutable array.
    1.67 +     */
    1.68 +    TaskListener[] listeners = { };
    1.69 +
    1.70 +    ClientCodeWrapper ccw;
    1.71 +
    1.72 +    public Collection<TaskListener> getTaskListeners() {
    1.73 +        return Arrays.asList(listeners);
    1.74 +    }
    1.75 +
    1.76 +    public boolean isEmpty() {
    1.77 +        return (listeners.length == 0);
    1.78 +    }
    1.79 +
    1.80 +    public void add(TaskListener listener) {
    1.81 +        for (TaskListener l: listeners) {
    1.82 +            if (ccw.unwrap(l) == listener)
    1.83 +                throw new IllegalStateException();
    1.84 +        }
    1.85 +        TaskListener[] newListeners = new TaskListener[listeners.length + 1];
    1.86 +        System.arraycopy(listeners, 0, newListeners, 0, listeners.length);
    1.87 +        newListeners[newListeners.length - 1] = ccw.wrap(listener);
    1.88 +        listeners = newListeners;
    1.89 +    }
    1.90 +
    1.91 +    public void remove(TaskListener listener) {
    1.92 +        for (int i = 0; i < listeners.length; i++) {
    1.93 +            if (ccw.unwrap(listeners[i]) == listener) {
    1.94 +                TaskListener[] newListeners = new TaskListener[listeners.length - 1];
    1.95 +                System.arraycopy(listeners, 0, newListeners, 0, i);
    1.96 +                System.arraycopy(listeners, i + 1, newListeners, i, newListeners.length - i);
    1.97 +                listeners = newListeners;
    1.98 +                break;
    1.99 +            }
   1.100 +        }
   1.101 +    }
   1.102 +
   1.103 +    @Override
   1.104 +    public void started(TaskEvent e) {
   1.105 +        // guard against listeners being updated by a listener
   1.106 +        TaskListener[] ll = this.listeners;
   1.107 +        for (TaskListener l: ll)
   1.108 +            l.started(e);
   1.109 +    }
   1.110 +
   1.111 +    @Override
   1.112 +    public void finished(TaskEvent e) {
   1.113 +        // guard against listeners being updated by a listener
   1.114 +        TaskListener[] ll = this.listeners;
   1.115 +        for (TaskListener l: ll)
   1.116 +            l.finished(e);
   1.117 +    }
   1.118 +
   1.119 +    @Override
   1.120 +    public String toString() {
   1.121 +        return Arrays.toString(listeners);
   1.122 +    }
   1.123 +}

mercurial