01: package org.columba.core.search;
02:
03: import java.util.Collections;
04: import java.util.Hashtable;
05: import java.util.Iterator;
06: import java.util.LinkedHashMap;
07: import java.util.Map;
08:
09: import org.columba.core.search.api.ISearchCriteria;
10: import org.columba.core.search.api.ISearchProvider;
11:
12: public class SearchHistoryList {
13: Map<String, HistoryItem> cache;
14:
15: final int MAX_ENTRIES = 5;
16:
17: private static SearchHistoryList instance;
18:
19: public static SearchHistoryList getInstance() {
20: if (instance == null)
21: instance = new SearchHistoryList();
22:
23: return instance;
24: }
25:
26: private SearchHistoryList() {
27: super ();
28: cache = new LinkedHashMap<String, HistoryItem>(MAX_ENTRIES + 1,
29: .75F, true) {
30: // This method is called just after a new entry has been added
31: public boolean removeEldestEntry(Map.Entry eldest) {
32: return size() > MAX_ENTRIES;
33: }
34: };
35:
36: // ensure map can be used by multiple threads
37: cache = Collections.synchronizedMap(cache);
38: }
39:
40: public void add(String searchTerm, ISearchProvider provider,
41: ISearchCriteria criteria) {
42: String key = searchTerm + "/" + provider.getTechnicalName()
43: + "/" + criteria.getTechnicalName();
44: cache.put(key, new HistoryItem(searchTerm, provider));
45: }
46:
47: /**
48: * Return map of history items.
49: *
50: * @return map key is the search term. Map value is the searchprovider.
51: */
52: public Map<String, ISearchProvider> getHistoryMap() {
53: Map<String, ISearchProvider> map = new Hashtable<String, ISearchProvider>();
54: Iterator<HistoryItem> it = cache.values().iterator();
55: while (it.hasNext()) {
56: HistoryItem item = it.next();
57: map.put(item.getSearchTerm(), item.getProvider());
58: }
59: return map;
60: }
61:
62: public class HistoryItem {
63: String searchTerm;
64:
65: ISearchProvider provider;
66: ISearchCriteria criteria;
67:
68: HistoryItem(String searchTerm, ISearchProvider provider) {
69: this .searchTerm = searchTerm;
70: this .provider = provider;
71: }
72:
73: public ISearchProvider getProvider() {
74: return provider;
75: }
76:
77: public String getSearchTerm() {
78: return searchTerm;
79: }
80:
81: public ISearchCriteria getCriteria() {
82: return criteria;
83: }
84: }
85: }
|