46f6df2c6410422d1361681bda505b61eede26cf
[jalview.git] / src / jalview / io / cache / AppCache.java
1 package jalview.io.cache;
2
3
4 import jalview.bin.Cache;
5
6 import java.util.Arrays;
7 import java.util.Hashtable;
8 import java.util.LinkedHashSet;
9 import java.util.List;
10 import java.util.Set;
11
12 public class AppCache implements AppCacheI
13 {
14   private static AppCache instance = null;
15
16   private Hashtable<String, LinkedHashSet<String>> cacheItems;
17
18   private static final String CACHE_DELIMITER = ";";
19
20   private AppCache()
21   {
22     cacheItems = new Hashtable<String, LinkedHashSet<String>>();
23   }
24
25   @Override
26   public LinkedHashSet<String> getAllCachedItemsFor(Cacheable cacheable)
27   {
28     String cacheKey = cacheable.getCacheKey();
29     LinkedHashSet<String> foundCache = cacheItems.get(cacheKey);
30     if (foundCache == null)
31     {
32       foundCache = new LinkedHashSet<String>();
33       cacheItems.put(cacheKey, foundCache);
34     }
35     return foundCache;
36   }
37
38
39   public static AppCache getInstance()
40   {
41     if (instance == null)
42     {
43       instance = new AppCache();
44     }
45     return instance;
46   }
47
48   @Override
49   public void updateCache(Cacheable cacheable)
50   {
51     CacheBoxI<String> cacheComboBox = cacheable.getCacheComboBox();
52     String cacheKey = cacheable.getCacheKey();
53     cacheComboBox.looseFocus();
54     String userInput = cacheComboBox.getUserInput();
55
56     if (userInput != null && !userInput.isEmpty())
57     {
58       LinkedHashSet<String> foundCache = getAllCachedItemsFor(cacheable);
59       foundCache.add(userInput);
60       cacheItems.put(cacheKey, foundCache);
61     }
62
63     String lastSearch = userInput;
64     if (cacheComboBox.getItemCount() > 0)
65     {
66       cacheComboBox.removeAllItems();
67     }
68
69     Set<String> cacheItems = getAllCachedItemsFor(cacheable);
70     if (cacheItems != null && !cacheItems.isEmpty())
71     {
72       for (String cacheItem : cacheItems)
73       {
74         cacheComboBox.addItem(cacheItem);
75       }
76     }
77
78     if (!lastSearch.isEmpty())
79     {
80       cacheComboBox.setSelectedItem(lastSearch);
81       cacheComboBox.requestFocus();
82     }
83     else
84     {
85       cacheable.initCache();
86       cacheComboBox.addItem("");
87       cacheComboBox.setSelectedItem("");
88     }
89   }
90
91   @Override
92   public void initCache(Cacheable cacheable)
93   {
94     String separatedStr = Cache.getProperty(cacheable.getCacheKey());
95     if (separatedStr == null || separatedStr.isEmpty())
96     {
97       return;
98     }
99
100     List<String> persistedCacheItems = Arrays.asList(separatedStr.split(CACHE_DELIMITER));
101     String cacheKey = cacheable.getCacheKey();
102
103     LinkedHashSet<String> foundCacheItems = cacheItems.get(cacheKey);
104     if (foundCacheItems == null)
105     {
106       foundCacheItems = new LinkedHashSet<String>();
107     }
108
109     for (String cacheItem : persistedCacheItems)
110     {
111       foundCacheItems.add(cacheItem);
112     }
113     cacheItems.put(cacheKey, foundCacheItems);
114     updateCache(cacheable);
115   }
116
117   @Override
118   public void persistCache(Cacheable cacheable)
119   {
120     String cacheKey = cacheable.getCacheKey();
121     LinkedHashSet<String> foundCacheItems = getAllCachedItemsFor(cacheable);
122     StringBuffer delimitedCacheBuf = new StringBuffer();
123     for (String cacheItem : foundCacheItems)
124     {
125       delimitedCacheBuf.append(CACHE_DELIMITER).append(cacheItem);
126     }
127     if (delimitedCacheBuf.length() > 0)
128     {
129       delimitedCacheBuf.deleteCharAt(0);
130     }
131     String delimitedCacheString = delimitedCacheBuf.toString();
132
133     Cache.setProperty(cacheKey, delimitedCacheString);
134   }
135 }