JAL-2664 Updates following review
[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.Hashtable;
7 import java.util.LinkedHashSet;
8
9 /**
10  * A singleton class used for querying and persisting cache items.
11  * 
12  * @author tcnofoegbu
13  *
14  */
15 public class AppCache
16 {
17   public static final String DEFAULT_LIMIT = "99";
18
19   public static final String CACHE_DELIMITER = ";";
20
21   private static AppCache instance = null;
22
23   private static final String DEFAULT_LIMIT_KEY = ".DEFAULT_LIMIT";
24
25
26
27   private Hashtable<String, LinkedHashSet<String>> cacheItems;
28
29   private AppCache()
30   {
31     cacheItems = new Hashtable<String, LinkedHashSet<String>>();
32   }
33
34   /**
35    * Method to obtain all the cache items for a given cache key
36    * 
37    * @param cacheKey
38    * @return
39    */
40   public LinkedHashSet<String> getAllCachedItemsFor(String cacheKey)
41   {
42     LinkedHashSet<String> foundCache = cacheItems.get(cacheKey);
43     if (foundCache == null)
44     {
45       foundCache = new LinkedHashSet<String>();
46       cacheItems.put(cacheKey, foundCache);
47     }
48     return foundCache;
49   }
50
51
52   /**
53    * Returns a singleton instance of AppCache
54    * 
55    * @return
56    */
57   public static AppCache getInstance()
58   {
59     if (instance == null)
60     {
61       instance = new AppCache();
62     }
63     return instance;
64   }
65
66
67
68   /**
69    * Method for persisting cache items for a given cache key
70    * 
71    * @param cacheKey
72    */
73   public void persistCache(String cacheKey)
74   {
75     LinkedHashSet<String> foundCacheItems = getAllCachedItemsFor(cacheKey);
76     StringBuffer delimitedCacheBuf = new StringBuffer();
77     for (String cacheItem : foundCacheItems)
78     {
79       delimitedCacheBuf.append(CACHE_DELIMITER).append(cacheItem);
80     }
81     if (delimitedCacheBuf.length() > 0)
82     {
83       delimitedCacheBuf.deleteCharAt(0);
84     }
85     String delimitedCacheString = delimitedCacheBuf.toString();
86
87     Cache.setProperty(cacheKey, delimitedCacheString);
88   }
89
90   /**
91    * Method for deleting cached items for a given cache key
92    * 
93    * @param cacheKey
94    *          the cache key
95    */
96   public void deleteCacheItems(String cacheKey)
97   {
98     cacheItems.put(cacheKey, new LinkedHashSet<String>());
99     persistCache(cacheKey);
100   }
101
102   /**
103    * Method for obtaining the preset maximum cache limit for a given cache key
104    * 
105    * @param cacheKey
106    *          the cache key
107    * @return the max number of items that could be cached
108    */
109   public String getCacheLimit(String cacheKey)
110   {
111     String uniqueKey = cacheKey + DEFAULT_LIMIT_KEY;
112     return Cache.getDefault(uniqueKey, DEFAULT_LIMIT);
113   }
114
115   /**
116    * Method for updating the preset maximum cache limit for a given cache key
117    * 
118    * @param cacheKey
119    *          the cache key
120    * @param newLimit
121    *          the max number of items that could be cached for the given cache
122    *          key
123    * @return
124    */
125   public int updateCacheLimit(String cacheKey, int newUserLimit)
126   {
127     String newLimit = String.valueOf(newUserLimit);
128     String uniqueKey = cacheKey + DEFAULT_LIMIT_KEY;
129     String formerLimit = getCacheLimit(cacheKey);
130     if (newLimit != null && !newLimit.isEmpty()
131             && !formerLimit.equals(newLimit))
132     {
133       Cache.setProperty(uniqueKey, newLimit);
134       formerLimit = newLimit;
135     }
136     return Integer.valueOf(formerLimit);
137   }
138
139   /**
140    * Method for inserting cache items for given cache key into the cache data
141    * structure
142    * 
143    * @param cacheKey
144    *          the cache key
145    * @param cacheItems
146    *          the items to add to the cache
147    */
148   public void putCache(String cacheKey, LinkedHashSet<String> newCacheItems)
149   {
150     cacheItems.put(cacheKey, newCacheItems);
151   }
152
153 }