Merge branch 'features/JAL-2446NCList' into features/JAL-2609fastPaintWrapped
[jalview.git] / src / jalview / io / cache / AppCache.java
index 9f9a25b..091d30e 100644 (file)
@@ -3,10 +3,8 @@ package jalview.io.cache;
 
 import jalview.bin.Cache;
 
-import java.util.Arrays;
 import java.util.Hashtable;
 import java.util.LinkedHashSet;
-import java.util.List;
 
 /**
  * A singleton class used for querying and persisting cache items.
@@ -16,11 +14,17 @@ import java.util.List;
  */
 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()
   {
@@ -46,7 +50,7 @@ public class AppCache
 
 
   /**
-   * Returns an singleton instance of AppCache
+   * Returns a singleton instance of AppCache
    * 
    * @return
    */
@@ -59,33 +63,7 @@ public class AppCache
     return instance;
   }
 
-  /**
-   * Method for initialising cache items for a given cache key
-   * 
-   * @param cacheKey
-   */
-  public void initCache(String cacheKey)
-  {
-    String separatedStr = Cache.getProperty(cacheKey);
-    if (separatedStr == null || separatedStr.isEmpty())
-    {
-      return;
-    }
-
-    List<String> persistedCacheItems = Arrays.asList(separatedStr.split(CACHE_DELIMITER));
-
-    LinkedHashSet<String> foundCacheItems = cacheItems.get(cacheKey);
-    if (foundCacheItems == null)
-    {
-      foundCacheItems = new LinkedHashSet<String>();
-    }
 
-    for (String cacheItem : persistedCacheItems)
-    {
-      foundCacheItems.add(cacheItem);
-    }
-    cacheItems.put(cacheKey, foundCacheItems);
-  }
 
   /**
    * Method for persisting cache items for a given cache key
@@ -109,6 +87,12 @@ public class AppCache
     Cache.setProperty(cacheKey, delimitedCacheString);
   }
 
+  /**
+   * 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<String>());
@@ -116,24 +100,54 @@ public class AppCache
   }
 
   /**
-   * Method for inserting cache items for given cache key into the cache data
-   * structure
+   * Method for obtaining the preset maximum cache limit for a given cache key
    * 
    * @param cacheKey
-   * @param cacheItems
+   *          the cache key
+   * @return the max number of items that could be cached
    */
-  public void putCache(String cacheKey, LinkedHashSet<String> cacheItems)
+  public String getCacheLimit(String cacheKey)
   {
-    getCacheItems().put(cacheKey, cacheItems);
+    String uniqueKey = cacheKey + DEFAULT_LIMIT_KEY;
+    return Cache.getDefault(uniqueKey, DEFAULT_LIMIT);
   }
 
   /**
-   * Getter method for obtaining cache data structure
+   * 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
    */
-  private Hashtable<String, LinkedHashSet<String>> getCacheItems()
+  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))
+    {
+      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<String> newCacheItems)
   {
-    return cacheItems;
+    cacheItems.put(cacheKey, newCacheItems);
   }
+
 }