9f9a25b37c2b3d9c3d01ae5675e223dc8e82dca1
[jalview.git] / src / jalview / io / cache / AppCache.java
1 package jalview.io.cache;
2
3
4 import jalview.bin.Cache;
5
6 import java.util.Arrays;
7 import java.util.Hashtable;
8 import java.util.LinkedHashSet;
9 import java.util.List;
10
11 /**
12  * A singleton class used for querying and persisting cache items.
13  * 
14  * @author tcnofoegbu
15  *
16  */
17 public class AppCache
18 {
19   private static AppCache instance = null;
20
21   private Hashtable<String, LinkedHashSet<String>> cacheItems;
22
23   private static final String CACHE_DELIMITER = ";";
24
25   private AppCache()
26   {
27     cacheItems = new Hashtable<String, LinkedHashSet<String>>();
28   }
29
30   /**
31    * Method to obtain all the cache items for a given cache key
32    * 
33    * @param cacheKey
34    * @return
35    */
36   public LinkedHashSet<String> getAllCachedItemsFor(String cacheKey)
37   {
38     LinkedHashSet<String> foundCache = cacheItems.get(cacheKey);
39     if (foundCache == null)
40     {
41       foundCache = new LinkedHashSet<String>();
42       cacheItems.put(cacheKey, foundCache);
43     }
44     return foundCache;
45   }
46
47
48   /**
49    * Returns an singleton instance of AppCache
50    * 
51    * @return
52    */
53   public static AppCache getInstance()
54   {
55     if (instance == null)
56     {
57       instance = new AppCache();
58     }
59     return instance;
60   }
61
62   /**
63    * Method for initialising cache items for a given cache key
64    * 
65    * @param cacheKey
66    */
67   public void initCache(String cacheKey)
68   {
69     String separatedStr = Cache.getProperty(cacheKey);
70     if (separatedStr == null || separatedStr.isEmpty())
71     {
72       return;
73     }
74
75     List<String> persistedCacheItems = Arrays.asList(separatedStr.split(CACHE_DELIMITER));
76
77     LinkedHashSet<String> foundCacheItems = cacheItems.get(cacheKey);
78     if (foundCacheItems == null)
79     {
80       foundCacheItems = new LinkedHashSet<String>();
81     }
82
83     for (String cacheItem : persistedCacheItems)
84     {
85       foundCacheItems.add(cacheItem);
86     }
87     cacheItems.put(cacheKey, foundCacheItems);
88   }
89
90   /**
91    * Method for persisting cache items for a given cache key
92    * 
93    * @param cacheKey
94    */
95   public void persistCache(String cacheKey)
96   {
97     LinkedHashSet<String> foundCacheItems = getAllCachedItemsFor(cacheKey);
98     StringBuffer delimitedCacheBuf = new StringBuffer();
99     for (String cacheItem : foundCacheItems)
100     {
101       delimitedCacheBuf.append(CACHE_DELIMITER).append(cacheItem);
102     }
103     if (delimitedCacheBuf.length() > 0)
104     {
105       delimitedCacheBuf.deleteCharAt(0);
106     }
107     String delimitedCacheString = delimitedCacheBuf.toString();
108
109     Cache.setProperty(cacheKey, delimitedCacheString);
110   }
111
112   public void deleteCacheItems(String cacheKey)
113   {
114     cacheItems.put(cacheKey, new LinkedHashSet<String>());
115     persistCache(cacheKey);
116   }
117
118   /**
119    * Method for inserting cache items for given cache key into the cache data
120    * structure
121    * 
122    * @param cacheKey
123    * @param cacheItems
124    */
125   public void putCache(String cacheKey, LinkedHashSet<String> cacheItems)
126   {
127     getCacheItems().put(cacheKey, cacheItems);
128   }
129
130   /**
131    * Getter method for obtaining cache data structure
132    * 
133    * @return
134    */
135   private Hashtable<String, LinkedHashSet<String>> getCacheItems()
136   {
137     return cacheItems;
138   }
139 }