fc528ef564775551a59f19ff80d7f45e4ca9fdb0
[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 DEFAULT_LIMIT_KEY = ".DEFAULT_LIMIT";
24
25   private static final String DEFAULT_LIMIT = "99";
26
27   private static final String CACHE_DELIMITER = ";";
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 an 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    * Method for initialising cache items for a given cache key
68    * 
69    * @param cacheKey
70    */
71   public void initCache(String cacheKey)
72   {
73     String separatedStr = Cache.getProperty(cacheKey);
74     if (separatedStr == null || separatedStr.isEmpty())
75     {
76       return;
77     }
78
79     List<String> persistedCacheItems = Arrays.asList(separatedStr.split(CACHE_DELIMITER));
80
81     LinkedHashSet<String> foundCacheItems = cacheItems.get(cacheKey);
82     if (foundCacheItems == null)
83     {
84       foundCacheItems = new LinkedHashSet<String>();
85     }
86
87     for (String cacheItem : persistedCacheItems)
88     {
89       foundCacheItems.add(cacheItem);
90     }
91     cacheItems.put(cacheKey, foundCacheItems);
92   }
93
94   /**
95    * Method for persisting cache items for a given cache key
96    * 
97    * @param cacheKey
98    */
99   public void persistCache(String cacheKey)
100   {
101     LinkedHashSet<String> foundCacheItems = getAllCachedItemsFor(cacheKey);
102     StringBuffer delimitedCacheBuf = new StringBuffer();
103     for (String cacheItem : foundCacheItems)
104     {
105       delimitedCacheBuf.append(CACHE_DELIMITER).append(cacheItem);
106     }
107     if (delimitedCacheBuf.length() > 0)
108     {
109       delimitedCacheBuf.deleteCharAt(0);
110     }
111     String delimitedCacheString = delimitedCacheBuf.toString();
112
113     Cache.setProperty(cacheKey, delimitedCacheString);
114   }
115
116   /**
117    * Method for deleted cached items for a given cache key
118    * 
119    * @param cacheKey
120    */
121   public void deleteCacheItems(String cacheKey)
122   {
123     cacheItems.put(cacheKey, new LinkedHashSet<String>());
124     persistCache(cacheKey);
125   }
126
127   /**
128    * Method for obtaining the preset maximum cache limit for a given cache key
129    * 
130    * @param cacheKey
131    * @return
132    */
133   public String getCacheLmit(String cacheKey)
134   {
135     String uniqueKey = cacheKey + DEFAULT_LIMIT_KEY;
136     return Cache.getDefault(uniqueKey, DEFAULT_LIMIT);
137   }
138
139   /**
140    * Method for updating the preset maximum cache limit for a given cache key
141    * 
142    * @param cacheKey
143    * @param newLimit
144    * @return
145    */
146   public int updateCacheLimit(String cacheKey, String newLimit)
147   {
148     String uniqueKey = cacheKey + DEFAULT_LIMIT_KEY;
149     String formerLimit = getCacheLmit(cacheKey);
150     if (newLimit != null && !newLimit.isEmpty()
151             && !formerLimit.equals(newLimit))
152     {
153       Cache.setProperty(uniqueKey, newLimit);
154       formerLimit = newLimit;
155     }
156     return Integer.valueOf(formerLimit);
157   }
158
159   /**
160    * Method for inserting cache items for given cache key into the cache data
161    * structure
162    * 
163    * @param cacheKey
164    * @param cacheItems
165    */
166   public void putCache(String cacheKey, LinkedHashSet<String> cacheItems)
167   {
168     getCacheItems().put(cacheKey, cacheItems);
169   }
170
171   /**
172    * Getter method for obtaining cache data structure
173    * 
174    * @return
175    */
176   private Hashtable<String, LinkedHashSet<String>> getCacheItems()
177   {
178     return cacheItems;
179   }
180 }