Skip to main content
aboutsummaryrefslogtreecommitdiffstats
blob: 9ac747761c4609466d314520e3ec412c9077a1ba (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
/*******************************************************************************
 * Copyright (c) 2005, 2006 QNX Software Systems and others.
 * All rights reserved. This program and the accompanying materials
 * are made available under the terms of the Eclipse Public License v1.0
 * which accompanies this distribution, and is available at
 * http://www.eclipse.org/legal/epl-v10.html
 *
 * Contributors:
 *     QNX Software Systems - initial API and implementation
 *******************************************************************************/
/*
 * Created on Aug 30, 2004
 *
 * TODO To change the template for this generated file go to
 * Window - Preferences - Java - Code Style - Code Templates
 */
package org.eclipse.cdt.internal.ui.search;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.HashSet;
import java.util.Iterator;
import java.util.Set;

import org.eclipse.ui.IWorkingSet;
import org.eclipse.ui.PlatformUI;



/**
 * @author bgheorgh
 *
 * TODO To change the template for this generated type comment go to
 * Window - Preferences - Java - Code Style - Code Templates
 */
public class LRUWorkingSets {
	
	ArrayList workingSetsCache = null;
	int size=0;
	
	public LRUWorkingSets(int size){
		workingSetsCache = new ArrayList(size);
		this.size = size;
	}
	
	public void add(IWorkingSet[] workingSet){
		cleanUpCache();
		//See if this working set has been previously added to the 
		IWorkingSet[] existingWorkingSets= find(workingSetsCache, workingSet);
		if (existingWorkingSets != null)
			workingSetsCache.remove(existingWorkingSets);
		else if (workingSetsCache.size() == size)
			workingSetsCache.remove(size - 1);
		workingSetsCache.add(0, workingSet);
	}
	
	private IWorkingSet[] find(ArrayList list, IWorkingSet[] workingSet) {
		Set workingSetList= new HashSet(Arrays.asList(workingSet));
		Iterator iter= list.iterator();
		while (iter.hasNext()) {
			IWorkingSet[] lruWorkingSets= (IWorkingSet[])iter.next();
			Set lruWorkingSetList= new HashSet(Arrays.asList(lruWorkingSets));
			if (lruWorkingSetList.equals(workingSetList))
				return lruWorkingSets;
		}
		return null;
	}

	private void cleanUpCache(){
     //Remove any previously deleted entries
	 Iterator iter = workingSetsCache.iterator();
	 while (iter.hasNext()){
	 	IWorkingSet[] workingSet = (IWorkingSet []) iter.next();
	 	for (int i= 0; i < workingSet.length; i++) {
			if (PlatformUI.getWorkbench().getWorkingSetManager().getWorkingSet(workingSet[i].getName()) == null) {
				workingSetsCache.remove(workingSet);
				break;
			}
		}
	 }
	}

	public Iterator iterator() {
		return workingSetsCache.iterator(); 
	}
}

Back to the top