import jalview.bin.Cache;
-import java.util.Arrays;
import java.util.Hashtable;
import java.util.LinkedHashSet;
-import java.util.List;
/**
* A singleton class used for querying and persisting cache items.
*/
public class AppCache
{
- private static AppCache instance = null;
+ public static final String DEFAULT_LIMIT = "99";
- private Hashtable<String, LinkedHashSet<String>> cacheItems;
+ public static final String CACHE_DELIMITER = ";";
+
+ private static AppCache instance = null;
private static final String DEFAULT_LIMIT_KEY = ".DEFAULT_LIMIT";
- private static final String DEFAULT_LIMIT = "99";
- private static final String CACHE_DELIMITER = ";";
+
+ private Hashtable<String, LinkedHashSet<String>> cacheItems;
private AppCache()
{
/**
- * Returns an singleton instance of AppCache
+ * Returns a singleton instance of AppCache
*
* @return
*/
return instance;
}
- /**
- * Method for initialising cache items for a given cache key
- *
- * @param cacheKey
- */
- public void initCache(String cacheKey)
- {
- String separatedStr = Cache.getProperty(cacheKey);
- if (separatedStr == null || separatedStr.isEmpty())
- {
- return;
- }
- List<String> persistedCacheItems = Arrays.asList(separatedStr.split(CACHE_DELIMITER));
-
- LinkedHashSet<String> foundCacheItems = cacheItems.get(cacheKey);
- if (foundCacheItems == null)
- {
- foundCacheItems = new LinkedHashSet<String>();
- }
-
- for (String cacheItem : persistedCacheItems)
- {
- foundCacheItems.add(cacheItem);
- }
- cacheItems.put(cacheKey, foundCacheItems);
- }
/**
* Method for persisting cache items for a given cache key
}
/**
- * Method for deleted cached items for a given cache key
+ * Method for deleting cached items for a given cache key
*
* @param cacheKey
+ * the cache key
*/
public void deleteCacheItems(String cacheKey)
{
* Method for obtaining the preset maximum cache limit for a given cache key
*
* @param cacheKey
- * @return
+ * the cache key
+ * @return the max number of items that could be cached
*/
- public String getCacheLmit(String cacheKey)
+ public String getCacheLimit(String cacheKey)
{
String uniqueKey = cacheKey + DEFAULT_LIMIT_KEY;
return Cache.getDefault(uniqueKey, DEFAULT_LIMIT);
* Method for updating the preset maximum cache limit for a given cache key
*
* @param cacheKey
+ * the cache key
* @param newLimit
+ * the max number of items that could be cached for the given cache
+ * key
* @return
*/
- public int updateCacheLimit(String cacheKey, String newLimit)
+ public int updateCacheLimit(String cacheKey, int newUserLimit)
{
+ String newLimit = String.valueOf(newUserLimit);
String uniqueKey = cacheKey + DEFAULT_LIMIT_KEY;
- String formerLimit = getCacheLmit(cacheKey);
+ String formerLimit = getCacheLimit(cacheKey);
if (newLimit != null && !newLimit.isEmpty()
&& !formerLimit.equals(newLimit))
{
* structure
*
* @param cacheKey
+ * the cache key
* @param cacheItems
+ * the items to add to the cache
*/
- public void putCache(String cacheKey, LinkedHashSet<String> cacheItems)
+ public void putCache(String cacheKey, LinkedHashSet<String> newCacheItems)
{
- getCacheItems().put(cacheKey, cacheItems);
+ cacheItems.put(cacheKey, newCacheItems);
}
- /**
- * Getter method for obtaining cache data structure
- *
- * @return
- */
- Hashtable<String, LinkedHashSet<String>> getCacheItems()
- {
- return cacheItems;
- }
}
package jalview.io.cache;
+import jalview.bin.Cache;
import jalview.util.MessageManager;
import java.awt.Color;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.ArrayList;
+import java.util.Arrays;
import java.util.Collections;
import java.util.LinkedHashSet;
import java.util.List;
private static final long serialVersionUID = 5774610435079326695L;
- private String cacheKey;
+ private static final int INPUT_LIMIT = 2;
- private AppCache appCache;
+ private static final int LEFT_BOARDER_WIDTH = 2;
- JPanel pnlDefaultCache = new JPanel();
+ private String cacheKey;
- JLabel lblDefaultCacheSize = new JLabel();
+ private AppCache appCache;
- JTextField txtDefaultCacheSize = new JTextField();
+ private JPanel pnlDefaultCache = new JPanel();
- JPopupMenu popup = new JPopupMenu();
+ private JLabel lblDefaultCacheSize = new JLabel();
- JMenuItem menuItemClearCache = new JMenuItem();
+ private JTextField txtDefaultCacheSize = new JTextField();
- final static int INPUT_LIMIT = 2;
+ private JPopupMenu popup = new JPopupMenu();
+ private JMenuItem menuItemClearCache = new JMenuItem();
- public JvCacheableInputBox(String cacheKey)
+ public JvCacheableInputBox(String newCacheKey)
{
super();
- this.cacheKey = cacheKey;
+ this.cacheKey = newCacheKey;
setEditable(true);
setPrototypeDisplayValue("XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX");
appCache = AppCache.getInstance();
initCachePopupMenu();
- appCache.initCache(cacheKey);
+ initCache(newCacheKey);
updateCache();
}
+ /**
+ * Method for initialising cache items for a given cache key and populating
+ * the in-memory cache with persisted cache items
+ *
+ * @param cacheKey
+ */
+ private void initCache(String cacheKey)
+ {
+ // obtain persisted cache items from properties file as a delimited string
+ String delimitedCacheStr = Cache.getProperty(cacheKey);
+ if (delimitedCacheStr == null || delimitedCacheStr.isEmpty())
+ {
+ return;
+ }
+ // convert delimited cache items to a list of strings
+ List<String> persistedCacheItems = Arrays.asList(delimitedCacheStr
+ .split(AppCache.CACHE_DELIMITER));
+
+ LinkedHashSet<String> foundCacheItems = appCache
+ .getAllCachedItemsFor(cacheKey);// cacheItems.get(cacheKey);
+ if (foundCacheItems == null)
+ {
+ foundCacheItems = new LinkedHashSet<String>();
+ }
+ // populate memory cache
+ for (String cacheItem : persistedCacheItems)
+ {
+ foundCacheItems.add(cacheItem);
+ }
+ appCache.putCache(cacheKey, foundCacheItems);
+ }
/**
* Initialise this cache's pop-up menu
{
pnlDefaultCache.setBackground(Color.WHITE);
// pad panel so as to align with other menu items
- pnlDefaultCache.setBorder(BorderFactory.createEmptyBorder(0, 19, 0, 0));
+ pnlDefaultCache.setBorder(BorderFactory.createEmptyBorder(0,
+ LEFT_BOARDER_WIDTH, 0, 0));
txtDefaultCacheSize.setPreferredSize(new Dimension(45, 20));
lblDefaultCacheSize.setText(MessageManager
.getString("label.default_cache_size"));
}
}
});
- txtDefaultCacheSize.setText(appCache.getCacheLmit(cacheKey));
+ txtDefaultCacheSize.setText(appCache.getCacheLimit(cacheKey));
pnlDefaultCache.add(lblDefaultCacheSize);
pnlDefaultCache.add(txtDefaultCacheSize);
menuItemClearCache.setText(MessageManager
@Override
public void run()
{
- int cacheLimit = appCache.updateCacheLimit(cacheKey,
- txtDefaultCacheSize.getText());
+ int userLimit = txtDefaultCacheSize.getText().trim().isEmpty() ? Integer
+ .valueOf(AppCache.DEFAULT_LIMIT) : Integer
+ .valueOf(txtDefaultCacheSize.getText());
+ int cacheLimit = appCache.updateCacheLimit(cacheKey, userLimit);
String userInput = getUserInput();
if (userInput != null && !userInput.isEmpty())
{
public void persistCache()
{
appCache.persistCache(cacheKey);
- appCache.updateCacheLimit(cacheKey, txtDefaultCacheSize.getText());
+ int userLimit = txtDefaultCacheSize.getText().trim().isEmpty() ? Integer
+ .valueOf(AppCache.DEFAULT_LIMIT) : Integer
+ .valueOf(txtDefaultCacheSize.getText());
+ appCache.updateCacheLimit(cacheKey, userLimit);
}
/**
@Test(groups = { "Functional" })
public void appCacheLimitTest()
{
- String limit = appCache.getCacheLmit(TEST_CACHE_KEY);
+ String limit = appCache.getCacheLimit(TEST_CACHE_KEY);
Assert.assertEquals(limit, "99");
- limit = String.valueOf(appCache.updateCacheLimit(TEST_CACHE_KEY, "20"));
+ limit = String.valueOf(appCache.updateCacheLimit(TEST_CACHE_KEY, 20));
Assert.assertEquals(limit, "20");
- limit = appCache.getCacheLmit(TEST_CACHE_KEY);
+ limit = appCache.getCacheLimit(TEST_CACHE_KEY);
Assert.assertEquals(limit, "20");
- appCache.updateCacheLimit(TEST_CACHE_KEY, "99");
+ appCache.updateCacheLimit(TEST_CACHE_KEY, 99);
}
- @Test(groups = { "Functional" })
- public void initCacheTest()
- {
- appCache.initCache(TEST_CACHE_KEY);
- Assert.assertTrue(appCache.getCacheItems().containsKey(TEST_CACHE_KEY));
- generateTestCacheItems();
- // appCache.
- appCache.initCache(TEST_CACHE_KEY);
- //
- Assert.assertTrue(appCache.getCacheItems().containsKey(TEST_CACHE_KEY));
- appCache.deleteCacheItems(TEST_CACHE_KEY);
- }
}
--- /dev/null
+package jalview.io.cache;
+
+import java.util.LinkedHashSet;
+
+import org.junit.Assert;
+import org.testng.annotations.BeforeClass;
+import org.testng.annotations.Test;
+
+public class JvCacheableInputBoxTest
+{
+
+ private AppCache appCache;
+
+ private static final String TEST_CACHE_KEY = "CACHE.UNIT_TEST";
+
+ private JvCacheableInputBox<String> cacheBox = new JvCacheableInputBox<String>(
+ TEST_CACHE_KEY);
+
+ @BeforeClass(alwaysRun = true)
+ private void setUpCache()
+ {
+ appCache = AppCache.getInstance();
+ }
+
+ @Test(groups = { "Functional" })
+ public void getUserInputTest()
+ {
+ String userInput = cacheBox.getUserInput();
+ Assert.assertEquals("", userInput);
+
+ String testInput = "TestInput";
+ cacheBox.addItem(testInput);
+ cacheBox.setSelectedItem(testInput);
+
+ try
+ {
+ // This 1ms delay is essential to prevent the
+ // assertion below from executing before
+ // swing thread finishes updating the combo-box
+ Thread.sleep(100);
+ } catch (InterruptedException e)
+ {
+ e.printStackTrace();
+ }
+ userInput = cacheBox.getUserInput();
+ Assert.assertEquals(testInput, userInput);
+ }
+
+ @Test(groups = { "Functional" })
+ public void updateCacheTest()
+ {
+ String testInput = "TestInput";
+ cacheBox.addItem(testInput);
+ cacheBox.setSelectedItem(testInput);
+ cacheBox.updateCache();
+ try
+ {
+ // This 1ms delay is essential to prevent the
+ // assertion below from executing before
+ // cacheBox.updateCache() finishes updating the cache
+ Thread.sleep(100);
+ } catch (InterruptedException e)
+ {
+ e.printStackTrace();
+ }
+ LinkedHashSet<String> foundCache = appCache
+ .getAllCachedItemsFor(TEST_CACHE_KEY);
+ Assert.assertTrue(foundCache.contains(testInput));
+ }
+}