1 package jalview.io.cache;
4 import jalview.bin.Cache;
6 import java.util.Arrays;
7 import java.util.Hashtable;
8 import java.util.LinkedHashSet;
12 public class AppCache implements AppCacheI
14 private static AppCache instance = null;
16 private Hashtable<String, LinkedHashSet<String>> cacheItems;
18 private static final String CACHE_DELIMITER = ";";
22 cacheItems = new Hashtable<String, LinkedHashSet<String>>();
26 public LinkedHashSet<String> getAllCachedItemsFor(Cacheable cacheable)
28 String cacheKey = cacheable.getCacheKey();
29 LinkedHashSet<String> foundCache = cacheItems.get(cacheKey);
30 if (foundCache == null)
32 foundCache = new LinkedHashSet<String>();
33 cacheItems.put(cacheKey, foundCache);
39 public static AppCache getInstance()
43 instance = new AppCache();
49 public void updateCache(Cacheable cacheable)
51 CacheBoxI<String> cacheComboBox = cacheable.getCacheComboBox();
52 String cacheKey = cacheable.getCacheKey();
53 cacheComboBox.looseFocus();
54 String userInput = cacheComboBox.getUserInput();
56 if (userInput != null && !userInput.isEmpty())
58 LinkedHashSet<String> foundCache = getAllCachedItemsFor(cacheable);
59 foundCache.add(userInput);
60 cacheItems.put(cacheKey, foundCache);
63 String lastSearch = userInput;
64 if (cacheComboBox.getItemCount() > 0)
66 cacheComboBox.removeAllItems();
69 Set<String> cacheItems = getAllCachedItemsFor(cacheable);
70 if (cacheItems != null && !cacheItems.isEmpty())
72 for (String cacheItem : cacheItems)
74 cacheComboBox.addItem(cacheItem);
78 if (!lastSearch.isEmpty())
80 cacheComboBox.setSelectedItem(lastSearch);
81 cacheComboBox.requestFocus();
85 cacheable.initCache();
86 cacheComboBox.addItem("");
87 cacheComboBox.setSelectedItem("");
92 public void initCache(Cacheable cacheable)
94 String separatedStr = Cache.getProperty(cacheable.getCacheKey());
95 if (separatedStr == null || separatedStr.isEmpty())
100 List<String> persistedCacheItems = Arrays.asList(separatedStr.split(CACHE_DELIMITER));
101 String cacheKey = cacheable.getCacheKey();
103 LinkedHashSet<String> foundCacheItems = cacheItems.get(cacheKey);
104 if (foundCacheItems == null)
106 foundCacheItems = new LinkedHashSet<String>();
109 for (String cacheItem : persistedCacheItems)
111 foundCacheItems.add(cacheItem);
113 cacheItems.put(cacheKey, foundCacheItems);
114 updateCache(cacheable);
118 public void persistCache(Cacheable cacheable)
120 String cacheKey = cacheable.getCacheKey();
121 LinkedHashSet<String> foundCache = getAllCachedItemsFor(cacheable);
122 String commaJoinedStr = String.join(CACHE_DELIMITER, foundCache);
123 Cache.setProperty(cacheKey, commaJoinedStr);