JAL-2484 added improvement to enable clearing cache items and configuring maximum...
[jalview.git] / src / jalview / io / cache / AppCache.java
index 9201341..8ed33e6 100644 (file)
@@ -1,25 +1,42 @@
 package jalview.io.cache;
 
 
+import jalview.bin.Cache;
+
+import java.util.Arrays;
 import java.util.Hashtable;
 import java.util.LinkedHashSet;
-import java.util.Set;
-
-import javax.swing.JComboBox;
-import javax.swing.JComponent;
+import java.util.List;
 
-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()
   {
     cacheItems = new Hashtable<String, LinkedHashSet<String>>();
   }
 
-  @Override
+  /**
+   * Method to obtain all the cache items for a given cache key
+   * 
+   * @param cacheKey
+   * @return
+   */
   public LinkedHashSet<String> getAllCachedItemsFor(String cacheKey)
   {
     LinkedHashSet<String> foundCache = cacheItems.get(cacheKey);
@@ -32,6 +49,11 @@ public class AppCache implements AppCacheI
   }
 
 
+  /**
+   * Returns an singleton instance of AppCache
+   * 
+   * @return
+   */
   public static AppCache getInstance()
   {
     if (instance == null)
@@ -41,49 +63,100 @@ 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)
   {
-    JComboBox<String> 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())
+    String separatedStr = Cache.getProperty(cacheKey);
+    if (separatedStr == null || separatedStr.isEmpty())
     {
-      LinkedHashSet<String> foundCache = getAllCachedItemsFor(cacheKey);
-      foundCache.add(userInput);
-      cacheItems.put(cacheKey, foundCache);
+      return;
     }
 
-    String lastSearch = userInput;
-    nextFocusableComponent.requestFocusInWindow();
-    if (cacheComboBox.getItemCount() > 0)
+    List<String> persistedCacheItems = Arrays.asList(separatedStr.split(CACHE_DELIMITER));
+
+    LinkedHashSet<String> foundCacheItems = cacheItems.get(cacheKey);
+    if (foundCacheItems == null)
     {
-      cacheComboBox.removeAllItems();
+      foundCacheItems = new LinkedHashSet<String>();
     }
 
-    Set<String> cacheItems = getAllCachedItemsFor(cacheKey);
-    if (cacheItems != null && !cacheItems.isEmpty())
+    for (String cacheItem : persistedCacheItems)
     {
-      for (String cacheItem : cacheItems)
-      {
-        cacheComboBox.addItem(cacheItem);
-      }
+      foundCacheItems.add(cacheItem);
     }
+    cacheItems.put(cacheKey, foundCacheItems);
+  }
 
-    if (!lastSearch.isEmpty())
+  /**
+   * Method for persisting cache items for a given cache key
+   * 
+   * @param cacheKey
+   */
+  public void persistCache(String cacheKey)
+  {
+    LinkedHashSet<String> foundCacheItems = getAllCachedItemsFor(cacheKey);
+    StringBuffer delimitedCacheBuf = new StringBuffer();
+    for (String cacheItem : foundCacheItems)
     {
-      cacheComboBox.setSelectedItem(lastSearch);
-      cacheComboBox.requestFocus();
+      delimitedCacheBuf.append(CACHE_DELIMITER).append(cacheItem);
     }
-    else
+    if (delimitedCacheBuf.length() > 0)
     {
-      cacheable.init();
-      cacheComboBox.addItem("");
-      cacheComboBox.setSelectedItem("");
+      delimitedCacheBuf.deleteCharAt(0);
     }
+    String delimitedCacheString = delimitedCacheBuf.toString();
+
+    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;
+  }
 }