1 19 20 package edu.umd.cs.findbugs.gui2; 21 22 import java.lang.management.ManagementFactory ; 23 import java.lang.management.ThreadMXBean ; 24 import java.util.ArrayList ; 25 import java.util.Collection ; 26 import java.util.Collections ; 27 import java.util.Comparator ; 28 import java.util.HashMap ; 29 import java.util.HashSet ; 30 import java.util.Iterator ; 31 import java.util.List ; 32 33 import edu.umd.cs.findbugs.BugInstance; 34 import edu.umd.cs.findbugs.BugPattern; 35 import edu.umd.cs.findbugs.annotations.NonNull; 36 import edu.umd.cs.findbugs.filter.Matcher; 37 import edu.umd.cs.findbugs.gui2.BugAspects.StringPair; 38 import edu.umd.cs.findbugs.gui2.SortableStringComparator; 39 40 50 public class BugSet implements Iterable <BugLeafNode>{ 51 52 private HashList<BugLeafNode> mainList; 53 private HashMap <StringPair,BugSet> doneMap; 54 private HashMap <StringPair,Boolean > doneContainsMap; 55 private HashMap <Sortables,HashList<String >> sortablesToStrings; 56 57 58 private static BugSet mainBugSet=null; 59 60 66 public static BugSet getMainBugSet() 67 { 68 return mainBugSet; 69 } 70 71 76 public String [] getAll(Sortables s) 77 { 78 HashList<String > list=sortablesToStrings.get(s); 79 Collections.sort(list, new SortableStringComparator(s)); 80 return list.toArray(new String [list.size()]); 81 } 82 83 88 BugSet(ArrayList <BugLeafNode> filteredSet) 89 { 90 this.mainList=new HashList<BugLeafNode>((ArrayList <BugLeafNode>)filteredSet.clone()); 91 doneMap=new HashMap <StringPair,BugSet>(); 92 doneContainsMap=new HashMap <StringPair,Boolean >(); 93 cacheSortables(); 94 } 95 96 100 static void setAsRootAndCache(BugSet bs) 101 { 102 mainBugSet=bs; 103 bs.sortList(); 104 bs.cacheSortables(); 105 } 106 107 111 void cacheSortables() 112 { 113 sortablesToStrings=new HashMap <Sortables,HashList<String >>(); 114 for (Sortables key: Sortables.values()) 115 if (key != Sortables.DIVIDER) 116 { 117 HashList<String > list=new HashList<String >(); 118 sortablesToStrings.put(key,list); 119 } 120 121 ArrayList <BugLeafNode> bugNodes=new ArrayList <BugLeafNode>(); 122 for(BugLeafNode p:mainList) 123 { 124 if (ProjectSettings.getInstance().getAllMatchers().match(p.getBug())) 125 bugNodes.add(p); 126 } 127 128 for (BugLeafNode b:bugNodes) 129 { 130 BugInstance bug=b.getBug(); 131 BugPattern bugP=bug.getBugPattern(); 132 133 if (bugP==null) 134 { 135 assert false; 136 if (MainFrame.DEBUG) System.err.println("A bug pattern was not found for "+bug.getMessage()); 137 continue; 138 } 139 140 for (Sortables key: Sortables.values()) 141 if (key != Sortables.DIVIDER) 142 { 143 HashList<String > list=sortablesToStrings.get(key); 144 145 String value=key.getFrom(bug); 146 if (!list.contains(value)) 147 list.add(value); 148 sortablesToStrings.put(key,list); 149 } 150 } 151 152 for (Sortables key: Sortables.values()) 153 if (key != Sortables.DIVIDER) 154 Collections.sort(sortablesToStrings.get(key)); 155 } 156 157 158 static int countFilteredBugs() 159 { 160 CompoundMatcher cm=ProjectSettings.getInstance().getAllMatchers(); 161 int result = 0; 162 for (BugLeafNode bug : mainBugSet.mainList) 163 if (!cm.match(bug.getBug())) 164 result++; 165 166 return result; 167 } 168 169 173 BugSet(BugSet copySet) 175 { 176 this.mainList=copySet.mainList; 177 doneMap=new HashMap <StringPair,BugSet>(); 178 doneContainsMap=new HashMap <StringPair,Boolean >(); 179 cacheSortables(); 180 } 181 182 183 195 BugSet query(StringPair keyValuePair) 196 { 197 if (doneMap.containsKey(keyValuePair)) 198 return doneMap.get(keyValuePair); 199 ArrayList <BugLeafNode> bugs=new ArrayList <BugLeafNode>(); 200 201 for(BugLeafNode b:mainList) 202 { 203 if (b.matches(keyValuePair)) 204 bugs.add(b); 205 } 206 207 BugSet temp=new BugSet(bugs); 208 doneMap.put(keyValuePair,temp); 209 return temp; 210 } 211 212 213 void sortList() 214 { 215 final List <Sortables> order = MainFrame.getInstance().getSorter().getOrderAfterDivider(); 218 221 Collections.sort(mainList, new Comparator <BugLeafNode>(){ 222 public int compare(BugLeafNode one, BugLeafNode two) 223 { 224 for (Sortables i : order) 225 { 226 int result = i.getBugLeafNodeComparator().compare(one, two); 227 if (result != 0) 228 return result; 229 } 230 return 0; 232 } 233 }); 234 } 235 236 243 public boolean contains(StringPair keyValuePair) 244 { 245 if (doneContainsMap.containsKey(keyValuePair)) 246 return doneContainsMap.get(keyValuePair); 247 248 for(BugLeafNode p:filterNoCache().mainList) 249 { 250 if (p.matches(keyValuePair)) 251 { 252 doneContainsMap.put(keyValuePair,true); 253 return true; 254 } 255 } 256 doneContainsMap.put(keyValuePair,false); 257 return false; 258 } 259 260 261 264 public BugSet query(BugAspects a) 265 { 266 BugSet result=this; 267 for (StringPair sp:a) 268 { 269 result=result.query(sp); 270 } 271 272 return result; 273 } 274 275 public int sizeUnfiltered() 276 { 277 return mainList.size(); 278 } 279 284 public int indexOfUnfiltered(BugLeafNode p) 285 { 286 return mainList.indexOf(p); 287 } 288 289 public BugLeafNode getUnfiltered(int index) 290 { 291 return mainList.get(index); 292 } 293 294 public Iterator <BugLeafNode> iterator() 295 { 296 return mainList.iterator(); 297 } 298 299 301 BugSet(ArrayList <BugLeafNode> filteredSet, boolean cacheSortables) 302 { 303 this.mainList=new HashList<BugLeafNode>((ArrayList <BugLeafNode>)filteredSet.clone()); 304 doneMap=new HashMap <StringPair,BugSet>(); 305 doneContainsMap=new HashMap <StringPair,Boolean >(); 306 if (cacheSortables) 307 cacheSortables(); 308 } 309 310 public BugSet filterNoCache() 311 { 312 313 Matcher m=ProjectSettings.getInstance().getAllMatchers(); 314 ArrayList <BugLeafNode> people=new ArrayList <BugLeafNode>(); 315 for(BugLeafNode p:mainList) 316 { 317 if (m.match(p.getBug())) 318 people.add(p); 319 } 320 return new BugSet(people,false); 321 } 322 323 public BugSet getBugsMatchingFilter(Matcher m) 324 { 325 ArrayList <BugLeafNode> people=new ArrayList <BugLeafNode>(); 326 for(BugLeafNode p:mainList) 327 { 328 if(!(m.match(p.getBug()))) 329 people.add(p); 330 } 331 return new BugSet(people,false); 332 } 333 334 public int size() 335 { 336 return filterNoCache().sizeUnfiltered(); 337 } 338 339 public int indexOf(BugLeafNode p) 340 { 341 return filterNoCache().indexOfUnfiltered(p); 342 } 343 344 public BugLeafNode get(int index) 345 { 346 return filterNoCache().getUnfiltered(index); 347 } 348 } | Popular Tags |