|
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 |
|
35 /** |
|
36 * TODO. |
|
37 * |
|
38 * <p><b>This is NOT part of any supported API. |
|
39 * If you write code that depends on this, you do so at your own risk. |
|
40 * This code and its internal interfaces are subject to change or |
|
41 * deletion without notice.</b> |
|
42 */ |
|
43 public class MultiTaskListener implements TaskListener { |
|
44 /** The context key for the MultiTaskListener. */ |
|
45 public static final Context.Key<MultiTaskListener> taskListenerKey = |
|
46 new Context.Key<MultiTaskListener>(); |
|
47 |
|
48 /** Get the MultiTaskListener instance for this context. */ |
|
49 public static MultiTaskListener instance(Context context) { |
|
50 MultiTaskListener instance = context.get(taskListenerKey); |
|
51 if (instance == null) |
|
52 instance = new MultiTaskListener(context); |
|
53 return instance; |
|
54 } |
|
55 |
|
56 protected MultiTaskListener(Context context) { |
|
57 context.put(taskListenerKey, this); |
|
58 ccw = ClientCodeWrapper.instance(context); |
|
59 } |
|
60 |
|
61 /** |
|
62 * The current set of registered listeners. |
|
63 * This is a mutable reference to an immutable array. |
|
64 */ |
|
65 TaskListener[] listeners = { }; |
|
66 |
|
67 ClientCodeWrapper ccw; |
|
68 |
|
69 public Collection<TaskListener> getTaskListeners() { |
|
70 return Arrays.asList(listeners); |
|
71 } |
|
72 |
|
73 public boolean isEmpty() { |
|
74 return (listeners.length == 0); |
|
75 } |
|
76 |
|
77 public void add(TaskListener listener) { |
|
78 for (TaskListener l: listeners) { |
|
79 if (ccw.unwrap(l) == listener) |
|
80 throw new IllegalStateException(); |
|
81 } |
|
82 TaskListener[] newListeners = new TaskListener[listeners.length + 1]; |
|
83 System.arraycopy(listeners, 0, newListeners, 0, listeners.length); |
|
84 newListeners[newListeners.length - 1] = ccw.wrap(listener); |
|
85 listeners = newListeners; |
|
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 |
|
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 |
|
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 } |