Lines 24-29
Link Here
|
24 |
import java.lang.ref.ReferenceQueue; |
24 |
import java.lang.ref.ReferenceQueue; |
25 |
|
25 |
|
26 |
import java.util.*; |
26 |
import java.util.*; |
|
|
27 |
import org.openide.util.Utilities; |
27 |
|
28 |
|
28 |
|
29 |
|
29 |
/** Implementation of the lookup from OpenAPIs that is based on the |
30 |
/** Implementation of the lookup from OpenAPIs that is based on the |
Lines 40-52
Link Here
|
40 |
/** lock for initialization of the maps of lookups */ |
41 |
/** lock for initialization of the maps of lookups */ |
41 |
private static Object treeLock = new Object(); |
42 |
private static Object treeLock = new Object(); |
42 |
|
43 |
|
43 |
/** This is a workaround for bug 35366 that probably has to be here until |
|
|
44 |
* we end producing separate openide-lookup.jar. The activeReferenceQueue |
45 |
* has to be obtained using reflection to prevent the openide-lookup.jar |
46 |
* to be as large as openide-util.jar. |
47 |
*/ |
48 |
private static Object activeQueue; |
49 |
|
50 |
/** the tree that registers all items (or Integer as a treshold size) */ |
44 |
/** the tree that registers all items (or Integer as a treshold size) */ |
51 |
private Object tree; |
45 |
private Object tree; |
52 |
|
46 |
|
Lines 68-74
Link Here
|
68 |
/** Constructor for testing purposes that allows specification of storage |
62 |
/** Constructor for testing purposes that allows specification of storage |
69 |
* as mechanism as well. |
63 |
* as mechanism as well. |
70 |
*/ |
64 |
*/ |
71 |
AbstractLookup(Content content, Storage storage) { |
65 |
AbstractLookup(Content content, Storage<?> storage) { |
72 |
this(content); |
66 |
this(content); |
73 |
this.tree = storage; |
67 |
this.tree = storage; |
74 |
initialize(); |
68 |
initialize(); |
Lines 90-96
Link Here
|
90 |
|
84 |
|
91 |
public String toString() { |
85 |
public String toString() { |
92 |
if (tree instanceof Storage) { |
86 |
if (tree instanceof Storage) { |
93 |
return "AbstractLookup" + lookup(new Lookup.Template(Object.class)).allItems(); // NOI18N |
87 |
return "AbstractLookup" + lookup(new Lookup.Template<Object>(Object.class)).allItems(); // NOI18N |
94 |
} else { |
88 |
} else { |
95 |
return super.toString(); |
89 |
return super.toString(); |
96 |
} |
90 |
} |
Lines 98-104
Link Here
|
98 |
|
92 |
|
99 |
/** Entres the storage management system. |
93 |
/** Entres the storage management system. |
100 |
*/ |
94 |
*/ |
101 |
private AbstractLookup.Storage enterStorage() { |
95 |
@SuppressWarnings("unchecked") |
|
|
96 |
private <T> AbstractLookup.Storage<T> enterStorage() { |
102 |
for (;;) { |
97 |
for (;;) { |
103 |
synchronized (treeLock) { |
98 |
synchronized (treeLock) { |
104 |
if (tree instanceof AbstractLookup.Storage) { |
99 |
if (tree instanceof AbstractLookup.Storage) { |
Lines 119-127
Link Here
|
119 |
continue; |
114 |
continue; |
120 |
} else { |
115 |
} else { |
121 |
// ok, tree is initialized and nobody is using it yet |
116 |
// ok, tree is initialized and nobody is using it yet |
122 |
tree = new DelegatingStorage((Storage) tree); |
117 |
tree = new DelegatingStorage((Storage<T>) tree); |
123 |
|
118 |
|
124 |
return (Storage) tree; |
119 |
return (Storage<T>) tree; |
125 |
} |
120 |
} |
126 |
} |
121 |
} |
127 |
|
122 |
|
Lines 158-174
Link Here
|
158 |
/** Notifies subclasses that a query is about to be processed. |
153 |
/** Notifies subclasses that a query is about to be processed. |
159 |
* @param template the template |
154 |
* @param template the template |
160 |
*/ |
155 |
*/ |
161 |
protected void beforeLookup(Template template) { |
156 |
protected void beforeLookup(Template<?> template) { |
162 |
} |
157 |
} |
163 |
|
158 |
|
164 |
/** The method to add instance to the lookup with. |
159 |
/** The method to add instance to the lookup with. |
165 |
* @param pair class/instance pair |
160 |
* @param pair class/instance pair |
166 |
*/ |
161 |
*/ |
167 |
protected final void addPair(Pair pair) { |
162 |
protected final void addPair(Pair<?> pair) { |
168 |
HashSet toNotify = new HashSet(); |
163 |
addPairImpl(pair); |
|
|
164 |
} |
165 |
|
166 |
private final <Transaction> void addPairImpl(Pair<?> pair) { |
167 |
HashSet<R> toNotify = new HashSet<R>(); |
169 |
|
168 |
|
170 |
Object transaction = null; |
169 |
AbstractLookup.Storage<Transaction> t = enterStorage(); |
171 |
AbstractLookup.Storage t = enterStorage(); |
170 |
Transaction transaction = null; |
172 |
|
171 |
|
173 |
try { |
172 |
try { |
174 |
transaction = t.beginTransaction(-2); |
173 |
transaction = t.beginTransaction(-2); |
Lines 188-194
Link Here
|
188 |
t.endTransaction(transaction, toNotify); |
187 |
t.endTransaction(transaction, toNotify); |
189 |
} else { |
188 |
} else { |
190 |
// just finish the process by calling endTransaction |
189 |
// just finish the process by calling endTransaction |
191 |
t.endTransaction(transaction, new HashSet()); |
190 |
t.endTransaction(transaction, new HashSet<R>()); |
192 |
} |
191 |
} |
193 |
} finally { |
192 |
} finally { |
194 |
exitStorage(); |
193 |
exitStorage(); |
Lines 200-210
Link Here
|
200 |
/** Remove instance. |
199 |
/** Remove instance. |
201 |
* @param pair class/instance pair |
200 |
* @param pair class/instance pair |
202 |
*/ |
201 |
*/ |
203 |
protected final void removePair(Pair pair) { |
202 |
protected final void removePair(Pair<?> pair) { |
204 |
HashSet toNotify = new HashSet(); |
203 |
removePairImpl(pair); |
|
|
204 |
} |
205 |
|
206 |
private <Transaction> void removePairImpl(Pair<?> pair) { |
207 |
HashSet<R> toNotify = new HashSet<R>(); |
205 |
|
208 |
|
206 |
Object transaction = null; |
209 |
AbstractLookup.Storage<Transaction> t = enterStorage(); |
207 |
AbstractLookup.Storage t = enterStorage(); |
210 |
Transaction transaction = null; |
208 |
|
211 |
|
209 |
try { |
212 |
try { |
210 |
transaction = t.beginTransaction(-1); |
213 |
transaction = t.beginTransaction(-1); |
Lines 220-243
Link Here
|
220 |
/** Changes all pairs in the lookup to new values. |
223 |
/** Changes all pairs in the lookup to new values. |
221 |
* @param collection the collection of (Pair) objects |
224 |
* @param collection the collection of (Pair) objects |
222 |
*/ |
225 |
*/ |
223 |
protected final void setPairs(Collection collection) { |
226 |
protected final void setPairs(Collection<? extends Pair> collection) { |
224 |
notifyCollectedListeners(setPairsAndCollectListeners(collection)); |
227 |
notifyCollectedListeners(setPairsAndCollectListeners(collection)); |
225 |
} |
228 |
} |
226 |
|
229 |
|
227 |
/** Getter for set of pairs. Package private contract with MetaInfServicesLookup. |
230 |
/** Getter for set of pairs. Package private contract with MetaInfServicesLookup. |
228 |
* @return a LinkedHashSet that can be modified |
231 |
* @return a LinkedHashSet that can be modified |
229 |
*/ |
232 |
*/ |
230 |
final LinkedHashSet getPairsAsLHS() { |
233 |
final LinkedHashSet<Pair<?>> getPairsAsLHS() { |
231 |
AbstractLookup.Storage t = enterStorage(); |
234 |
AbstractLookup.Storage<?> t = enterStorage(); |
232 |
|
235 |
|
233 |
try { |
236 |
try { |
234 |
Enumeration en = t.lookup(Object.class); |
237 |
Enumeration<Pair<Object>> en = t.lookup(Object.class); |
235 |
LinkedHashSet arr = new LinkedHashSet(); |
238 |
LinkedHashSet<Pair<?>> arr = new LinkedHashSet<Pair<?>>(); |
236 |
while (en.hasMoreElements()) { |
239 |
while (en.hasMoreElements()) { |
237 |
Pair item = (Pair) en.nextElement(); |
240 |
Pair<Object> item = en.nextElement(); |
238 |
arr.add(item); |
241 |
arr.add(item); |
239 |
} |
242 |
} |
240 |
return arr; |
243 |
return arr; |
241 |
} finally { |
244 |
} finally { |
242 |
exitStorage(); |
245 |
exitStorage(); |
243 |
} |
246 |
} |
Lines 246-260
Link Here
|
246 |
/** Collects listeners without notification. Needed in MetaInfServicesLookup |
249 |
/** Collects listeners without notification. Needed in MetaInfServicesLookup |
247 |
* right now, but maybe will become an API later. |
250 |
* right now, but maybe will become an API later. |
248 |
*/ |
251 |
*/ |
249 |
final HashSet setPairsAndCollectListeners(Collection collection) { |
252 |
final <Transaction> HashSet<R> setPairsAndCollectListeners(Collection<? extends Pair> collection) { |
250 |
HashSet toNotify = new HashSet(27); |
253 |
HashSet<R> toNotify = new HashSet<R>(27); |
251 |
|
254 |
|
252 |
Object transaction = null; |
255 |
AbstractLookup.Storage<Transaction> t = enterStorage(); |
253 |
AbstractLookup.Storage t = enterStorage(); |
256 |
Transaction transaction = null; |
254 |
|
257 |
|
255 |
try { |
258 |
try { |
256 |
// map between the Items and their indexes (Integer) |
259 |
// map between the Items and their indexes (Integer) |
257 |
HashMap shouldBeThere = new HashMap(collection.size() * 2); |
260 |
HashMap<Item<?>,Info> shouldBeThere = new HashMap<Item<?>,Info>(collection.size() * 2); |
258 |
|
261 |
|
259 |
count = 0; |
262 |
count = 0; |
260 |
|
263 |
|
Lines 311-318
Link Here
|
311 |
/** Notifies all collected listeners. Needed by MetaInfServicesLookup, |
314 |
/** Notifies all collected listeners. Needed by MetaInfServicesLookup, |
312 |
* maybe it will be an API later. |
315 |
* maybe it will be an API later. |
313 |
*/ |
316 |
*/ |
314 |
final void notifyCollectedListeners(Object listeners) { |
317 |
final void notifyCollectedListeners(Set<R> listeners) { |
315 |
notifyListeners((HashSet) listeners); |
318 |
notifyListeners(listeners); |
316 |
} |
319 |
} |
317 |
|
320 |
|
318 |
private final void writeObject(ObjectOutputStream oos) |
321 |
private final void writeObject(ObjectOutputStream oos) |
Lines 330-349
Link Here
|
330 |
} |
333 |
} |
331 |
} |
334 |
} |
332 |
|
335 |
|
333 |
public final Object lookup(Class clazz) { |
336 |
public final <T> T lookup(Class<T> clazz) { |
334 |
Lookup.Item item = lookupItem(new Lookup.Template(clazz)); |
337 |
Lookup.Item<T> item = lookupItem(new Lookup.Template<T>(clazz)); |
335 |
|
|
|
336 |
return (item == null) ? null : item.getInstance(); |
338 |
return (item == null) ? null : item.getInstance(); |
337 |
} |
339 |
} |
338 |
|
340 |
|
339 |
public final Lookup.Item lookupItem(Lookup.Template template) { |
341 |
public final <T> Lookup.Item<T> lookupItem(Lookup.Template<T> template) { |
340 |
AbstractLookup.this.beforeLookup(template); |
342 |
AbstractLookup.this.beforeLookup(template); |
341 |
|
343 |
|
342 |
ArrayList list = null; |
344 |
ArrayList<Pair<T>> list = null; |
343 |
AbstractLookup.Storage t = enterStorage(); |
345 |
AbstractLookup.Storage<?> t = enterStorage(); |
344 |
|
346 |
|
345 |
try { |
347 |
try { |
346 |
Enumeration en; |
348 |
Enumeration<Pair<T>> en; |
347 |
|
349 |
|
348 |
try { |
350 |
try { |
349 |
en = t.lookup(template.getType()); |
351 |
en = t.lookup(template.getType()); |
Lines 352-358
Link Here
|
352 |
} catch (AbstractLookup.ISE ex) { |
354 |
} catch (AbstractLookup.ISE ex) { |
353 |
// not possible to enumerate the exception, ok, copy it |
355 |
// not possible to enumerate the exception, ok, copy it |
354 |
// to create new |
356 |
// to create new |
355 |
list = new ArrayList(); |
357 |
list = new ArrayList<Pair<T>>(); |
356 |
en = t.lookup(null); // this should get all the items without any checks |
358 |
en = t.lookup(null); // this should get all the items without any checks |
357 |
|
359 |
|
358 |
// the checks will be done out side of the storage |
360 |
// the checks will be done out side of the storage |
Lines 367-378
Link Here
|
367 |
return findSmallest(Collections.enumeration(list), template, true); |
369 |
return findSmallest(Collections.enumeration(list), template, true); |
368 |
} |
370 |
} |
369 |
|
371 |
|
370 |
private static Pair findSmallest(Enumeration en, Lookup.Template template, boolean deepCheck) { |
372 |
private static <T> Pair<T> findSmallest(Enumeration<Pair<T>> en, Lookup.Template<T> template, boolean deepCheck) { |
371 |
int smallest = InheritanceTree.unsorted(en) ? Integer.MAX_VALUE : Integer.MIN_VALUE; |
373 |
int smallest = InheritanceTree.unsorted(en) ? Integer.MAX_VALUE : Integer.MIN_VALUE; |
372 |
Pair res = null; |
374 |
Pair<T> res = null; |
373 |
|
375 |
|
374 |
while (en.hasMoreElements()) { |
376 |
while (en.hasMoreElements()) { |
375 |
Pair item = (Pair) en.nextElement(); |
377 |
Pair<T> item = en.nextElement(); |
376 |
|
378 |
|
377 |
if (matches(template, item, deepCheck)) { |
379 |
if (matches(template, item, deepCheck)) { |
378 |
if (smallest == Integer.MIN_VALUE) { |
380 |
if (smallest == Integer.MIN_VALUE) { |
Lines 391-405
Link Here
|
391 |
return res; |
393 |
return res; |
392 |
} |
394 |
} |
393 |
|
395 |
|
394 |
public final Lookup.Result lookup(Lookup.Template template) { |
396 |
public final <T> Lookup.Result<T> lookup(Lookup.Template<T> template) { |
395 |
for (;;) { |
397 |
for (;;) { |
396 |
AbstractLookup.ISE toRun = null; |
398 |
AbstractLookup.ISE toRun = null; |
397 |
|
399 |
|
398 |
AbstractLookup.Storage t = enterStorage(); |
400 |
AbstractLookup.Storage<?> t = enterStorage(); |
399 |
|
401 |
|
400 |
try { |
402 |
try { |
401 |
R r = new R(); |
403 |
R<T> r = new R<T>(); |
402 |
ReferenceToResult newRef = new ReferenceToResult(r, this, template); |
404 |
ReferenceToResult<T> newRef = new ReferenceToResult<T>(r, this, template); |
403 |
newRef.next = t.registerReferenceToResult(newRef); |
405 |
newRef.next = t.registerReferenceToResult(newRef); |
404 |
|
406 |
|
405 |
return r; |
407 |
return r; |
Lines 418-433
Link Here
|
418 |
/** Notifies listeners. |
420 |
/** Notifies listeners. |
419 |
* @param allAffectedResults set of R |
421 |
* @param allAffectedResults set of R |
420 |
*/ |
422 |
*/ |
421 |
private static void notifyListeners(Set allAffectedResults) { |
423 |
private static void notifyListeners(Set<R> allAffectedResults) { |
422 |
if (allAffectedResults.isEmpty()) { |
424 |
if (allAffectedResults.isEmpty()) { |
423 |
return; |
425 |
return; |
424 |
} |
426 |
} |
425 |
|
427 |
|
426 |
ArrayList evAndListeners = new ArrayList(); |
428 |
ArrayList<Object> evAndListeners = new ArrayList<Object>(); |
427 |
{ |
429 |
{ |
428 |
Iterator it = allAffectedResults.iterator(); |
430 |
for (R<?> result : allAffectedResults) { |
429 |
while(it.hasNext()) { |
|
|
430 |
AbstractLookup.R result = (AbstractLookup.R) it.next(); |
431 |
result.collectFires(evAndListeners); |
431 |
result.collectFires(evAndListeners); |
432 |
} |
432 |
} |
433 |
} |
433 |
} |
Lines 449-462
Link Here
|
449 |
* objects on even positions and the listeners on odd positions |
449 |
* objects on even positions and the listeners on odd positions |
450 |
* @param ev the event to fire |
450 |
* @param ev the event to fire |
451 |
*/ |
451 |
*/ |
452 |
static void notifyListeners(Object[] listeners, LookupEvent ev, Collection evAndListeners) { |
452 |
static void notifyListeners(Object[] listeners, LookupEvent ev, Collection<Object> evAndListeners) { |
453 |
for (int i = listeners.length - 1; i >= 0; i -= 2) { |
453 |
for (int i = listeners.length - 1; i >= 0; i -= 2) { |
454 |
LookupListener ll = (LookupListener) listeners[i]; |
454 |
LookupListener ll = (LookupListener) listeners[i]; |
455 |
|
455 |
|
456 |
try { |
456 |
try { |
457 |
if (evAndListeners != null) { |
457 |
if (evAndListeners != null) { |
458 |
if (ll instanceof WaitableResult) { |
458 |
if (ll instanceof WaitableResult) { |
459 |
WaitableResult wr = (WaitableResult)ll; |
459 |
WaitableResult<?> wr = (WaitableResult<?>)ll; |
460 |
wr.collectFires(evAndListeners); |
460 |
wr.collectFires(evAndListeners); |
461 |
} else { |
461 |
} else { |
462 |
evAndListeners.add(ev); |
462 |
evAndListeners.add(ev); |
Lines 478-484
Link Here
|
478 |
* @param deepCheck true if type of the pair should be tested, false if it is already has been tested |
478 |
* @param deepCheck true if type of the pair should be tested, false if it is already has been tested |
479 |
* @return true if item matches the template requirements, false if not |
479 |
* @return true if item matches the template requirements, false if not |
480 |
*/ |
480 |
*/ |
481 |
static boolean matches(Template t, Pair item, boolean deepCheck) { |
481 |
static boolean matches(Template<?> t, Pair<?> item, boolean deepCheck) { |
482 |
String id = t.getId(); |
482 |
String id = t.getId(); |
483 |
|
483 |
|
484 |
if ((id != null) && !item.getId().equals(id)) { |
484 |
if ((id != null) && !item.getId().equals(id)) { |
Lines 546-553
Link Here
|
546 |
* @param template the template the result was for |
546 |
* @param template the template the result was for |
547 |
* @return true if the hash map with all items has been cleared |
547 |
* @return true if the hash map with all items has been cleared |
548 |
*/ |
548 |
*/ |
549 |
boolean cleanUpResult(Lookup.Template template) { |
549 |
<T> boolean cleanUpResult(Lookup.Template<T> template) { |
550 |
AbstractLookup.Storage t = enterStorage(); |
550 |
AbstractLookup.Storage<?> t = enterStorage(); |
551 |
|
551 |
|
552 |
try { |
552 |
try { |
553 |
return t.cleanUpResult(template) == null; |
553 |
return t.cleanUpResult(template) == null; |
Lines 563-568
Link Here
|
563 |
* @param ref the value of the reference to listener or listener list |
563 |
* @param ref the value of the reference to listener or listener list |
564 |
* @return new value to the reference to listener or list |
564 |
* @return new value to the reference to listener or list |
565 |
*/ |
565 |
*/ |
|
|
566 |
@SuppressWarnings("unchecked") |
566 |
static Object modifyListenerList(boolean add, LookupListener l, Object ref) { |
567 |
static Object modifyListenerList(boolean add, LookupListener l, Object ref) { |
567 |
if (add) { |
568 |
if (add) { |
568 |
if (ref == null) { |
569 |
if (ref == null) { |
Lines 599-623
Link Here
|
599 |
} |
600 |
} |
600 |
} |
601 |
} |
601 |
|
602 |
|
602 |
private static ReferenceQueue activeQueue() { |
603 |
private static ReferenceQueue<Object> activeQueue() { |
603 |
if (activeQueue == null) { |
604 |
return Utilities.activeReferenceQueue(); |
604 |
try { |
|
|
605 |
Class c = Class.forName("org.openide.util.Utilities"); // NOI18N |
606 |
Class[] noArgs = new Class[0]; |
607 |
java.lang.reflect.Method m = c.getDeclaredMethod("activeReferenceQueue", noArgs); // NOI18N |
608 |
activeQueue = m.invoke(null, noArgs); |
609 |
} catch (Exception ex) { |
610 |
activeQueue = ex; |
611 |
} |
612 |
} |
613 |
|
614 |
return (activeQueue instanceof ReferenceQueue) ? (ReferenceQueue) activeQueue : null; |
615 |
} |
605 |
} |
616 |
|
606 |
|
617 |
/** Storage to keep the internal structure of Pairs and to answer |
607 |
/** Storage to keep the internal structure of Pairs and to answer |
618 |
* different queries. |
608 |
* different queries. |
619 |
*/ |
609 |
*/ |
620 |
interface Storage { |
610 |
interface Storage<Transaction> { |
621 |
/** Initializes a modification operation by creating an object |
611 |
/** Initializes a modification operation by creating an object |
622 |
* that will be passsed to all add, remove, retainAll methods |
612 |
* that will be passsed to all add, remove, retainAll methods |
623 |
* and should collect enough information about the change to |
613 |
* and should collect enough information about the change to |
Lines 628-634
Link Here
|
628 |
* the amount of objects at the end |
618 |
* the amount of objects at the end |
629 |
* @return a token to identify the transaction |
619 |
* @return a token to identify the transaction |
630 |
*/ |
620 |
*/ |
631 |
public Object beginTransaction(int ensure); |
621 |
public Transaction beginTransaction(int ensure); |
632 |
|
622 |
|
633 |
/** Collects all affected results R that were modified in the |
623 |
/** Collects all affected results R that were modified in the |
634 |
* given transaction. |
624 |
* given transaction. |
Lines 636-642
Link Here
|
636 |
* @param modified place to add results R to |
626 |
* @param modified place to add results R to |
637 |
* @param transaction the transaction indentification |
627 |
* @param transaction the transaction indentification |
638 |
*/ |
628 |
*/ |
639 |
public void endTransaction(Object transaction, Set modifiedResults); |
629 |
public void endTransaction(Transaction transaction, Set<R> modifiedResults); |
640 |
|
630 |
|
641 |
/** Adds an item into the storage. |
631 |
/** Adds an item into the storage. |
642 |
* @param item to add |
632 |
* @param item to add |
Lines 644-667
Link Here
|
644 |
* @return true if the Item has been added for the first time or false if some other |
634 |
* @return true if the Item has been added for the first time or false if some other |
645 |
* item equal to this one already existed in the lookup |
635 |
* item equal to this one already existed in the lookup |
646 |
*/ |
636 |
*/ |
647 |
public boolean add(AbstractLookup.Pair item, Object transaction); |
637 |
public boolean add(AbstractLookup.Pair<?> item, Transaction transaction); |
648 |
|
638 |
|
649 |
/** Removes an item. |
639 |
/** Removes an item. |
650 |
*/ |
640 |
*/ |
651 |
public void remove(AbstractLookup.Pair item, Object transaction); |
641 |
public void remove(AbstractLookup.Pair item, Transaction transaction); |
652 |
|
642 |
|
653 |
/** Removes all items that are not present in the provided collection. |
643 |
/** Removes all items that are not present in the provided collection. |
654 |
* @param retain collection of Pairs to keep them in |
644 |
* @param retain collection of Pairs to keep them in |
655 |
* @param transaction the transaction context |
645 |
* @param transaction the transaction context |
656 |
*/ |
646 |
*/ |
657 |
public void retainAll(Map retain, Object transaction); |
647 |
public void retainAll(Map retain, Transaction transaction); |
658 |
|
648 |
|
659 |
/** Queries for instances of given class. |
649 |
/** Queries for instances of given class. |
660 |
* @param clazz the class to check |
650 |
* @param clazz the class to check |
661 |
* @return enumeration of Item |
651 |
* @return enumeration of Item |
662 |
* @see #unsorted |
652 |
* @see #unsorted |
663 |
*/ |
653 |
*/ |
664 |
public Enumeration lookup(Class clazz); |
654 |
public <T> Enumeration<Pair<T>> lookup(Class<T> clazz); |
665 |
|
655 |
|
666 |
/** Registers another reference to a result with the storage. This method |
656 |
/** Registers another reference to a result with the storage. This method |
667 |
* has also a special meaning. |
657 |
* has also a special meaning. |
Lines 671-689
Link Here
|
671 |
* the applications is expected to link from newRef to this returned |
661 |
* the applications is expected to link from newRef to this returned |
672 |
* value to form a linked list |
662 |
* value to form a linked list |
673 |
*/ |
663 |
*/ |
674 |
public ReferenceToResult registerReferenceToResult(ReferenceToResult newRef); |
664 |
public ReferenceToResult<?> registerReferenceToResult(ReferenceToResult<?> newRef); |
675 |
|
665 |
|
676 |
/** Given the provided template, Do cleanup the results. |
666 |
/** Given the provided template, Do cleanup the results. |
677 |
* @param templ template of a result(s) that should be checked |
667 |
* @param templ template of a result(s) that should be checked |
678 |
* @return null if all references for this template were cleared or one of them |
668 |
* @return null if all references for this template were cleared or one of them |
679 |
*/ |
669 |
*/ |
680 |
public ReferenceToResult cleanUpResult(Lookup.Template templ); |
670 |
public ReferenceToResult<?> cleanUpResult(Lookup.Template<?> templ); |
681 |
} |
671 |
} |
682 |
|
672 |
|
683 |
/** Extension to the default lookup item that offers additional information |
673 |
/** Extension to the default lookup item that offers additional information |
684 |
* for the data structures use in AbstractLookup |
674 |
* for the data structures use in AbstractLookup |
685 |
*/ |
675 |
*/ |
686 |
public static abstract class Pair extends Lookup.Item implements Serializable { |
676 |
public static abstract class Pair<T> extends Lookup.Item<T> implements Serializable { |
687 |
private static final long serialVersionUID = 1L; |
677 |
private static final long serialVersionUID = 1L; |
688 |
|
678 |
|
689 |
/** possition of this item in the lookup, manipulated in addPair, removePair, setPairs methods */ |
679 |
/** possition of this item in the lookup, manipulated in addPair, removePair, setPairs methods */ |
Lines 697-703
Link Here
|
697 |
return index; |
687 |
return index; |
698 |
} |
688 |
} |
699 |
|
689 |
|
700 |
final void setIndex(AbstractLookup.Storage tree, int x) { |
690 |
final void setIndex(AbstractLookup.Storage<?> tree, int x) { |
701 |
if (tree == null) { |
691 |
if (tree == null) { |
702 |
this.index = x; |
692 |
this.index = x; |
703 |
|
693 |
|
Lines 714-720
Link Here
|
714 |
/** Tests whether this item can produce object |
704 |
/** Tests whether this item can produce object |
715 |
* of class c. |
705 |
* of class c. |
716 |
*/ |
706 |
*/ |
717 |
protected abstract boolean instanceOf(Class c); |
707 |
protected abstract boolean instanceOf(Class<?> c); |
718 |
|
708 |
|
719 |
/** Method that can test whether an instance of a class has been created |
709 |
/** Method that can test whether an instance of a class has been created |
720 |
* by this item. |
710 |
* by this item. |
Lines 728-736
Link Here
|
728 |
|
718 |
|
729 |
/** Result based on one instance returned. |
719 |
/** Result based on one instance returned. |
730 |
*/ |
720 |
*/ |
731 |
static final class R extends WaitableResult { |
721 |
static final class R<T> extends WaitableResult<T> { |
732 |
/** reference our result is attached to (do not modify) */ |
722 |
/** reference our result is attached to (do not modify) */ |
733 |
public ReferenceToResult reference; |
723 |
public ReferenceToResult<T> reference; |
734 |
|
724 |
|
735 |
/** listeners on the results or pointer to one listener */ |
725 |
/** listeners on the results or pointer to one listener */ |
736 |
private Object listeners; |
726 |
private Object listeners; |
Lines 766-773
Link Here
|
766 |
return null; |
756 |
return null; |
767 |
} |
757 |
} |
768 |
|
758 |
|
769 |
private Set getClassesCache() { |
759 |
@SuppressWarnings("unchecked") |
770 |
return (Set) getFromCache(0); |
760 |
private Set<Class<? extends T>> getClassesCache() { |
|
|
761 |
return (Set<Class<? extends T>>) getFromCache(0); |
771 |
} |
762 |
} |
772 |
|
763 |
|
773 |
private void setClassesCache(Set s) { |
764 |
private void setClassesCache(Set s) { |
Lines 785-792
Link Here
|
785 |
((Object[]) reference.caches)[0] = s; |
776 |
((Object[]) reference.caches)[0] = s; |
786 |
} |
777 |
} |
787 |
|
778 |
|
788 |
private Collection getInstancesCache() { |
779 |
@SuppressWarnings("unchecked") |
789 |
return (Collection) getFromCache(1); |
780 |
private Collection<T> getInstancesCache() { |
|
|
781 |
return (Collection<T>) getFromCache(1); |
790 |
} |
782 |
} |
791 |
|
783 |
|
792 |
private void setInstancesCache(Collection c) { |
784 |
private void setInstancesCache(Collection c) { |
Lines 804-814
Link Here
|
804 |
((Object[]) reference.caches)[1] = c; |
796 |
((Object[]) reference.caches)[1] = c; |
805 |
} |
797 |
} |
806 |
|
798 |
|
807 |
private Object[] getItemsCache() { |
799 |
@SuppressWarnings("unchecked") |
808 |
return (Object[]) getFromCache(2); |
800 |
private Pair<T>[] getItemsCache() { |
|
|
801 |
return (Pair<T>[]) getFromCache(2); |
809 |
} |
802 |
} |
810 |
|
803 |
|
811 |
private void setItemsCache(Collection c) { |
804 |
private void setItemsCache(Collection<?> c) { |
812 |
if (isSimple()) { |
805 |
if (isSimple()) { |
813 |
// mark it as being used |
806 |
// mark it as being used |
814 |
reference.caches = reference; |
807 |
reference.caches = reference; |
Lines 820-826
Link Here
|
820 |
reference.caches = new Object[3]; |
813 |
reference.caches = new Object[3]; |
821 |
} |
814 |
} |
822 |
|
815 |
|
823 |
((Object[]) reference.caches)[2] = c.toArray(); |
816 |
((Object[]) reference.caches)[2] = c.toArray(new Pair[0]); |
824 |
} |
817 |
} |
825 |
|
818 |
|
826 |
private void clearCaches() { |
819 |
private void clearCaches() { |
Lines 843-849
Link Here
|
843 |
|
836 |
|
844 |
/** Delete all cached values, the template changed. |
837 |
/** Delete all cached values, the template changed. |
845 |
*/ |
838 |
*/ |
846 |
protected void collectFires(Collection evAndListeners) { |
839 |
protected void collectFires(Collection<Object> evAndListeners) { |
847 |
Object[] previousItems = getItemsCache(); |
840 |
Object[] previousItems = getItemsCache(); |
848 |
clearCaches(); |
841 |
clearCaches(); |
849 |
|
842 |
|
Lines 866-873
Link Here
|
866 |
if (listeners instanceof LookupListener) { |
859 |
if (listeners instanceof LookupListener) { |
867 |
arr = new LookupListener[] { (LookupListener) listeners }; |
860 |
arr = new LookupListener[] { (LookupListener) listeners }; |
868 |
} else { |
861 |
} else { |
869 |
ArrayList l = (ArrayList) listeners; |
862 |
ArrayList<?> l = (ArrayList<?>) listeners; |
870 |
arr = (LookupListener[]) l.toArray(new LookupListener[l.size()]); |
863 |
arr = l.toArray(new LookupListener[l.size()]); |
871 |
} |
864 |
} |
872 |
} |
865 |
} |
873 |
|
866 |
|
Lines 876-905
Link Here
|
876 |
notifyListeners(ll, ev, evAndListeners); |
869 |
notifyListeners(ll, ev, evAndListeners); |
877 |
} |
870 |
} |
878 |
|
871 |
|
879 |
public Collection allInstances() { |
872 |
public Collection<T> allInstances() { |
880 |
reference.lookup.beforeLookup(reference.template); |
873 |
reference.lookup.beforeLookup(reference.template); |
881 |
|
874 |
|
882 |
Collection s = getInstancesCache(); |
875 |
Collection<T> s = getInstancesCache(); |
883 |
|
876 |
|
884 |
if (s != null) { |
877 |
if (s != null) { |
885 |
return s; |
878 |
return s; |
886 |
} |
879 |
} |
887 |
|
880 |
|
888 |
Collection items = allItemsWithoutBeforeLookup(); |
881 |
Collection<Pair<T>> items = allItemsWithoutBeforeLookup(); |
889 |
s = new ArrayList(items.size()); |
882 |
ArrayList<T> list = new ArrayList<T>(items.size()); |
890 |
|
883 |
|
891 |
Iterator it = items.iterator(); |
884 |
Iterator<Pair<T>> it = items.iterator(); |
892 |
|
885 |
|
893 |
while (it.hasNext()) { |
886 |
while (it.hasNext()) { |
894 |
Pair item = (Pair) it.next(); |
887 |
Pair<T> item = it.next(); |
895 |
Object obj = item.getInstance(); |
888 |
T obj = item.getInstance(); |
896 |
|
889 |
|
897 |
if (reference.template.getType().isInstance(obj)) { |
890 |
if (reference.template.getType().isInstance(obj)) { |
898 |
s.add(obj); |
891 |
list.add(obj); |
899 |
} |
892 |
} |
900 |
} |
893 |
} |
901 |
|
894 |
|
902 |
s = Collections.unmodifiableList((ArrayList)s); |
895 |
s = Collections.unmodifiableList(list); |
903 |
setInstancesCache(s); |
896 |
setInstancesCache(s); |
904 |
|
897 |
|
905 |
return s; |
898 |
return s; |
Lines 908-929
Link Here
|
908 |
/** Set of all classes. |
901 |
/** Set of all classes. |
909 |
* |
902 |
* |
910 |
*/ |
903 |
*/ |
911 |
public Set allClasses() { |
904 |
public Set<Class<? extends T>> allClasses() { |
912 |
reference.lookup.beforeLookup(reference.template); |
905 |
reference.lookup.beforeLookup(reference.template); |
913 |
|
906 |
|
914 |
Set s = getClassesCache(); |
907 |
Set<Class<? extends T>> s = getClassesCache(); |
915 |
|
908 |
|
916 |
if (s != null) { |
909 |
if (s != null) { |
917 |
return s; |
910 |
return s; |
918 |
} |
911 |
} |
919 |
|
912 |
|
920 |
s = new HashSet(); |
913 |
s = new HashSet<Class<? extends T>>(); |
921 |
|
914 |
|
922 |
Iterator it = allItemsWithoutBeforeLookup().iterator(); |
915 |
for (Pair<T> item : allItemsWithoutBeforeLookup()) { |
923 |
|
916 |
Class<? extends T> clazz = item.getType(); |
924 |
while (it.hasNext()) { |
|
|
925 |
Item item = (Item) it.next(); |
926 |
Class clazz = item.getType(); |
927 |
|
917 |
|
928 |
if (clazz != null) { |
918 |
if (clazz != null) { |
929 |
s.add(clazz); |
919 |
s.add(clazz); |
Lines 938-959
Link Here
|
938 |
|
928 |
|
939 |
/** Items are stored directly in the allItems. |
929 |
/** Items are stored directly in the allItems. |
940 |
*/ |
930 |
*/ |
941 |
public Collection allItems() { |
931 |
public Collection<? extends Item<T>> allItems() { |
942 |
reference.lookup.beforeLookup(reference.template); |
932 |
reference.lookup.beforeLookup(reference.template); |
943 |
|
933 |
|
944 |
return allItemsWithoutBeforeLookup(); |
934 |
return allItemsWithoutBeforeLookup(); |
945 |
} |
935 |
} |
946 |
|
936 |
|
947 |
/** Implements the search for allItems, but without asking for before lookup */ |
937 |
/** Implements the search for allItems, but without asking for before lookup */ |
948 |
private Collection allItemsWithoutBeforeLookup() { |
938 |
private Collection<Pair<T>> allItemsWithoutBeforeLookup() { |
949 |
Object[] c = getItemsCache(); |
939 |
Pair<T>[] c = getItemsCache(); |
950 |
|
940 |
|
951 |
if (c != null) { |
941 |
if (c != null) { |
952 |
return Collections.unmodifiableList(Arrays.asList(c)); |
942 |
return Collections.unmodifiableList(Arrays.asList(c)); |
953 |
} |
943 |
} |
954 |
|
944 |
|
955 |
ArrayList saferCheck = null; |
945 |
ArrayList<Pair<Object>> saferCheck = null; |
956 |
AbstractLookup.Storage t = reference.lookup.enterStorage(); |
946 |
AbstractLookup.Storage<?> t = reference.lookup.enterStorage(); |
957 |
|
947 |
|
958 |
try { |
948 |
try { |
959 |
try { |
949 |
try { |
Lines 961-1007
Link Here
|
961 |
} catch (AbstractLookup.ISE ex) { |
951 |
} catch (AbstractLookup.ISE ex) { |
962 |
// do less effective evaluation of items outside of the |
952 |
// do less effective evaluation of items outside of the |
963 |
// locked storage |
953 |
// locked storage |
964 |
saferCheck = new ArrayList(); |
954 |
saferCheck = new ArrayList<Pair<Object>>(); |
965 |
|
955 |
|
966 |
Enumeration en = t.lookup(null); // get all Pairs |
956 |
Enumeration<Pair<Object>> en = t.lookup(null); // get all Pairs |
967 |
|
957 |
|
968 |
while (en.hasMoreElements()) { |
958 |
while (en.hasMoreElements()) { |
969 |
Pair i = (Pair) en.nextElement(); |
959 |
Pair<Object> i = en.nextElement(); |
970 |
; |
|
|
971 |
saferCheck.add(i); |
960 |
saferCheck.add(i); |
972 |
} |
961 |
} |
973 |
} |
962 |
} |
974 |
} finally { |
963 |
} finally { |
975 |
reference.lookup.exitStorage(); |
964 |
reference.lookup.exitStorage(); |
976 |
} |
965 |
} |
|
|
966 |
return extractPairs(saferCheck); |
967 |
} |
977 |
|
968 |
|
978 |
Iterator it = saferCheck.iterator(); |
969 |
@SuppressWarnings("unchecked") |
979 |
|
970 |
private Collection<Pair<T>> extractPairs(final ArrayList<Pair<Object>> saferCheck) { |
980 |
// InheritanceTree is comparator for AbstractLookup.Pairs |
971 |
TreeSet<Pair<T>> items = new TreeSet<Pair<T>>(ALPairComparator.DEFAULT); |
981 |
TreeSet items = new TreeSet(ALPairComparator.DEFAULT); |
972 |
for (Pair<Object> i : saferCheck) { |
982 |
|
|
|
983 |
while (it.hasNext()) { |
984 |
Pair i = (Pair) it.next(); |
985 |
|
986 |
if (matches(reference.template, i, false)) { |
973 |
if (matches(reference.template, i, false)) { |
987 |
items.add(i); |
974 |
items.add((Pair<T>)i); |
988 |
} |
975 |
} |
989 |
} |
976 |
} |
990 |
|
|
|
991 |
return Collections.unmodifiableCollection(items); |
977 |
return Collections.unmodifiableCollection(items); |
992 |
} |
978 |
} |
993 |
|
979 |
|
994 |
/** Initializes items. |
980 |
/** Initializes items. |
995 |
*/ |
981 |
*/ |
996 |
private Collection initItems(Storage t) { |
982 |
private Collection<Pair<T>> initItems(Storage<?> t) { |
997 |
// manipulation with the tree must be synchronized |
983 |
// manipulation with the tree must be synchronized |
998 |
Enumeration en = t.lookup(reference.template.getType()); |
984 |
Enumeration<Pair<T>> en = t.lookup(reference.template.getType()); |
999 |
|
985 |
|
1000 |
// InheritanceTree is comparator for AbstractLookup.Pairs |
986 |
// InheritanceTree is comparator for AbstractLookup.Pairs |
1001 |
TreeSet items = new TreeSet(ALPairComparator.DEFAULT); |
987 |
TreeSet<Pair<T>> items = new TreeSet<Pair<T>>(ALPairComparator.DEFAULT); |
1002 |
|
988 |
|
1003 |
while (en.hasMoreElements()) { |
989 |
while (en.hasMoreElements()) { |
1004 |
Pair i = (Pair) en.nextElement(); |
990 |
Pair<T> i = en.nextElement(); |
1005 |
|
991 |
|
1006 |
if (matches(reference.template, i, false)) { |
992 |
if (matches(reference.template, i, false)) { |
1007 |
items.add(i); |
993 |
items.add(i); |
Lines 1046-1052
Link Here
|
1046 |
|
1032 |
|
1047 |
/** abstract lookup we are connected to */ |
1033 |
/** abstract lookup we are connected to */ |
1048 |
private AbstractLookup al = null; |
1034 |
private AbstractLookup al = null; |
1049 |
private transient ArrayList earlyPairs; |
1035 |
private transient ArrayList<Pair> earlyPairs; |
1050 |
|
1036 |
|
1051 |
/** A lookup attaches to this object. |
1037 |
/** A lookup attaches to this object. |
1052 |
*/ |
1038 |
*/ |
Lines 1056-1065
Link Here
|
1056 |
|
1042 |
|
1057 |
if (earlyPairs != null) { |
1043 |
if (earlyPairs != null) { |
1058 |
// we must just add no override! |
1044 |
// we must just add no override! |
1059 |
Pair[] p = (Pair[]) earlyPairs.toArray(new Pair[earlyPairs.size()]); |
1045 |
for (Pair<?> p : earlyPairs) { |
1060 |
|
1046 |
addPair(p); |
1061 |
for (int i = 0; i < p.length; i++) { |
|
|
1062 |
addPair(p[i]); |
1063 |
} |
1047 |
} |
1064 |
} |
1048 |
} |
1065 |
|
1049 |
|
Lines 1074-1087
Link Here
|
1074 |
/** The method to add instance to the lookup with. |
1058 |
/** The method to add instance to the lookup with. |
1075 |
* @param pair class/instance pair |
1059 |
* @param pair class/instance pair |
1076 |
*/ |
1060 |
*/ |
1077 |
public final void addPair(Pair pair) { |
1061 |
public final void addPair(Pair<?> pair) { |
1078 |
AbstractLookup a = al; |
1062 |
AbstractLookup a = al; |
1079 |
|
1063 |
|
1080 |
if (a != null) { |
1064 |
if (a != null) { |
1081 |
a.addPair(pair); |
1065 |
a.addPair(pair); |
1082 |
} else { |
1066 |
} else { |
1083 |
if (earlyPairs == null) { |
1067 |
if (earlyPairs == null) { |
1084 |
earlyPairs = new ArrayList(3); |
1068 |
earlyPairs = new ArrayList<Pair>(3); |
1085 |
} |
1069 |
} |
1086 |
|
1070 |
|
1087 |
earlyPairs.add(pair); |
1071 |
earlyPairs.add(pair); |
Lines 1091-1104
Link Here
|
1091 |
/** Remove instance. |
1075 |
/** Remove instance. |
1092 |
* @param pair class/instance pair |
1076 |
* @param pair class/instance pair |
1093 |
*/ |
1077 |
*/ |
1094 |
public final void removePair(Pair pair) { |
1078 |
public final void removePair(Pair<?> pair) { |
1095 |
AbstractLookup a = al; |
1079 |
AbstractLookup a = al; |
1096 |
|
1080 |
|
1097 |
if (a != null) { |
1081 |
if (a != null) { |
1098 |
a.removePair(pair); |
1082 |
a.removePair(pair); |
1099 |
} else { |
1083 |
} else { |
1100 |
if (earlyPairs == null) { |
1084 |
if (earlyPairs == null) { |
1101 |
earlyPairs = new ArrayList(3); |
1085 |
earlyPairs = new ArrayList<Pair>(3); |
1102 |
} |
1086 |
} |
1103 |
|
1087 |
|
1104 |
earlyPairs.remove(pair); |
1088 |
earlyPairs.remove(pair); |
Lines 1108-1120
Link Here
|
1108 |
/** Changes all pairs in the lookup to new values. |
1092 |
/** Changes all pairs in the lookup to new values. |
1109 |
* @param c the collection of (Pair) objects |
1093 |
* @param c the collection of (Pair) objects |
1110 |
*/ |
1094 |
*/ |
1111 |
public final void setPairs(Collection c) { |
1095 |
public final void setPairs(Collection<? extends Pair> c) { |
1112 |
AbstractLookup a = al; |
1096 |
AbstractLookup a = al; |
1113 |
|
1097 |
|
1114 |
if (a != null) { |
1098 |
if (a != null) { |
1115 |
a.setPairs(c); |
1099 |
a.setPairs(c); |
1116 |
} else { |
1100 |
} else { |
1117 |
earlyPairs = new ArrayList(c); |
1101 |
earlyPairs = new ArrayList<Pair>(c); |
1118 |
} |
1102 |
} |
1119 |
} |
1103 |
} |
1120 |
} |
1104 |
} |
Lines 1134-1145
Link Here
|
1134 |
|
1118 |
|
1135 |
/** Reference to a result R |
1119 |
/** Reference to a result R |
1136 |
*/ |
1120 |
*/ |
1137 |
static final class ReferenceToResult extends WeakReference implements Runnable { |
1121 |
static final class ReferenceToResult<T> extends WeakReference<R<T>> implements Runnable { |
1138 |
/** next refernece in chain, modified only from AbstractLookup or this */ |
1122 |
/** next refernece in chain, modified only from AbstractLookup or this */ |
1139 |
private ReferenceToResult next; |
1123 |
private ReferenceToResult<?> next; |
1140 |
|
1124 |
|
1141 |
/** the template for the result */ |
1125 |
/** the template for the result */ |
1142 |
public final Template template; |
1126 |
public final Template<T> template; |
1143 |
|
1127 |
|
1144 |
/** the lookup we are attached to */ |
1128 |
/** the lookup we are attached to */ |
1145 |
public final AbstractLookup lookup; |
1129 |
public final AbstractLookup lookup; |
Lines 1150-1156
Link Here
|
1150 |
/** Creates a weak refernece to a new result R in context of lookup |
1134 |
/** Creates a weak refernece to a new result R in context of lookup |
1151 |
* for given template |
1135 |
* for given template |
1152 |
*/ |
1136 |
*/ |
1153 |
private ReferenceToResult(R result, AbstractLookup lookup, Template template) { |
1137 |
private ReferenceToResult(R<T> result, AbstractLookup lookup, Template<T> template) { |
1154 |
super(result, activeQueue()); |
1138 |
super(result, activeQueue()); |
1155 |
this.template = template; |
1139 |
this.template = template; |
1156 |
this.lookup = lookup; |
1140 |
this.lookup = lookup; |
Lines 1159-1166
Link Here
|
1159 |
|
1143 |
|
1160 |
/** Returns the result or null |
1144 |
/** Returns the result or null |
1161 |
*/ |
1145 |
*/ |
1162 |
R getResult() { |
1146 |
R<T> getResult() { |
1163 |
return (R) get(); |
1147 |
return get(); |
1164 |
} |
1148 |
} |
1165 |
|
1149 |
|
1166 |
/** Cleans the reference. Implements Runnable interface, do not call |
1150 |
/** Cleans the reference. Implements Runnable interface, do not call |
Lines 1173-1184
Link Here
|
1173 |
/** Clones the reference list to given Storage. |
1157 |
/** Clones the reference list to given Storage. |
1174 |
* @param storage storage to clone to |
1158 |
* @param storage storage to clone to |
1175 |
*/ |
1159 |
*/ |
1176 |
public void cloneList(AbstractLookup.Storage storage) { |
1160 |
public void cloneList(AbstractLookup.Storage<?> storage) { |
1177 |
ReferenceIterator it = new ReferenceIterator(this); |
1161 |
ReferenceIterator it = new ReferenceIterator(this); |
1178 |
|
1162 |
|
1179 |
while (it.next()) { |
1163 |
while (it.next()) { |
1180 |
ReferenceToResult current = it.current(); |
1164 |
ReferenceToResult<?> current = it.current(); |
1181 |
ReferenceToResult newRef = new ReferenceToResult(current.getResult(), current.lookup, current.template); |
1165 |
ReferenceToResult<?> newRef = current.cloneRef(); |
1182 |
newRef.next = storage.registerReferenceToResult(newRef); |
1166 |
newRef.next = storage.registerReferenceToResult(newRef); |
1183 |
newRef.caches = current.caches; |
1167 |
newRef.caches = current.caches; |
1184 |
|
1168 |
|
Lines 1187-1192
Link Here
|
1187 |
} |
1171 |
} |
1188 |
} |
1172 |
} |
1189 |
} |
1173 |
} |
|
|
1174 |
|
1175 |
private ReferenceToResult<T> cloneRef() { |
1176 |
return new ReferenceToResult<T>(getResult(), lookup, template); |
1177 |
} |
1190 |
} |
1178 |
} |
1191 |
// end of ReferenceToResult |
1179 |
// end of ReferenceToResult |
1192 |
|
1180 |
|
Lines 1200-1221
Link Here
|
1200 |
* this.ref = it.first (); // remember the first one |
1188 |
* this.ref = it.first (); // remember the first one |
1201 |
*/ |
1189 |
*/ |
1202 |
static final class ReferenceIterator extends Object { |
1190 |
static final class ReferenceIterator extends Object { |
1203 |
private ReferenceToResult first; |
1191 |
private ReferenceToResult<?> first; |
1204 |
private ReferenceToResult current; |
1192 |
private ReferenceToResult<?> current; |
1205 |
|
1193 |
|
1206 |
/** hard reference to current result, so it is not GCed meanwhile */ |
1194 |
/** hard reference to current result, so it is not GCed meanwhile */ |
1207 |
private R currentResult; |
1195 |
private R<?> currentResult; |
1208 |
|
1196 |
|
1209 |
/** Initializes the iterator with first reference. |
1197 |
/** Initializes the iterator with first reference. |
1210 |
*/ |
1198 |
*/ |
1211 |
public ReferenceIterator(ReferenceToResult first) { |
1199 |
public ReferenceIterator(ReferenceToResult<?> first) { |
1212 |
this.first = first; |
1200 |
this.first = first; |
1213 |
} |
1201 |
} |
1214 |
|
1202 |
|
1215 |
/** Moves the current to next possition */ |
1203 |
/** Moves the current to next possition */ |
1216 |
public boolean next() { |
1204 |
public boolean next() { |
1217 |
ReferenceToResult prev; |
1205 |
ReferenceToResult<?> prev; |
1218 |
ReferenceToResult ref; |
1206 |
ReferenceToResult<?> ref; |
1219 |
|
1207 |
|
1220 |
if (current == null) { |
1208 |
if (current == null) { |
1221 |
ref = first; |
1209 |
ref = first; |
Lines 1226-1232
Link Here
|
1226 |
} |
1214 |
} |
1227 |
|
1215 |
|
1228 |
while (ref != null) { |
1216 |
while (ref != null) { |
1229 |
R result = (R) ref.get(); |
1217 |
R<?> result = ref.get(); |
1230 |
|
1218 |
|
1231 |
if (result == null) { |
1219 |
if (result == null) { |
1232 |
if (prev == null) { |
1220 |
if (prev == null) { |
Lines 1256-1268
Link Here
|
1256 |
|
1244 |
|
1257 |
/** Access to current reference. |
1245 |
/** Access to current reference. |
1258 |
*/ |
1246 |
*/ |
1259 |
public ReferenceToResult current() { |
1247 |
public ReferenceToResult<?> current() { |
1260 |
return current; |
1248 |
return current; |
1261 |
} |
1249 |
} |
1262 |
|
1250 |
|
1263 |
/** Access to reference that is supposed to be the first one. |
1251 |
/** Access to reference that is supposed to be the first one. |
1264 |
*/ |
1252 |
*/ |
1265 |
public ReferenceToResult first() { |
1253 |
public ReferenceToResult<?> first() { |
1266 |
return first; |
1254 |
return first; |
1267 |
} |
1255 |
} |
1268 |
} |
1256 |
} |
Lines 1273-1279
Link Here
|
1273 |
*/ |
1261 |
*/ |
1274 |
static final class ISE extends IllegalStateException { |
1262 |
static final class ISE extends IllegalStateException { |
1275 |
/** list of jobs to execute. */ |
1263 |
/** list of jobs to execute. */ |
1276 |
private java.util.List jobs; |
1264 |
private java.util.List<Job> jobs; |
1277 |
|
1265 |
|
1278 |
/** @param msg message |
1266 |
/** @param msg message |
1279 |
*/ |
1267 |
*/ |
Lines 1286-1292
Link Here
|
1286 |
*/ |
1274 |
*/ |
1287 |
public void registerJob(Job job) { |
1275 |
public void registerJob(Job job) { |
1288 |
if (jobs == null) { |
1276 |
if (jobs == null) { |
1289 |
jobs = new java.util.ArrayList(); |
1277 |
jobs = new java.util.ArrayList<Job>(); |
1290 |
} |
1278 |
} |
1291 |
|
1279 |
|
1292 |
jobs.add(job); |
1280 |
jobs.add(job); |
Lines 1300-1315
Link Here
|
1300 |
throw this; |
1288 |
throw this; |
1301 |
} |
1289 |
} |
1302 |
|
1290 |
|
1303 |
for (java.util.Iterator it = jobs.iterator(); it.hasNext();) { |
1291 |
for (Job j : jobs) { |
1304 |
Job j = (Job) it.next(); |
|
|
1305 |
j.before(); |
1292 |
j.before(); |
1306 |
} |
1293 |
} |
1307 |
|
1294 |
|
1308 |
AbstractLookup.Storage s = lookup.enterStorage(); |
1295 |
AbstractLookup.Storage s = lookup.enterStorage(); |
1309 |
|
1296 |
|
1310 |
try { |
1297 |
try { |
1311 |
for (java.util.Iterator it = jobs.iterator(); it.hasNext();) { |
1298 |
for (Job j : jobs) { |
1312 |
Job j = (Job) it.next(); |
|
|
1313 |
j.inside(); |
1299 |
j.inside(); |
1314 |
} |
1300 |
} |
1315 |
} finally { |
1301 |
} finally { |