2 * Jalview - A Sequence Alignment Editor and Viewer ($$Version-Rel$$)
3 * Copyright (C) $$Year-Rel$$ The Jalview Authors
5 * This file is part of Jalview.
7 * Jalview is free software: you can redistribute it and/or
8 * modify it under the terms of the GNU General Public License
9 * as published by the Free Software Foundation, either version 3
10 * of the License, or (at your option) any later version.
12 * Jalview is distributed in the hope that it will be useful, but
13 * WITHOUT ANY WARRANTY; without even the implied warranty
14 * of MERCHANTABILITY or FITNESS FOR A PARTICULAR
15 * PURPOSE. See the GNU General Public License for more details.
17 * You should have received a copy of the GNU General Public License
18 * along with Jalview. If not, see <http://www.gnu.org/licenses/>.
19 * The Jalview Authors are detailed in the 'AUTHORS' file.
21 package jalview.io.cache;
23 import jalview.bin.Cache;
25 import java.util.Hashtable;
26 import java.util.LinkedHashSet;
29 * A singleton class used for querying and persisting cache items.
36 public static final String DEFAULT_LIMIT = "99";
38 public static final String CACHE_DELIMITER = ";";
40 private static AppCache instance = null;
42 private static final String DEFAULT_LIMIT_KEY = ".DEFAULT_LIMIT";
44 private Hashtable<String, LinkedHashSet<String>> cacheItems;
48 cacheItems = new Hashtable<String, LinkedHashSet<String>>();
52 * Method to obtain all the cache items for a given cache key
57 public LinkedHashSet<String> getAllCachedItemsFor(String cacheKey)
59 LinkedHashSet<String> foundCache = cacheItems.get(cacheKey);
60 if (foundCache == null)
62 foundCache = new LinkedHashSet<String>();
63 cacheItems.put(cacheKey, foundCache);
69 * Returns a singleton instance of AppCache
73 public static AppCache getInstance()
77 instance = new AppCache();
83 * Method for persisting cache items for a given cache key
87 public void persistCache(String cacheKey)
89 LinkedHashSet<String> foundCacheItems = getAllCachedItemsFor(cacheKey);
90 StringBuffer delimitedCacheBuf = new StringBuffer();
91 for (String cacheItem : foundCacheItems)
93 delimitedCacheBuf.append(CACHE_DELIMITER).append(cacheItem);
95 if (delimitedCacheBuf.length() > 0)
97 delimitedCacheBuf.deleteCharAt(0);
99 String delimitedCacheString = delimitedCacheBuf.toString();
101 Cache.setProperty(cacheKey, delimitedCacheString);
105 * Method for deleting cached items for a given cache key
110 public void deleteCacheItems(String cacheKey)
112 cacheItems.put(cacheKey, new LinkedHashSet<String>());
113 persistCache(cacheKey);
117 * Method for obtaining the preset maximum cache limit for a given cache key
121 * @return the max number of items that could be cached
123 public String getCacheLimit(String cacheKey)
125 String uniqueKey = cacheKey + DEFAULT_LIMIT_KEY;
126 return Cache.getDefault(uniqueKey, DEFAULT_LIMIT);
130 * Method for updating the preset maximum cache limit for a given cache key
135 * the max number of items that could be cached for the given cache
139 public int updateCacheLimit(String cacheKey, int newUserLimit)
141 String newLimit = String.valueOf(newUserLimit);
142 String uniqueKey = cacheKey + DEFAULT_LIMIT_KEY;
143 String formerLimit = getCacheLimit(cacheKey);
144 if (newLimit != null && !newLimit.isEmpty()
145 && !formerLimit.equals(newLimit))
147 Cache.setProperty(uniqueKey, newLimit);
148 formerLimit = newLimit;
150 return Integer.valueOf(formerLimit);
154 * Method for inserting cache items for given cache key into the cache data
160 * the items to add to the cache
162 public void putCache(String cacheKey, LinkedHashSet<String> newCacheItems)
164 cacheItems.put(cacheKey, newCacheItems);