|
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 |
|
26 package com.sun.tools.javac.api; |
|
27 |
|
28 import java.util.Arrays; |
|
29 import java.util.Collection; |
|
30 |
|
31 import com.sun.source.util.TaskEvent; |
|
32 import com.sun.source.util.TaskListener; |
|
33 import com.sun.tools.javac.util.Context; |
|
34 import com.sun.tools.javac.util.DefinedBy; |
|
35 import 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 */ |
|
46 public class MultiTaskListener implements TaskListener { |
|
47 /** The context key for the MultiTaskListener. */ |
|
48 public static final Context.Key<MultiTaskListener> taskListenerKey = new Context.Key<>(); |
|
49 |
|
50 /** Empty array of task listeners */ |
|
51 private static final TaskListener[] EMPTY_LISTENERS = new TaskListener[0]; |
|
52 |
|
53 /** Get the MultiTaskListener instance for this context. */ |
|
54 public static MultiTaskListener instance(Context context) { |
|
55 MultiTaskListener instance = context.get(taskListenerKey); |
|
56 if (instance == null) |
|
57 instance = new MultiTaskListener(context); |
|
58 return instance; |
|
59 } |
|
60 |
|
61 protected MultiTaskListener(Context context) { |
|
62 context.put(taskListenerKey, this); |
|
63 ccw = ClientCodeWrapper.instance(context); |
|
64 } |
|
65 |
|
66 /** |
|
67 * The current set of registered listeners. |
|
68 * This is a mutable reference to an immutable array. |
|
69 */ |
|
70 TaskListener[] listeners = EMPTY_LISTENERS; |
|
71 |
|
72 ClientCodeWrapper ccw; |
|
73 |
|
74 public Collection<TaskListener> getTaskListeners() { |
|
75 return Arrays.asList(listeners); |
|
76 } |
|
77 |
|
78 public boolean isEmpty() { |
|
79 return listeners == EMPTY_LISTENERS; |
|
80 } |
|
81 |
|
82 public void add(TaskListener listener) { |
|
83 for (TaskListener l: listeners) { |
|
84 if (ccw.unwrap(l) == listener) |
|
85 throw new IllegalStateException(); |
|
86 } |
|
87 listeners = Arrays.copyOf(listeners, listeners.length + 1); |
|
88 listeners[listeners.length - 1] = ccw.wrap(listener); |
|
89 } |
|
90 |
|
91 public void remove(TaskListener listener) { |
|
92 for (int i = 0; i < listeners.length; i++) { |
|
93 if (ccw.unwrap(listeners[i]) == listener) { |
|
94 if (listeners.length == 1) { |
|
95 listeners = EMPTY_LISTENERS; |
|
96 } else { |
|
97 TaskListener[] newListeners = new TaskListener[listeners.length - 1]; |
|
98 System.arraycopy(listeners, 0, newListeners, 0, i); |
|
99 System.arraycopy(listeners, i + 1, newListeners, i, newListeners.length - i); |
|
100 listeners = newListeners; |
|
101 } |
|
102 break; |
|
103 } |
|
104 } |
|
105 } |
|
106 |
|
107 @Override @DefinedBy(Api.COMPILER_TREE) |
|
108 public void started(TaskEvent e) { |
|
109 // guard against listeners being updated by a listener |
|
110 TaskListener[] ll = this.listeners; |
|
111 for (TaskListener l: ll) |
|
112 l.started(e); |
|
113 } |
|
114 |
|
115 @Override @DefinedBy(Api.COMPILER_TREE) |
|
116 public void finished(TaskEvent e) { |
|
117 // guard against listeners being updated by a listener |
|
118 TaskListener[] ll = this.listeners; |
|
119 for (TaskListener l: ll) |
|
120 l.finished(e); |
|
121 } |
|
122 |
|
123 @Override |
|
124 public String toString() { |
|
125 return Arrays.toString(listeners); |
|
126 } |
|
127 |
|
128 public void clear() { |
|
129 listeners = EMPTY_LISTENERS; |
|
130 } |
|
131 } |