author | mcimadamore |
Mon, 31 Aug 2015 17:33:34 +0100 | |
changeset 32454 | b0ac04e0fefe |
parent 26266 | 2d24bda701dc |
permissions | -rw-r--r-- |
12016 | 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; |
|
26266
2d24bda701dc
8056061: Mark implementations of public interfaces with an annotation
jlahoda
parents:
25874
diff
changeset
|
34 |
import com.sun.tools.javac.util.DefinedBy; |
2d24bda701dc
8056061: Mark implementations of public interfaces with an annotation
jlahoda
parents:
25874
diff
changeset
|
35 |
import com.sun.tools.javac.util.DefinedBy.Api; |
12016 | 36 |
|
37 |
/** |
|
26266
2d24bda701dc
8056061: Mark implementations of public interfaces with an annotation
jlahoda
parents:
25874
diff
changeset
|
38 |
* A collection of currently registered {@link TaskListener}s. Events passed to this TaskListener |
2d24bda701dc
8056061: Mark implementations of public interfaces with an annotation
jlahoda
parents:
25874
diff
changeset
|
39 |
* will be forwarded to all the registered TaskListeners. |
12016 | 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. */ |
|
22163 | 48 |
public static final Context.Key<MultiTaskListener> taskListenerKey = new Context.Key<>(); |
12016 | 49 |
|
32454
b0ac04e0fefe
8129962: Investigate performance improvements in langtools combo tests
mcimadamore
parents:
26266
diff
changeset
|
50 |
/** Empty array of task listeners */ |
b0ac04e0fefe
8129962: Investigate performance improvements in langtools combo tests
mcimadamore
parents:
26266
diff
changeset
|
51 |
private static final TaskListener[] EMPTY_LISTENERS = new TaskListener[0]; |
b0ac04e0fefe
8129962: Investigate performance improvements in langtools combo tests
mcimadamore
parents:
26266
diff
changeset
|
52 |
|
12016 | 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 |
*/ |
|
32454
b0ac04e0fefe
8129962: Investigate performance improvements in langtools combo tests
mcimadamore
parents:
26266
diff
changeset
|
70 |
TaskListener[] listeners = EMPTY_LISTENERS; |
12016 | 71 |
|
72 |
ClientCodeWrapper ccw; |
|
73 |
||
74 |
public Collection<TaskListener> getTaskListeners() { |
|
75 |
return Arrays.asList(listeners); |
|
76 |
} |
|
77 |
||
78 |
public boolean isEmpty() { |
|
32454
b0ac04e0fefe
8129962: Investigate performance improvements in langtools combo tests
mcimadamore
parents:
26266
diff
changeset
|
79 |
return listeners == EMPTY_LISTENERS; |
12016 | 80 |
} |
81 |
||
82 |
public void add(TaskListener listener) { |
|
83 |
for (TaskListener l: listeners) { |
|
84 |
if (ccw.unwrap(l) == listener) |
|
85 |
throw new IllegalStateException(); |
|
86 |
} |
|
14049
3207422a0f9b
7193657: provide internal ArrayUtils class to simplify common usage of arrays in javac
jjg
parents:
12016
diff
changeset
|
87 |
listeners = Arrays.copyOf(listeners, listeners.length + 1); |
3207422a0f9b
7193657: provide internal ArrayUtils class to simplify common usage of arrays in javac
jjg
parents:
12016
diff
changeset
|
88 |
listeners[listeners.length - 1] = ccw.wrap(listener); |
12016 | 89 |
} |
90 |
||
91 |
public void remove(TaskListener listener) { |
|
92 |
for (int i = 0; i < listeners.length; i++) { |
|
93 |
if (ccw.unwrap(listeners[i]) == listener) { |
|
32454
b0ac04e0fefe
8129962: Investigate performance improvements in langtools combo tests
mcimadamore
parents:
26266
diff
changeset
|
94 |
if (listeners.length == 1) { |
b0ac04e0fefe
8129962: Investigate performance improvements in langtools combo tests
mcimadamore
parents:
26266
diff
changeset
|
95 |
listeners = EMPTY_LISTENERS; |
b0ac04e0fefe
8129962: Investigate performance improvements in langtools combo tests
mcimadamore
parents:
26266
diff
changeset
|
96 |
} else { |
b0ac04e0fefe
8129962: Investigate performance improvements in langtools combo tests
mcimadamore
parents:
26266
diff
changeset
|
97 |
TaskListener[] newListeners = new TaskListener[listeners.length - 1]; |
b0ac04e0fefe
8129962: Investigate performance improvements in langtools combo tests
mcimadamore
parents:
26266
diff
changeset
|
98 |
System.arraycopy(listeners, 0, newListeners, 0, i); |
b0ac04e0fefe
8129962: Investigate performance improvements in langtools combo tests
mcimadamore
parents:
26266
diff
changeset
|
99 |
System.arraycopy(listeners, i + 1, newListeners, i, newListeners.length - i); |
b0ac04e0fefe
8129962: Investigate performance improvements in langtools combo tests
mcimadamore
parents:
26266
diff
changeset
|
100 |
listeners = newListeners; |
b0ac04e0fefe
8129962: Investigate performance improvements in langtools combo tests
mcimadamore
parents:
26266
diff
changeset
|
101 |
} |
12016 | 102 |
break; |
103 |
} |
|
104 |
} |
|
105 |
} |
|
106 |
||
26266
2d24bda701dc
8056061: Mark implementations of public interfaces with an annotation
jlahoda
parents:
25874
diff
changeset
|
107 |
@Override @DefinedBy(Api.COMPILER_TREE) |
12016 | 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 |
||
26266
2d24bda701dc
8056061: Mark implementations of public interfaces with an annotation
jlahoda
parents:
25874
diff
changeset
|
115 |
@Override @DefinedBy(Api.COMPILER_TREE) |
12016 | 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 |
} |
|
32454
b0ac04e0fefe
8129962: Investigate performance improvements in langtools combo tests
mcimadamore
parents:
26266
diff
changeset
|
127 |
|
b0ac04e0fefe
8129962: Investigate performance improvements in langtools combo tests
mcimadamore
parents:
26266
diff
changeset
|
128 |
public void clear() { |
b0ac04e0fefe
8129962: Investigate performance improvements in langtools combo tests
mcimadamore
parents:
26266
diff
changeset
|
129 |
listeners = EMPTY_LISTENERS; |
b0ac04e0fefe
8129962: Investigate performance improvements in langtools combo tests
mcimadamore
parents:
26266
diff
changeset
|
130 |
} |
12016 | 131 |
} |