KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > netbeans > modules > retouche > hints > LazyHintComputationFactory


1 /*
2  * The contents of this file are subject to the terms of the Common Development
3  * and Distribution License (the License). You may not use this file except in
4  * compliance with the License.
5  *
6  * You can obtain a copy of the License at http://www.netbeans.org/cddl.html
7  * or http://www.netbeans.org/cddl.txt.
8  *
9  * When distributing Covered Code, include this CDDL Header Notice in each file
10  * and include the License file at http://www.netbeans.org/cddl.txt.
11  * If applicable, add the following below the CDDL Header, with the fields
12  * enclosed by brackets [] replaced by your own identifying information:
13  * "Portions Copyrighted [year] [name of copyright owner]"
14  *
15  * The Original Software is NetBeans. The Initial Developer of the Original
16  * Software is Sun Microsystems, Inc. Portions Copyright 1997-2006 Sun
17  * Microsystems, Inc. All Rights Reserved.
18  */

19
20 package org.netbeans.modules.retouche.hints;
21 import java.lang.ref.Reference JavaDoc;
22 import java.lang.ref.WeakReference JavaDoc;
23 import java.util.ArrayList JavaDoc;
24 import java.util.Collections JavaDoc;
25 import java.util.HashMap JavaDoc;
26 import java.util.List JavaDoc;
27 import java.util.Map JavaDoc;
28 import java.util.WeakHashMap JavaDoc;
29 import org.netbeans.api.gsf.CancellableTask;
30 import org.netbeans.api.retouche.source.CompilationInfo;
31 import org.netbeans.api.retouche.source.Phase;
32 import org.netbeans.api.retouche.source.Source.Priority;
33 import org.netbeans.api.retouche.source.support.EditorAwareSourceTaskFactory;
34 import org.openide.filesystems.FileObject;
35 import org.openide.util.Lookup;
36
37 /**
38  * This file is originally from Retouche, the Java Support
39  * infrastructure in NetBeans. I have modified the file as little
40  * as possible to make merging Retouche fixes back as simple as
41  * possible.
42  *
43  *
44  * @author Jan Lahoda
45  */

46 public class LazyHintComputationFactory extends EditorAwareSourceTaskFactory {
47     
48     private static Map JavaDoc<FileObject, List JavaDoc<Reference JavaDoc<CreatorBasedLazyFixList>>> file2Creators = new WeakHashMap JavaDoc<FileObject, List JavaDoc<Reference JavaDoc<CreatorBasedLazyFixList>>>();
49     
50     /** Creates a new instance of LazyHintComputationFactory */
51     public LazyHintComputationFactory() {
52         super(Phase.RESOLVED, Priority.LOW);
53     }
54
55     public CancellableTask<CompilationInfo> createTask(FileObject file) {
56         return new LazyHintComputation(file);
57     }
58  
59     private static void rescheduleImpl(FileObject file) {
60         LazyHintComputationFactory f = Lookup.getDefault().lookup(LazyHintComputationFactory.class);
61         
62         if (f != null) {
63             f.reschedule(file);
64         }
65     }
66     
67     public static void addToCompute(FileObject file, CreatorBasedLazyFixList list) {
68         synchronized (LazyHintComputationFactory.class) {
69             List JavaDoc<Reference JavaDoc<CreatorBasedLazyFixList>> references = file2Creators.get(file);
70             
71             if (references == null) {
72                 file2Creators.put(file, references = new ArrayList JavaDoc<Reference JavaDoc<CreatorBasedLazyFixList>>());
73             }
74             
75             references.add(new WeakReference JavaDoc(list));
76         }
77         
78         rescheduleImpl(file);
79     }
80     
81     public static synchronized List JavaDoc<CreatorBasedLazyFixList> getAndClearToCompute(FileObject file) {
82         List JavaDoc<Reference JavaDoc<CreatorBasedLazyFixList>> references = file2Creators.get(file);
83         
84         if (references == null) {
85             return Collections.emptyList();
86         }
87         
88         List JavaDoc<CreatorBasedLazyFixList> result = new ArrayList JavaDoc<CreatorBasedLazyFixList>();
89         
90         for (Reference JavaDoc<CreatorBasedLazyFixList> r : references) {
91             CreatorBasedLazyFixList c = r.get();
92             
93             if (c != null) {
94                 result.add(c);
95             }
96         }
97         
98         references.clear();
99         
100         return result;
101     }
102 }
103
Popular Tags