KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > netbeans > modules > java > 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.java.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.java.source.CancellableTask;
30 import org.netbeans.api.java.source.CompilationInfo;
31 import org.netbeans.api.java.source.JavaSource;
32 import org.netbeans.api.java.source.JavaSource.Phase;
33 import org.netbeans.api.java.source.JavaSource.Priority;
34 import org.netbeans.api.java.source.support.EditorAwareJavaSourceTaskFactory;
35 import org.openide.filesystems.FileObject;
36 import org.openide.util.Lookup;
37
38 /**
39  *
40  * @author Jan Lahoda
41  */

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