X-Git-Url: http://source.jalview.org/gitweb/?a=blobdiff_plain;f=src%2Fjalview%2Fio%2Fcache%2FAppCache.java;fp=src%2Fjalview%2Fio%2Fcache%2FAppCache.java;h=9f9a25b37c2b3d9c3d01ae5675e223dc8e82dca1;hb=221f613b5d235a11c602b989598afe97b5b7b203;hp=46f6df2c6410422d1361681bda505b61eede26cf;hpb=75415606931bbe145f471ff2e13abefc431dbd2c;p=jalview.git diff --git a/src/jalview/io/cache/AppCache.java b/src/jalview/io/cache/AppCache.java index 46f6df2..9f9a25b 100644 --- a/src/jalview/io/cache/AppCache.java +++ b/src/jalview/io/cache/AppCache.java @@ -7,9 +7,14 @@ import java.util.Arrays; import java.util.Hashtable; import java.util.LinkedHashSet; import java.util.List; -import java.util.Set; -public class AppCache implements AppCacheI +/** + * A singleton class used for querying and persisting cache items. + * + * @author tcnofoegbu + * + */ +public class AppCache { private static AppCache instance = null; @@ -22,10 +27,14 @@ public class AppCache implements AppCacheI cacheItems = new Hashtable>(); } - @Override - public LinkedHashSet getAllCachedItemsFor(Cacheable cacheable) + /** + * Method to obtain all the cache items for a given cache key + * + * @param cacheKey + * @return + */ + public LinkedHashSet getAllCachedItemsFor(String cacheKey) { - String cacheKey = cacheable.getCacheKey(); LinkedHashSet foundCache = cacheItems.get(cacheKey); if (foundCache == null) { @@ -36,6 +45,11 @@ public class AppCache implements AppCacheI } + /** + * Returns an singleton instance of AppCache + * + * @return + */ public static AppCache getInstance() { if (instance == null) @@ -45,60 +59,20 @@ public class AppCache implements AppCacheI return instance; } - @Override - public void updateCache(Cacheable cacheable) + /** + * Method for initialising cache items for a given cache key + * + * @param cacheKey + */ + public void initCache(String cacheKey) { - CacheBoxI cacheComboBox = cacheable.getCacheComboBox(); - String cacheKey = cacheable.getCacheKey(); - cacheComboBox.looseFocus(); - String userInput = cacheComboBox.getUserInput(); - - if (userInput != null && !userInput.isEmpty()) - { - LinkedHashSet foundCache = getAllCachedItemsFor(cacheable); - foundCache.add(userInput); - cacheItems.put(cacheKey, foundCache); - } - - String lastSearch = userInput; - if (cacheComboBox.getItemCount() > 0) - { - cacheComboBox.removeAllItems(); - } - - Set cacheItems = getAllCachedItemsFor(cacheable); - if (cacheItems != null && !cacheItems.isEmpty()) - { - for (String cacheItem : cacheItems) - { - cacheComboBox.addItem(cacheItem); - } - } - - if (!lastSearch.isEmpty()) - { - cacheComboBox.setSelectedItem(lastSearch); - cacheComboBox.requestFocus(); - } - else - { - cacheable.initCache(); - cacheComboBox.addItem(""); - cacheComboBox.setSelectedItem(""); - } - } - - @Override - public void initCache(Cacheable cacheable) - { - String separatedStr = Cache.getProperty(cacheable.getCacheKey()); + String separatedStr = Cache.getProperty(cacheKey); if (separatedStr == null || separatedStr.isEmpty()) { return; } List persistedCacheItems = Arrays.asList(separatedStr.split(CACHE_DELIMITER)); - String cacheKey = cacheable.getCacheKey(); LinkedHashSet foundCacheItems = cacheItems.get(cacheKey); if (foundCacheItems == null) @@ -111,14 +85,16 @@ public class AppCache implements AppCacheI foundCacheItems.add(cacheItem); } cacheItems.put(cacheKey, foundCacheItems); - updateCache(cacheable); } - @Override - public void persistCache(Cacheable cacheable) + /** + * Method for persisting cache items for a given cache key + * + * @param cacheKey + */ + public void persistCache(String cacheKey) { - String cacheKey = cacheable.getCacheKey(); - LinkedHashSet foundCacheItems = getAllCachedItemsFor(cacheable); + LinkedHashSet foundCacheItems = getAllCachedItemsFor(cacheKey); StringBuffer delimitedCacheBuf = new StringBuffer(); for (String cacheItem : foundCacheItems) { @@ -132,4 +108,32 @@ public class AppCache implements AppCacheI Cache.setProperty(cacheKey, delimitedCacheString); } + + public void deleteCacheItems(String cacheKey) + { + cacheItems.put(cacheKey, new LinkedHashSet()); + persistCache(cacheKey); + } + + /** + * Method for inserting cache items for given cache key into the cache data + * structure + * + * @param cacheKey + * @param cacheItems + */ + public void putCache(String cacheKey, LinkedHashSet cacheItems) + { + getCacheItems().put(cacheKey, cacheItems); + } + + /** + * Getter method for obtaining cache data structure + * + * @return + */ + private Hashtable> getCacheItems() + { + return cacheItems; + } }