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

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

mercurial