JAL-2418 source formatting
[jalview.git] / src / jalview / io / cache / AppCache.java
index 46f6df2..4514948 100644 (file)
@@ -1,31 +1,41 @@
 package jalview.io.cache;
 
-
 import jalview.bin.Cache;
 
-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
 {
+  public static final String DEFAULT_LIMIT = "99";
+
+  public static final String CACHE_DELIMITER = ";";
+
   private static AppCache instance = null;
 
-  private Hashtable<String, LinkedHashSet<String>> cacheItems;
+  private static final String DEFAULT_LIMIT_KEY = ".DEFAULT_LIMIT";
 
-  private static final String CACHE_DELIMITER = ";";
+  private Hashtable<String, LinkedHashSet<String>> cacheItems;
 
   private AppCache()
   {
     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)
     {
@@ -35,7 +45,11 @@ public class AppCache implements AppCacheI
     return foundCache;
   }
 
-
+  /**
+   * Returns a singleton instance of AppCache
+   * 
+   * @return
+   */
   public static AppCache getInstance()
   {
     if (instance == null)
@@ -45,91 +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)
   {
-    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)
+    LinkedHashSet<String> foundCacheItems = getAllCachedItemsFor(cacheKey);
+    StringBuffer delimitedCacheBuf = new StringBuffer();
+    for (String cacheItem : foundCacheItems)
     {
-      cacheComboBox.removeAllItems();
+      delimitedCacheBuf.append(CACHE_DELIMITER).append(cacheItem);
     }
-
-    Set<String> cacheItems = getAllCachedItemsFor(cacheable);
-    if (cacheItems != null && !cacheItems.isEmpty())
+    if (delimitedCacheBuf.length() > 0)
     {
-      for (String cacheItem : cacheItems)
-      {
-        cacheComboBox.addItem(cacheItem);
-      }
+      delimitedCacheBuf.deleteCharAt(0);
     }
+    String delimitedCacheString = delimitedCacheBuf.toString();
 
-    if (!lastSearch.isEmpty())
-    {
-      cacheComboBox.setSelectedItem(lastSearch);
-      cacheComboBox.requestFocus();
-    }
-    else
-    {
-      cacheable.initCache();
-      cacheComboBox.addItem("");
-      cacheComboBox.setSelectedItem("");
-    }
+    Cache.setProperty(cacheKey, delimitedCacheString);
   }
 
-  @Override
-  public void initCache(Cacheable cacheable)
+  /**
+   * Method for deleting cached items for a given cache key
+   * 
+   * @param cacheKey
+   *          the cache key
+   */
+  public void deleteCacheItems(String cacheKey)
   {
-    String separatedStr = Cache.getProperty(cacheable.getCacheKey());
-    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)
-    {
-      foundCacheItems = new LinkedHashSet<String>();
-    }
+    cacheItems.put(cacheKey, new LinkedHashSet<String>());
+    persistCache(cacheKey);
+  }
 
-    for (String cacheItem : persistedCacheItems)
-    {
-      foundCacheItems.add(cacheItem);
-    }
-    cacheItems.put(cacheKey, foundCacheItems);
-    updateCache(cacheable);
+  /**
+   * 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);
   }
 
-  @Override
-  public void persistCache(Cacheable cacheable)
+  /**
+   * 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 cacheKey = cacheable.getCacheKey();
-    LinkedHashSet<String> foundCacheItems = getAllCachedItemsFor(cacheable);
-    StringBuffer delimitedCacheBuf = new StringBuffer();
-    for (String cacheItem : foundCacheItems)
-    {
-      delimitedCacheBuf.append(CACHE_DELIMITER).append(cacheItem);
-    }
-    if (delimitedCacheBuf.length() > 0)
+    String newLimit = String.valueOf(newUserLimit);
+    String uniqueKey = cacheKey + DEFAULT_LIMIT_KEY;
+    String formerLimit = getCacheLimit(cacheKey);
+    if (newLimit != null && !newLimit.isEmpty()
+            && !formerLimit.equals(newLimit))
     {
-      delimitedCacheBuf.deleteCharAt(0);
+      Cache.setProperty(uniqueKey, newLimit);
+      formerLimit = newLimit;
     }
-    String delimitedCacheString = delimitedCacheBuf.toString();
+    return Integer.valueOf(formerLimit);
+  }
 
-    Cache.setProperty(cacheKey, delimitedCacheString);
+  /**
+   * 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<String> newCacheItems)
+  {
+    cacheItems.put(cacheKey, newCacheItems);
   }
+
 }