X-Git-Url: http://source.jalview.org/gitweb/?a=blobdiff_plain;f=src%2Fjalview%2Fio%2Fcache%2FAppCache.java;h=4514948a6564a2659bc5eafeabf6162d8a66eb94;hb=3d0101179759ef157b088ea135423cd909512d9f;hp=92013415c2bf5ae5e094fcf2a04386942d59a4ca;hpb=0ce95d9c8581b19c1d636f67f67dd670e41f1ca9;p=jalview.git diff --git a/src/jalview/io/cache/AppCache.java b/src/jalview/io/cache/AppCache.java index 9201341..4514948 100644 --- a/src/jalview/io/cache/AppCache.java +++ b/src/jalview/io/cache/AppCache.java @@ -1,17 +1,26 @@ package jalview.io.cache; +import jalview.bin.Cache; import java.util.Hashtable; import java.util.LinkedHashSet; -import java.util.Set; -import javax.swing.JComboBox; -import javax.swing.JComponent; - -public class AppCache implements AppCacheI +/** + * A singleton class used for querying and persisting cache items. + * + * @author tcnofoegbu + * + */ +public class AppCache { + public static final String DEFAULT_LIMIT = "99"; + + public static final String CACHE_DELIMITER = ";"; + private static AppCache instance = null; + private static final String DEFAULT_LIMIT_KEY = ".DEFAULT_LIMIT"; + private Hashtable> cacheItems; private AppCache() @@ -19,7 +28,12 @@ public class AppCache implements AppCacheI cacheItems = new Hashtable>(); } - @Override + /** + * Method to obtain all the cache items for a given cache key + * + * @param cacheKey + * @return + */ public LinkedHashSet getAllCachedItemsFor(String cacheKey) { LinkedHashSet foundCache = cacheItems.get(cacheKey); @@ -31,7 +45,11 @@ public class AppCache implements AppCacheI return foundCache; } - + /** + * Returns a singleton instance of AppCache + * + * @return + */ public static AppCache getInstance() { if (instance == null) @@ -41,49 +59,89 @@ public class AppCache implements AppCacheI return instance; } - @Override - public void updateCache(Cacheable cacheable) + /** + * Method for persisting cache items for a given cache key + * + * @param cacheKey + */ + public void persistCache(String cacheKey) { - JComboBox cacheComboBox = cacheable.getCacheComboBox(); - String cacheKey = cacheable.getCacheKey(); - JComponent nextFocusableComponent = cacheable.getNextFocusableElement(); - String userInput = cacheComboBox.getEditor().getItem() == null ? "" - : cacheComboBox.getEditor().getItem().toString().trim(); - - if (userInput != null && !userInput.isEmpty()) + LinkedHashSet foundCacheItems = getAllCachedItemsFor(cacheKey); + StringBuffer delimitedCacheBuf = new StringBuffer(); + for (String cacheItem : foundCacheItems) { - LinkedHashSet foundCache = getAllCachedItemsFor(cacheKey); - foundCache.add(userInput); - cacheItems.put(cacheKey, foundCache); + delimitedCacheBuf.append(CACHE_DELIMITER).append(cacheItem); } - - String lastSearch = userInput; - nextFocusableComponent.requestFocusInWindow(); - if (cacheComboBox.getItemCount() > 0) + if (delimitedCacheBuf.length() > 0) { - cacheComboBox.removeAllItems(); + delimitedCacheBuf.deleteCharAt(0); } + String delimitedCacheString = delimitedCacheBuf.toString(); - Set cacheItems = getAllCachedItemsFor(cacheKey); - if (cacheItems != null && !cacheItems.isEmpty()) - { - for (String cacheItem : cacheItems) - { - cacheComboBox.addItem(cacheItem); - } - } + Cache.setProperty(cacheKey, delimitedCacheString); + } - if (!lastSearch.isEmpty()) - { - cacheComboBox.setSelectedItem(lastSearch); - cacheComboBox.requestFocus(); - } - else + /** + * Method for deleting cached items for a given cache key + * + * @param cacheKey + * the cache key + */ + public void deleteCacheItems(String cacheKey) + { + cacheItems.put(cacheKey, new LinkedHashSet()); + persistCache(cacheKey); + } + + /** + * Method for obtaining the preset maximum cache limit for a given cache key + * + * @param cacheKey + * the cache key + * @return the max number of items that could be cached + */ + public String getCacheLimit(String cacheKey) + { + String uniqueKey = cacheKey + DEFAULT_LIMIT_KEY; + return Cache.getDefault(uniqueKey, DEFAULT_LIMIT); + } + + /** + * Method for updating the preset maximum cache limit for a given cache key + * + * @param cacheKey + * the cache key + * @param newLimit + * the max number of items that could be cached for the given cache + * key + * @return + */ + public int updateCacheLimit(String cacheKey, int newUserLimit) + { + String newLimit = String.valueOf(newUserLimit); + String uniqueKey = cacheKey + DEFAULT_LIMIT_KEY; + String formerLimit = getCacheLimit(cacheKey); + if (newLimit != null && !newLimit.isEmpty() + && !formerLimit.equals(newLimit)) { - cacheable.init(); - cacheComboBox.addItem(""); - cacheComboBox.setSelectedItem(""); + Cache.setProperty(uniqueKey, newLimit); + formerLimit = newLimit; } + return Integer.valueOf(formerLimit); + } + + /** + * Method for inserting cache items for given cache key into the cache data + * structure + * + * @param cacheKey + * the cache key + * @param cacheItems + * the items to add to the cache + */ + public void putCache(String cacheKey, LinkedHashSet newCacheItems) + { + cacheItems.put(cacheKey, newCacheItems); } }