JAL-1648 Added improvement to persist cache items to properties file beyound a Jalvie...
[jalview.git] / src / jalview / io / cache / AppCache.java
index 111a99b..26e1eda 100644 (file)
@@ -1,8 +1,12 @@
 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
@@ -11,14 +15,17 @@ public class AppCache implements AppCacheI
 
   private Hashtable<String, LinkedHashSet<String>> cacheItems;
 
+  private static final String CACHE_DELIMITER = ";";
+
   private AppCache()
   {
     cacheItems = new Hashtable<String, LinkedHashSet<String>>();
   }
 
   @Override
-  public LinkedHashSet<String> getAllCachedItemsFor(String cacheKey)
+  public LinkedHashSet<String> getAllCachedItemsFor(Cacheable cacheable)
   {
+    String cacheKey = cacheable.getCacheKey();
     LinkedHashSet<String> foundCache = cacheItems.get(cacheKey);
     if (foundCache == null)
     {
@@ -48,7 +55,7 @@ public class AppCache implements AppCacheI
 
     if (userInput != null && !userInput.isEmpty())
     {
-      LinkedHashSet<String> foundCache = getAllCachedItemsFor(cacheKey);
+      LinkedHashSet<String> foundCache = getAllCachedItemsFor(cacheable);
       foundCache.add(userInput);
       cacheItems.put(cacheKey, foundCache);
     }
@@ -59,7 +66,7 @@ public class AppCache implements AppCacheI
       cacheComboBox.removeAllItems();
     }
 
-    Set<String> cacheItems = getAllCachedItemsFor(cacheKey);
+    Set<String> cacheItems = getAllCachedItemsFor(cacheable);
     if (cacheItems != null && !cacheItems.isEmpty())
     {
       for (String cacheItem : cacheItems)
@@ -75,10 +82,44 @@ public class AppCache implements AppCacheI
     }
     else
     {
-      cacheable.init();
+      cacheable.initCache();
       cacheComboBox.addItem("");
       cacheComboBox.setSelectedItem("");
     }
   }
 
+  @Override
+  public void initCache(Cacheable cacheable)
+  {
+    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>();
+    }
+
+    for (String cacheItem : persistedCacheItems)
+    {
+      foundCacheItems.add(cacheItem);
+    }
+    cacheItems.put(cacheKey, foundCacheItems);
+    updateCache(cacheable);
+  }
+
+  @Override
+  public void persistCache(Cacheable cacheable)
+  {
+    String cacheKey = cacheable.getCacheKey();
+    LinkedHashSet<String> foundCache = getAllCachedItemsFor(cacheable);
+    String commaJoinedStr = String.join(CACHE_DELIMITER, foundCache);
+    Cache.setProperty(cacheKey, commaJoinedStr);
+  }
 }