KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > columba > core > search > SearchHistoryList


1 package org.columba.core.search;
2
3 import java.util.Collections JavaDoc;
4 import java.util.Hashtable JavaDoc;
5 import java.util.Iterator JavaDoc;
6 import java.util.LinkedHashMap JavaDoc;
7 import java.util.Map JavaDoc;
8
9 import org.columba.core.search.api.ISearchCriteria;
10 import org.columba.core.search.api.ISearchProvider;
11
12 public class SearchHistoryList {
13     Map JavaDoc<String JavaDoc, 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 JavaDoc<String JavaDoc, HistoryItem>(MAX_ENTRIES + 1, .75F,
29                 true) {
30             // This method is called just after a new entry has been added
31
public boolean removeEldestEntry(Map.Entry JavaDoc 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 JavaDoc searchTerm, ISearchProvider provider, ISearchCriteria criteria) {
41         String JavaDoc key = searchTerm + "/" + provider.getTechnicalName() + "/" + criteria.getTechnicalName();
42         cache.put(key, new HistoryItem(searchTerm, provider));
43     }
44
45     /**
46      * Return map of history items.
47      *
48      * @return map key is the search term. Map value is the searchprovider.
49      */

50     public Map JavaDoc<String JavaDoc, ISearchProvider> getHistoryMap() {
51         Map JavaDoc<String JavaDoc, ISearchProvider> map = new Hashtable JavaDoc<String JavaDoc, ISearchProvider>();
52         Iterator JavaDoc<HistoryItem> it = cache.values().iterator();
53         while (it.hasNext()) {
54             HistoryItem item = it.next();
55             map.put(item.getSearchTerm(), item.getProvider());
56         }
57         return map;
58     }
59
60     public class HistoryItem {
61         String JavaDoc searchTerm;
62
63         ISearchProvider provider;
64         ISearchCriteria criteria;
65         
66         HistoryItem(String JavaDoc searchTerm, ISearchProvider provider) {
67             this.searchTerm = searchTerm;
68             this.provider = provider;
69         }
70
71         public ISearchProvider getProvider() {
72             return provider;
73         }
74
75         public String JavaDoc getSearchTerm() {
76             return searchTerm;
77         }
78
79         public ISearchCriteria getCriteria() {
80             return criteria;
81         }
82     }
83 }
84
Popular Tags