1 19 20 package org.netbeans.modules.retouche.hints; 21 import java.lang.ref.Reference ; 22 import java.lang.ref.WeakReference ; 23 import java.util.ArrayList ; 24 import java.util.Collections ; 25 import java.util.HashMap ; 26 import java.util.List ; 27 import java.util.Map ; 28 import java.util.WeakHashMap ; 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 46 public class LazyHintComputationFactory extends EditorAwareSourceTaskFactory { 47 48 private static Map <FileObject, List <Reference <CreatorBasedLazyFixList>>> file2Creators = new WeakHashMap <FileObject, List <Reference <CreatorBasedLazyFixList>>>(); 49 50 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 <Reference <CreatorBasedLazyFixList>> references = file2Creators.get(file); 70 71 if (references == null) { 72 file2Creators.put(file, references = new ArrayList <Reference <CreatorBasedLazyFixList>>()); 73 } 74 75 references.add(new WeakReference (list)); 76 } 77 78 rescheduleImpl(file); 79 } 80 81 public static synchronized List <CreatorBasedLazyFixList> getAndClearToCompute(FileObject file) { 82 List <Reference <CreatorBasedLazyFixList>> references = file2Creators.get(file); 83 84 if (references == null) { 85 return Collections.emptyList(); 86 } 87 88 List <CreatorBasedLazyFixList> result = new ArrayList <CreatorBasedLazyFixList>(); 89 90 for (Reference <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 |