@Override
public void run()
{
- int cacheLimit = Integer.parseInt(appCache.getCacheLimit(cacheKey));
- String userInput = getUserInput();
- if (userInput != null && !userInput.isEmpty())
- {
- LinkedHashSet<String> foundCache = appCache
- .getAllCachedItemsFor(cacheKey);
- // remove old cache item so as to place current input at the top of
- // the result
- foundCache.remove(userInput);
- foundCache.add(userInput);
- appCache.putCache(cacheKey, foundCache);
- }
+ updateCacheNow();
+ }
+ });
+ }
- String lastSearch = userInput;
- if (comboBox.getItemCount() > 0)
- {
- comboBox.removeAllItems();
- }
- Set<String> cacheItems = appCache.getAllCachedItemsFor(cacheKey);
- List<String> reversedCacheItems = new ArrayList<>();
- reversedCacheItems.addAll(cacheItems);
- cacheItems = null;
- Collections.reverse(reversedCacheItems);
- if (lastSearch.isEmpty())
- {
- comboBox.addItem("");
- }
+ /**
+ * Updates the cached values to include the last user input as the most
+ * recently added (without duplication), and rebuilds the dropdown list with
+ * this as the first entry. If the maximum configured cache size would be
+ * exceeded, the oldest entry is removed.
+ */
+ void updateCacheNow()
+ {
+ int cacheLimit = Integer.valueOf(appCache.getCacheLimit(cacheKey));
+ String userInput = getUserInput();
+ if (userInput != null && !userInput.isEmpty())
+ {
+ LinkedHashSet<String> foundCache = appCache
+ .getAllCachedItemsFor(cacheKey);
+ // remove old cache item so as to place current input at the top of
+ // the result
+ foundCache.remove(userInput);
+ foundCache.add(userInput);
+ appCache.putCache(cacheKey, foundCache);
+ }
- if (reversedCacheItems != null && !reversedCacheItems.isEmpty())
+ String lastSearch = userInput;
+ if (comboBox.getItemCount() > 0)
+ {
+ comboBox.removeAllItems();
+ }
+ Set<String> cacheItems = appCache.getAllCachedItemsFor(cacheKey);
+ List<String> reversedCacheItems = new ArrayList<>();
+ reversedCacheItems.addAll(cacheItems);
+ cacheItems = null;
+ Collections.reverse(reversedCacheItems);
+ if (lastSearch.isEmpty())
+ {
+ comboBox.addItem("");
+ }
+
+ if (reversedCacheItems != null && !reversedCacheItems.isEmpty())
+ {
+ LinkedHashSet<String> foundCache = appCache
+ .getAllCachedItemsFor(cacheKey);
+ boolean prune = reversedCacheItems.size() > cacheLimit;
+ int count = 1;
+ boolean limitExceeded = false;
+ for (String cacheItem : reversedCacheItems)
+ {
+ limitExceeded = (count++ > cacheLimit);
+ if (prune)
{
- LinkedHashSet<String> foundCache = appCache
- .getAllCachedItemsFor(cacheKey);
- boolean prune = reversedCacheItems.size() > cacheLimit;
- int count = 1;
- boolean limitExceeded = false;
- for (String cacheItem : reversedCacheItems)
+ if (limitExceeded)
{
- limitExceeded = (count++ > cacheLimit);
- if (prune)
- {
- if (limitExceeded)
- {
- foundCache.remove(cacheItem);
- }
- else
- {
- comboBox.addItem(cacheItem);
- }
- }
- else
- {
- comboBox.addItem(cacheItem);
- }
+ foundCache.remove(cacheItem);
}
- appCache.putCache(cacheKey, foundCache);
+ else
+ {
+ comboBox.addItem(cacheItem);
+ }
+ }
+ else
+ {
+ comboBox.addItem(cacheItem);
}
- setSelectedItem(lastSearch.isEmpty() ? "" : lastSearch);
}
- });
+ appCache.putCache(cacheKey, foundCache);
+ }
+ setSelectedItem(lastSearch.isEmpty() ? "" : lastSearch);
+
}
/**