1 11 package org.eclipse.jdt.internal.ui.search; 12 13 import java.util.ArrayList ; 14 import java.util.Arrays ; 15 import java.util.Collections ; 16 import java.util.HashSet ; 17 import java.util.Iterator ; 18 import java.util.Set ; 19 import org.eclipse.ui.IWorkingSet; 20 import org.eclipse.ui.PlatformUI; 21 22 public class LRUWorkingSetsList { 23 24 private final ArrayList fLRUList; 25 private final int fSize; 26 private final WorkingSetsComparator fComparator= new WorkingSetsComparator(); 27 28 public LRUWorkingSetsList(int size) { 29 fSize= size; 30 fLRUList= new ArrayList (size); 31 } 32 33 public void add(IWorkingSet[] workingSets) { 34 removeDeletedWorkingSets(); 35 IWorkingSet[] existingWorkingSets= find(fLRUList, workingSets); 36 if (existingWorkingSets != null) 37 fLRUList.remove(existingWorkingSets); 38 else if (fLRUList.size() == fSize) 39 fLRUList.remove(fSize - 1); 40 fLRUList.add(0, workingSets); 41 42 } 43 44 public Iterator iterator() { 45 removeDeletedWorkingSets(); 46 return fLRUList.iterator(); 47 } 48 49 public Iterator sortedIterator() { 50 removeDeletedWorkingSets(); 51 ArrayList sortedList= new ArrayList (fLRUList); 52 Collections.sort(sortedList, fComparator); 53 return sortedList.iterator(); 54 } 55 56 private void removeDeletedWorkingSets() { 57 Iterator iter= new ArrayList (fLRUList).iterator(); 58 while (iter.hasNext()) { 59 IWorkingSet[] workingSets= (IWorkingSet[])iter.next(); 60 for (int i= 0; i < workingSets.length; i++) { 61 if (PlatformUI.getWorkbench().getWorkingSetManager().getWorkingSet(workingSets[i].getName()) == null) { 62 fLRUList.remove(workingSets); 63 break; 64 } 65 } 66 } 67 } 68 69 private IWorkingSet[] find(ArrayList list, IWorkingSet[] workingSets) { 70 Set workingSetList= new HashSet (Arrays.asList(workingSets)); 71 Iterator iter= list.iterator(); 72 while (iter.hasNext()) { 73 IWorkingSet[] lruWorkingSets= (IWorkingSet[])iter.next(); 74 Set lruWorkingSetList= new HashSet (Arrays.asList(lruWorkingSets)); 75 if (lruWorkingSetList.equals(workingSetList)) 76 return lruWorkingSets; 77 } 78 return null; 79 } 80 } 81 | Popular Tags |