JAL-2484 added improvement to enable clearing cache items and configuring maximum...
[jalview.git] / src / jalview / io / cache / AppCache.java
index 46f6df2..8ed33e6 100644 (file)
@@ -7,14 +7,23 @@ 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;
 
   private Hashtable<String, LinkedHashSet<String>> cacheItems;
 
+  private static final String DEFAULT_LIMIT_KEY = ".DEFAULT_LIMIT";
+
+  private static final String DEFAULT_LIMIT = "99";
+
   private static final String CACHE_DELIMITER = ";";
 
   private AppCache()
@@ -22,10 +31,14 @@ public class AppCache implements AppCacheI
     cacheItems = new Hashtable<String, LinkedHashSet<String>>();
   }
 
-  @Override
-  public LinkedHashSet<String> getAllCachedItemsFor(Cacheable cacheable)
+  /**
+   * Method to obtain all the cache items for a given cache key
+   * 
+   * @param cacheKey
+   * @return
+   */
+  public LinkedHashSet<String> getAllCachedItemsFor(String cacheKey)
   {
-    String cacheKey = cacheable.getCacheKey();
     LinkedHashSet<String> foundCache = cacheItems.get(cacheKey);
     if (foundCache == null)
     {
@@ -36,6 +49,11 @@ public class AppCache implements AppCacheI
   }
 
 
+  /**
+   * Returns an singleton instance of AppCache
+   * 
+   * @return
+   */
   public static AppCache getInstance()
   {
     if (instance == null)
@@ -45,60 +63,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<String> cacheComboBox = cacheable.getCacheComboBox();
-    String cacheKey = cacheable.getCacheKey();
-    cacheComboBox.looseFocus();
-    String userInput = cacheComboBox.getUserInput();
-
-    if (userInput != null && !userInput.isEmpty())
-    {
-      LinkedHashSet<String> foundCache = getAllCachedItemsFor(cacheable);
-      foundCache.add(userInput);
-      cacheItems.put(cacheKey, foundCache);
-    }
-
-    String lastSearch = userInput;
-    if (cacheComboBox.getItemCount() > 0)
-    {
-      cacheComboBox.removeAllItems();
-    }
-
-    Set<String> 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<String> persistedCacheItems = Arrays.asList(separatedStr.split(CACHE_DELIMITER));
-    String cacheKey = cacheable.getCacheKey();
 
     LinkedHashSet<String> foundCacheItems = cacheItems.get(cacheKey);
     if (foundCacheItems == null)
@@ -111,14 +89,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<String> foundCacheItems = getAllCachedItemsFor(cacheable);
+    LinkedHashSet<String> foundCacheItems = getAllCachedItemsFor(cacheKey);
     StringBuffer delimitedCacheBuf = new StringBuffer();
     for (String cacheItem : foundCacheItems)
     {
@@ -132,4 +112,51 @@ public class AppCache implements AppCacheI
 
     Cache.setProperty(cacheKey, delimitedCacheString);
   }
+
+  public void deleteCacheItems(String cacheKey)
+  {
+    cacheItems.put(cacheKey, new LinkedHashSet<String>());
+    persistCache(cacheKey);
+  }
+
+  public String getCacheLmit(String cacheKey)
+  {
+    String uniqueKey = cacheKey + DEFAULT_LIMIT_KEY;
+    return Cache.getDefault(uniqueKey, DEFAULT_LIMIT);
+  }
+
+  public int updateCacheLimit(String cacheKey, String newLimit)
+  {
+    String uniqueKey = cacheKey + DEFAULT_LIMIT_KEY;
+    String formerLimit = getCacheLmit(cacheKey);
+    if (newLimit != null && !newLimit.isEmpty()
+            && !formerLimit.equals(newLimit))
+    {
+      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
+   * @param cacheItems
+   */
+  public void putCache(String cacheKey, LinkedHashSet<String> cacheItems)
+  {
+    getCacheItems().put(cacheKey, cacheItems);
+  }
+
+  /**
+   * Getter method for obtaining cache data structure
+   * 
+   * @return
+   */
+  private Hashtable<String, LinkedHashSet<String>> getCacheItems()
+  {
+    return cacheItems;
+  }
 }