Line 0
Link Here
|
|
|
1 |
/* |
2 |
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER. |
3 |
* |
4 |
* Copyright 2015 Oracle and/or its affiliates. All rights reserved. |
5 |
* |
6 |
* Oracle and Java are registered trademarks of Oracle and/or its affiliates. |
7 |
* Other names may be trademarks of their respective owners. |
8 |
* |
9 |
* The contents of this file are subject to the terms of either the GNU |
10 |
* General Public License Version 2 only ("GPL") or the Common |
11 |
* Development and Distribution License("CDDL") (collectively, the |
12 |
* "License"). You may not use this file except in compliance with the |
13 |
* License. You can obtain a copy of the License at |
14 |
* http://www.netbeans.org/cddl-gplv2.html |
15 |
* or nbbuild/licenses/CDDL-GPL-2-CP. See the License for the |
16 |
* specific language governing permissions and limitations under the |
17 |
* License. When distributing the software, include this License Header |
18 |
* Notice in each file and include the License file at |
19 |
* nbbuild/licenses/CDDL-GPL-2-CP. Oracle designates this |
20 |
* particular file as subject to the "Classpath" exception as provided |
21 |
* by Oracle in the GPL Version 2 section of the License file that |
22 |
* accompanied this code. If applicable, add the following below the |
23 |
* License Header, with the fields enclosed by brackets [] replaced by |
24 |
* your own identifying information: |
25 |
* "Portions Copyrighted [year] [name of copyright owner]" |
26 |
* |
27 |
* If you wish your version of this file to be governed by only the CDDL |
28 |
* or only the GPL Version 2, indicate your decision by adding |
29 |
* "[Contributor] elects to include this software in this distribution |
30 |
* under the [CDDL or GPL Version 2] license." If you do not indicate a |
31 |
* single choice of license, a recipient has the option to distribute |
32 |
* your version of this file under either the CDDL, the GPL Version 2 or |
33 |
* to extend the choice of license to its licensees as provided above. |
34 |
* However, if you add GPL Version 2 code and therefore, elected the GPL |
35 |
* Version 2 license, then the option applies only if the new code is |
36 |
* made subject to such option by the copyright holder. |
37 |
* |
38 |
* Contributor(s): |
39 |
* |
40 |
* Portions Copyrighted 2015 Sun Microsystems, Inc. |
41 |
*/ |
42 |
package org.openide.util; |
43 |
|
44 |
import java.lang.ref.PhantomReference; |
45 |
import java.lang.ref.Reference; |
46 |
import java.lang.ref.SoftReference; |
47 |
import java.lang.ref.WeakReference; |
48 |
import java.util.concurrent.atomic.AtomicBoolean; |
49 |
import org.openide.util.lookup.implspi.CallbackReferencesSupport; |
50 |
|
51 |
/** |
52 |
* This class provides references, that are able to call a defined callback, |
53 |
* after the referent object becomes unreachable. It is useful when there is |
54 |
* a need to run a callback code after the referent is collected. |
55 |
* <p> |
56 |
* Usually, in order to implement such logic, one needs to either create |
57 |
* a dedicated thread that blocks on the queue and is <code>Object.notify</code>-ed, |
58 |
* which is the right approach but consumes valuable system resources (threads), |
59 |
* or one can periodically check the content of the queue by |
60 |
* <code>RequestProcessor.Task.schedule</code> which is completely wrong, |
61 |
* because it wakes up the system every (say) 15 seconds. |
62 |
* <p> |
63 |
* In order to provide an efficient support for this problem, these references |
64 |
* has been provided. They share one background thread to process the callbacks, |
65 |
* which dies when there are no more callbacks to process.<br> |
66 |
* Be sure not to block in such callbacks for a long time as this prevents other |
67 |
* waiting references from processing. |
68 |
* |
69 |
* @author Martin Entlicher |
70 |
* @since 9.8 |
71 |
*/ |
72 |
public final class CallbackReferences { |
73 |
|
74 |
private CallbackReferences() {} |
75 |
|
76 |
private static <T> T referentCheck(T referent) { |
77 |
if (referent == null) { |
78 |
throw new NullPointerException("The referent must not be null."); // NOI18N |
79 |
} |
80 |
return referent; |
81 |
} |
82 |
|
83 |
/** |
84 |
* An implementation of {@link PhantomReference} with a callback functionality. |
85 |
* After the referent becomes unreachable, the provided Runnable callback is executed. |
86 |
*/ |
87 |
public static class Phantom<T> extends PhantomReference<T> { |
88 |
|
89 |
private static final AtomicBoolean REGISTERED = new AtomicBoolean(false); |
90 |
|
91 |
final CallbackReferencesSupport support; |
92 |
|
93 |
/** |
94 |
* Create a new phantom reference with a callback. Both referent and the |
95 |
* callback should not be <code>null</code>. |
96 |
* @param referent the object the new phantom reference will refer to |
97 |
* @param callback the callback executed after the referent becomes unreachable. |
98 |
*/ |
99 |
public Phantom(T referent, Runnable callback) { |
100 |
this(referentCheck(referent), new CallbackReferencesSupport(callback)); |
101 |
} |
102 |
|
103 |
private Phantom(T referent, CallbackReferencesSupport support) { |
104 |
super(referent, support.getReferenceQueue()); |
105 |
this.support = support; |
106 |
if (!REGISTERED.getAndSet(true)) { |
107 |
CallbackReferencesSupport.addReferenceSupportProvider(Phantom.class, new SupportProvider()); |
108 |
} |
109 |
} |
110 |
|
111 |
@Override |
112 |
protected final void finalize() throws Throwable { |
113 |
super.finalize(); |
114 |
support.notifyFinalized(); |
115 |
} |
116 |
|
117 |
private static final class SupportProvider implements CallbackReferencesSupport.ReferenceSupportProvider { |
118 |
|
119 |
@Override |
120 |
public CallbackReferencesSupport getSupport(Reference reference) { |
121 |
return ((Phantom) reference).support; |
122 |
} |
123 |
|
124 |
} |
125 |
|
126 |
} |
127 |
|
128 |
/** |
129 |
* An implementation of {@link SoftReference} with a callback functionality. |
130 |
* After the referent becomes unreachable, the provided Runnable callback is executed. |
131 |
*/ |
132 |
public static class Soft<T> extends SoftReference<T> { |
133 |
|
134 |
private static final AtomicBoolean REGISTERED = new AtomicBoolean(false); |
135 |
|
136 |
final CallbackReferencesSupport support; |
137 |
|
138 |
/** |
139 |
* Create a new soft reference with a callback. Both referent and the |
140 |
* callback should not be <code>null</code>. |
141 |
* @param referent the object the new soft reference will refer to |
142 |
* @param callback the callback executed after the referent becomes unreachable. |
143 |
*/ |
144 |
public Soft(T referent, Runnable callback) { |
145 |
this(referentCheck(referent), new CallbackReferencesSupport(callback)); |
146 |
} |
147 |
|
148 |
private Soft(T referent, CallbackReferencesSupport support) { |
149 |
super(referent, support.getReferenceQueue()); |
150 |
this.support = support; |
151 |
if (!REGISTERED.getAndSet(true)) { |
152 |
CallbackReferencesSupport.addReferenceSupportProvider(Soft.class, new SupportProvider()); |
153 |
} |
154 |
} |
155 |
|
156 |
@Override |
157 |
protected final void finalize() throws Throwable { |
158 |
support.notifyFinalized(); |
159 |
super.finalize(); |
160 |
} |
161 |
|
162 |
private static final class SupportProvider implements CallbackReferencesSupport.ReferenceSupportProvider { |
163 |
|
164 |
@Override |
165 |
public CallbackReferencesSupport getSupport(Reference reference) { |
166 |
return ((Soft) reference).support; |
167 |
} |
168 |
|
169 |
} |
170 |
|
171 |
} |
172 |
|
173 |
/** |
174 |
* An implementation of {@link WeakReference} with a callback functionality. |
175 |
* After the referent becomes unreachable, the provided Runnable callback is executed. |
176 |
*/ |
177 |
public static class Weak<T> extends WeakReference<T> { |
178 |
|
179 |
private static final AtomicBoolean REGISTERED = new AtomicBoolean(false); |
180 |
|
181 |
final CallbackReferencesSupport support; |
182 |
|
183 |
/** |
184 |
* Create a new weak reference with a callback. Both referent and the |
185 |
* callback should not be <code>null</code>. |
186 |
* @param referent the object the new weak reference will refer to |
187 |
* @param callback the callback executed after the referent becomes unreachable. |
188 |
*/ |
189 |
public Weak(T referent, Runnable callback) { |
190 |
this(referentCheck(referent), new CallbackReferencesSupport(callback)); |
191 |
} |
192 |
|
193 |
private Weak(T referent, CallbackReferencesSupport support) { |
194 |
super(referent, support.getReferenceQueue()); |
195 |
this.support = support; |
196 |
if (!REGISTERED.getAndSet(true)) { |
197 |
CallbackReferencesSupport.addReferenceSupportProvider(Weak.class, new SupportProvider()); |
198 |
} |
199 |
} |
200 |
|
201 |
@Override |
202 |
protected final void finalize() throws Throwable { |
203 |
support.notifyFinalized(); |
204 |
super.finalize(); |
205 |
} |
206 |
|
207 |
private static final class SupportProvider implements CallbackReferencesSupport.ReferenceSupportProvider { |
208 |
|
209 |
@Override |
210 |
public CallbackReferencesSupport getSupport(Reference reference) { |
211 |
return ((Weak) reference).support; |
212 |
} |
213 |
|
214 |
} |
215 |
|
216 |
} |
217 |
|
218 |
/** |
219 |
* An abstract {@link PhantomReference}, which acts itself as a callback |
220 |
* by implementing {@link Runable}. |
221 |
* After the referent becomes unreachable, it's <code>run()</code> method |
222 |
* is executed. |
223 |
*/ |
224 |
public static abstract class PhantomCallback<T> extends Phantom<T> implements Runnable { |
225 |
|
226 |
/** |
227 |
* Create a new phantom callback reference. |
228 |
* @param referent the object the new phantom reference will refer to |
229 |
*/ |
230 |
public PhantomCallback(T referent) { |
231 |
super(referentCheck(referent), new CallbackReferencesSupport()); |
232 |
support.setCallback(this); |
233 |
} |
234 |
|
235 |
} |
236 |
|
237 |
/** |
238 |
* An abstract {@link SoftReference}, which acts itself as a callback |
239 |
* by implementing {@link Runable}. |
240 |
* After the referent becomes unreachable, it's <code>run()</code> method |
241 |
* is executed. |
242 |
*/ |
243 |
public static abstract class SoftCallback<T> extends Soft<T> implements Runnable { |
244 |
|
245 |
/** |
246 |
* Create a new soft callback reference. |
247 |
* @param referent the object the new phantom reference will refer to |
248 |
*/ |
249 |
public SoftCallback(T referent) { |
250 |
super(referentCheck(referent), new CallbackReferencesSupport()); |
251 |
support.setCallback(this); |
252 |
} |
253 |
|
254 |
} |
255 |
|
256 |
/** |
257 |
* An abstract {@link WeakReference}, which acts itself as a callback |
258 |
* by implementing {@link Runable}. |
259 |
* After the referent becomes unreachable, it's <code>run()</code> method |
260 |
* is executed. |
261 |
*/ |
262 |
public static abstract class WeakCallback<T> extends Weak<T> implements Runnable { |
263 |
|
264 |
/** |
265 |
* Create a new weak callback reference. |
266 |
* @param referent the object the new phantom reference will refer to |
267 |
*/ |
268 |
public WeakCallback(T referent) { |
269 |
super(referentCheck(referent), new CallbackReferencesSupport()); |
270 |
support.setCallback(this); |
271 |
} |
272 |
|
273 |
} |
274 |
|
275 |
} |