KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > eclipse > jdt > internal > ui > search > LRUWorkingSetsList


1 /*******************************************************************************
2  * Copyright (c) 2000, 2005 IBM Corporation and others.
3  * All rights reserved. This program and the accompanying materials
4  * are made available under the terms of the Eclipse Public License v1.0
5  * which accompanies this distribution, and is available at
6  * http://www.eclipse.org/legal/epl-v10.html
7  *
8  * Contributors:
9  * IBM Corporation - initial API and implementation
10  *******************************************************************************/

11 package org.eclipse.jdt.internal.ui.search;
12
13 import java.util.ArrayList JavaDoc;
14 import java.util.Arrays JavaDoc;
15 import java.util.Collections JavaDoc;
16 import java.util.HashSet JavaDoc;
17 import java.util.Iterator JavaDoc;
18 import java.util.Set JavaDoc;
19 import org.eclipse.ui.IWorkingSet;
20 import org.eclipse.ui.PlatformUI;
21
22 public class LRUWorkingSetsList {
23
24     private final ArrayList JavaDoc 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 JavaDoc(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 JavaDoc iterator() {
45         removeDeletedWorkingSets();
46         return fLRUList.iterator();
47     }
48
49     public Iterator JavaDoc sortedIterator() {
50         removeDeletedWorkingSets();
51         ArrayList JavaDoc sortedList= new ArrayList JavaDoc(fLRUList);
52         Collections.sort(sortedList, fComparator);
53         return sortedList.iterator();
54     }
55     
56     private void removeDeletedWorkingSets() {
57         Iterator JavaDoc iter= new ArrayList JavaDoc(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 JavaDoc list, IWorkingSet[] workingSets) {
70         Set JavaDoc workingSetList= new HashSet JavaDoc(Arrays.asList(workingSets));
71         Iterator JavaDoc iter= list.iterator();
72         while (iter.hasNext()) {
73             IWorkingSet[] lruWorkingSets= (IWorkingSet[])iter.next();
74             Set JavaDoc lruWorkingSetList= new HashSet JavaDoc(Arrays.asList(lruWorkingSets));
75             if (lruWorkingSetList.equals(workingSetList))
76                 return lruWorkingSets;
77         }
78         return null;
79     }
80 }
81
Popular Tags