JAL-1648 refactored and improved caching architecture implementation. The improved...
[jalview.git] / src / jalview / io / cache / AppCache.java
index 46f6df2..9f9a25b 100644 (file)
@@ -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<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 +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<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 +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<String> foundCacheItems = getAllCachedItemsFor(cacheable);
+    LinkedHashSet<String> 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<String>());
+    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<String> cacheItems)
+  {
+    getCacheItems().put(cacheKey, cacheItems);
+  }
+
+  /**
+   * Getter method for obtaining cache data structure
+   * 
+   * @return
+   */
+  private Hashtable<String, LinkedHashSet<String>> getCacheItems()
+  {
+    return cacheItems;
+  }
 }