jjg@1210: /* jjg@1210: * Copyright (c) 2011, 2012, Oracle and/or its affiliates. All rights reserved. jjg@1210: * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. jjg@1210: * jjg@1210: * This code is free software; you can redistribute it and/or modify it jjg@1210: * under the terms of the GNU General Public License version 2 only, as jjg@1210: * published by the Free Software Foundation. Oracle designates this jjg@1210: * particular file as subject to the "Classpath" exception as provided jjg@1210: * by Oracle in the LICENSE file that accompanied this code. jjg@1210: * jjg@1210: * This code is distributed in the hope that it will be useful, but WITHOUT jjg@1210: * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or jjg@1210: * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License jjg@1210: * version 2 for more details (a copy is included in the LICENSE file that jjg@1210: * accompanied this code). jjg@1210: * jjg@1210: * You should have received a copy of the GNU General Public License version jjg@1210: * 2 along with this work; if not, write to the Free Software Foundation, jjg@1210: * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. jjg@1210: * jjg@1210: * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA jjg@1210: * or visit www.oracle.com if you need additional information or have any jjg@1210: * questions. jjg@1210: */ jjg@1210: jjg@1210: package com.sun.tools.javac.api; jjg@1210: jjg@1210: import java.util.Arrays; jjg@1210: import java.util.Collection; jjg@1210: jjg@1210: import com.sun.source.util.TaskEvent; jjg@1210: import com.sun.source.util.TaskListener; jjg@1210: import com.sun.tools.javac.util.Context; jjg@1210: jjg@1210: /** jjg@1210: * TODO. jjg@1210: * jjg@1210: *

This is NOT part of any supported API. jjg@1210: * If you write code that depends on this, you do so at your own risk. jjg@1210: * This code and its internal interfaces are subject to change or jjg@1210: * deletion without notice. jjg@1210: */ jjg@1210: public class MultiTaskListener implements TaskListener { jjg@1210: /** The context key for the MultiTaskListener. */ jjg@1210: public static final Context.Key taskListenerKey = jjg@1210: new Context.Key(); jjg@1210: jjg@1210: /** Get the MultiTaskListener instance for this context. */ jjg@1210: public static MultiTaskListener instance(Context context) { jjg@1210: MultiTaskListener instance = context.get(taskListenerKey); jjg@1210: if (instance == null) jjg@1210: instance = new MultiTaskListener(context); jjg@1210: return instance; jjg@1210: } jjg@1210: jjg@1210: protected MultiTaskListener(Context context) { jjg@1210: context.put(taskListenerKey, this); jjg@1210: ccw = ClientCodeWrapper.instance(context); jjg@1210: } jjg@1210: jjg@1210: /** jjg@1210: * The current set of registered listeners. jjg@1210: * This is a mutable reference to an immutable array. jjg@1210: */ jjg@1210: TaskListener[] listeners = { }; jjg@1210: jjg@1210: ClientCodeWrapper ccw; jjg@1210: jjg@1210: public Collection getTaskListeners() { jjg@1210: return Arrays.asList(listeners); jjg@1210: } jjg@1210: jjg@1210: public boolean isEmpty() { jjg@1210: return (listeners.length == 0); jjg@1210: } jjg@1210: jjg@1210: public void add(TaskListener listener) { jjg@1210: for (TaskListener l: listeners) { jjg@1210: if (ccw.unwrap(l) == listener) jjg@1210: throw new IllegalStateException(); jjg@1210: } jjg@1339: listeners = Arrays.copyOf(listeners, listeners.length + 1); jjg@1339: listeners[listeners.length - 1] = ccw.wrap(listener); jjg@1210: } jjg@1210: jjg@1210: public void remove(TaskListener listener) { jjg@1210: for (int i = 0; i < listeners.length; i++) { jjg@1210: if (ccw.unwrap(listeners[i]) == listener) { jjg@1210: TaskListener[] newListeners = new TaskListener[listeners.length - 1]; jjg@1210: System.arraycopy(listeners, 0, newListeners, 0, i); jjg@1210: System.arraycopy(listeners, i + 1, newListeners, i, newListeners.length - i); jjg@1210: listeners = newListeners; jjg@1210: break; jjg@1210: } jjg@1210: } jjg@1210: } jjg@1210: jjg@1210: @Override jjg@1210: public void started(TaskEvent e) { jjg@1210: // guard against listeners being updated by a listener jjg@1210: TaskListener[] ll = this.listeners; jjg@1210: for (TaskListener l: ll) jjg@1210: l.started(e); jjg@1210: } jjg@1210: jjg@1210: @Override jjg@1210: public void finished(TaskEvent e) { jjg@1210: // guard against listeners being updated by a listener jjg@1210: TaskListener[] ll = this.listeners; jjg@1210: for (TaskListener l: ll) jjg@1210: l.finished(e); jjg@1210: } jjg@1210: jjg@1210: @Override jjg@1210: public String toString() { jjg@1210: return Arrays.toString(listeners); jjg@1210: } jjg@1210: }