import java.util.Hashtable;
import java.util.LinkedHashSet;
import java.util.List;
-import java.util.Set;
-public class AppCache implements AppCacheI
+/**
+ * A singleton class used for querying and persisting cache items.
+ *
+ * @author tcnofoegbu
+ *
+ */
+public class AppCache
{
private static AppCache instance = null;
cacheItems = new Hashtable<String, LinkedHashSet<String>>();
}
- @Override
- public LinkedHashSet<String> getAllCachedItemsFor(Cacheable cacheable)
+ /**
+ * Method to obtain all the cache items for a given cache key
+ *
+ * @param cacheKey
+ * @return
+ */
+ public LinkedHashSet<String> getAllCachedItemsFor(String cacheKey)
{
- String cacheKey = cacheable.getCacheKey();
LinkedHashSet<String> foundCache = cacheItems.get(cacheKey);
if (foundCache == null)
{
}
+ /**
+ * Returns an singleton instance of AppCache
+ *
+ * @return
+ */
public static AppCache getInstance()
{
if (instance == null)
return instance;
}
- @Override
- public void updateCache(Cacheable cacheable)
+ /**
+ * Method for initialising cache items for a given cache key
+ *
+ * @param cacheKey
+ */
+ public void initCache(String cacheKey)
{
- CacheBoxI<String> cacheComboBox = cacheable.getCacheComboBox();
- String cacheKey = cacheable.getCacheKey();
- cacheComboBox.looseFocus();
- String userInput = cacheComboBox.getUserInput();
-
- if (userInput != null && !userInput.isEmpty())
- {
- LinkedHashSet<String> foundCache = getAllCachedItemsFor(cacheable);
- foundCache.add(userInput);
- cacheItems.put(cacheKey, foundCache);
- }
-
- String lastSearch = userInput;
- if (cacheComboBox.getItemCount() > 0)
- {
- cacheComboBox.removeAllItems();
- }
-
- Set<String> cacheItems = getAllCachedItemsFor(cacheable);
- if (cacheItems != null && !cacheItems.isEmpty())
- {
- for (String cacheItem : cacheItems)
- {
- cacheComboBox.addItem(cacheItem);
- }
- }
-
- if (!lastSearch.isEmpty())
- {
- cacheComboBox.setSelectedItem(lastSearch);
- cacheComboBox.requestFocus();
- }
- else
- {
- cacheable.initCache();
- cacheComboBox.addItem("");
- cacheComboBox.setSelectedItem("");
- }
- }
-
- @Override
- public void initCache(Cacheable cacheable)
- {
- String separatedStr = Cache.getProperty(cacheable.getCacheKey());
+ String separatedStr = Cache.getProperty(cacheKey);
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.add(cacheItem);
}
cacheItems.put(cacheKey, foundCacheItems);
- updateCache(cacheable);
}
- @Override
- public void persistCache(Cacheable cacheable)
+ /**
+ * Method for persisting cache items for a given cache key
+ *
+ * @param cacheKey
+ */
+ public void persistCache(String cacheKey)
{
- String cacheKey = cacheable.getCacheKey();
- LinkedHashSet<String> foundCacheItems = getAllCachedItemsFor(cacheable);
+ LinkedHashSet<String> foundCacheItems = getAllCachedItemsFor(cacheKey);
StringBuffer delimitedCacheBuf = new StringBuffer();
for (String cacheItem : foundCacheItems)
{
Cache.setProperty(cacheKey, delimitedCacheString);
}
+
+ public void deleteCacheItems(String cacheKey)
+ {
+ cacheItems.put(cacheKey, new LinkedHashSet<String>());
+ persistCache(cacheKey);
+ }
+
+ /**
+ * Method for inserting cache items for given cache key into the cache data
+ * structure
+ *
+ * @param cacheKey
+ * @param cacheItems
+ */
+ public void putCache(String cacheKey, LinkedHashSet<String> cacheItems)
+ {
+ getCacheItems().put(cacheKey, cacheItems);
+ }
+
+ /**
+ * Getter method for obtaining cache data structure
+ *
+ * @return
+ */
+ private Hashtable<String, LinkedHashSet<String>> getCacheItems()
+ {
+ return cacheItems;
+ }
}