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