JAL-1648 added unit test for AppCache
[jalview.git] / src / jalview / io / cache / AppCache.java
index 9f9a25b..9663c98 100644 (file)
@@ -20,6 +20,10 @@ public class AppCache
 
   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()
@@ -109,6 +113,11 @@ public class AppCache
     Cache.setProperty(cacheKey, delimitedCacheString);
   }
 
+  /**
+   * Method for deleted cached items for a given cache key
+   * 
+   * @param cacheKey
+   */
   public void deleteCacheItems(String cacheKey)
   {
     cacheItems.put(cacheKey, new LinkedHashSet<String>());
@@ -116,6 +125,38 @@ public class AppCache
   }
 
   /**
+   * Method for obtaining the preset maximum cache limit for a given cache key
+   * 
+   * @param cacheKey
+   * @return
+   */
+  public String getCacheLmit(String cacheKey)
+  {
+    String uniqueKey = cacheKey + DEFAULT_LIMIT_KEY;
+    return Cache.getDefault(uniqueKey, DEFAULT_LIMIT);
+  }
+
+  /**
+   * Method for updating the preset maximum cache limit for a given cache key
+   * 
+   * @param cacheKey
+   * @param newLimit
+   * @return
+   */
+  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
    * 
@@ -132,7 +173,7 @@ public class AppCache
    * 
    * @return
    */
-  private Hashtable<String, LinkedHashSet<String>> getCacheItems()
+  Hashtable<String, LinkedHashSet<String>> getCacheItems()
   {
     return cacheItems;
   }