MultiTaskListener.java revision 2601:8e638f046bf0
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 */
25
26package com.sun.tools.javac.api;
27
28import java.util.Arrays;
29import java.util.Collection;
30
31import com.sun.source.util.TaskEvent;
32import com.sun.source.util.TaskListener;
33import com.sun.tools.javac.util.Context;
34import com.sun.tools.javac.util.DefinedBy;
35import com.sun.tools.javac.util.DefinedBy.Api;
36
37/**
38 * A collection of currently registered {@link TaskListener}s. Events passed to this TaskListener
39 * will be forwarded to all the registered TaskListeners.
40 *
41 * <p><b>This is NOT part of any supported API.
42 * If you write code that depends on this, you do so at your own risk.
43 * This code and its internal interfaces are subject to change or
44 * deletion without notice.</b>
45 */
46public class MultiTaskListener implements TaskListener {
47    /** The context key for the MultiTaskListener. */
48    public static final Context.Key<MultiTaskListener> taskListenerKey = new Context.Key<>();
49
50    /** Get the MultiTaskListener instance for this context. */
51    public static MultiTaskListener instance(Context context) {
52        MultiTaskListener instance = context.get(taskListenerKey);
53        if (instance == null)
54            instance = new MultiTaskListener(context);
55        return instance;
56    }
57
58    protected MultiTaskListener(Context context) {
59        context.put(taskListenerKey, this);
60        ccw = ClientCodeWrapper.instance(context);
61    }
62
63    /**
64     * The current set of registered listeners.
65     * This is a mutable reference to an immutable array.
66     */
67    TaskListener[] listeners = { };
68
69    ClientCodeWrapper ccw;
70
71    public Collection<TaskListener> getTaskListeners() {
72        return Arrays.asList(listeners);
73    }
74
75    public boolean isEmpty() {
76        return (listeners.length == 0);
77    }
78
79    public void add(TaskListener listener) {
80        for (TaskListener l: listeners) {
81            if (ccw.unwrap(l) == listener)
82                throw new IllegalStateException();
83        }
84        listeners = Arrays.copyOf(listeners, listeners.length + 1);
85        listeners[listeners.length - 1] = ccw.wrap(listener);
86    }
87
88    public void remove(TaskListener listener) {
89        for (int i = 0; i < listeners.length; i++) {
90            if (ccw.unwrap(listeners[i]) == listener) {
91                TaskListener[] newListeners = new TaskListener[listeners.length - 1];
92                System.arraycopy(listeners, 0, newListeners, 0, i);
93                System.arraycopy(listeners, i + 1, newListeners, i, newListeners.length - i);
94                listeners = newListeners;
95                break;
96            }
97        }
98    }
99
100    @Override @DefinedBy(Api.COMPILER_TREE)
101    public void started(TaskEvent e) {
102        // guard against listeners being updated by a listener
103        TaskListener[] ll = this.listeners;
104        for (TaskListener l: ll)
105            l.started(e);
106    }
107
108    @Override @DefinedBy(Api.COMPILER_TREE)
109    public void finished(TaskEvent e) {
110        // guard against listeners being updated by a listener
111        TaskListener[] ll = this.listeners;
112        for (TaskListener l: ll)
113            l.finished(e);
114    }
115
116    @Override
117    public String toString() {
118        return Arrays.toString(listeners);
119    }
120}
121