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.ApplicationSingletonProvider;
24 import jalview.bin.ApplicationSingletonProvider.ApplicationSingletonI;
25 import jalview.bin.Cache;
27 import java.util.Hashtable;
28 import java.util.LinkedHashSet;
31 * A singleton class used for querying and persisting cache items.
36 public class AppCache implements ApplicationSingletonI
39 public static AppCache getInstance()
41 return (AppCache) ApplicationSingletonProvider.getInstance(AppCache.class);
44 public static final String DEFAULT_LIMIT = "99";
46 public static final String CACHE_DELIMITER = ";";
48 private static final String DEFAULT_LIMIT_KEY = ".DEFAULT_LIMIT";
50 private Hashtable<String, LinkedHashSet<String>> cacheItems;
54 cacheItems = new Hashtable<String, LinkedHashSet<String>>();
58 * Method to obtain all the cache items for a given cache key
63 public LinkedHashSet<String> getAllCachedItemsFor(String cacheKey)
65 LinkedHashSet<String> foundCache = cacheItems.get(cacheKey);
66 if (foundCache == null)
68 foundCache = new LinkedHashSet<String>();
69 cacheItems.put(cacheKey, foundCache);
75 * Method for persisting cache items for a given cache key
79 public void persistCache(String cacheKey)
81 LinkedHashSet<String> foundCacheItems = getAllCachedItemsFor(cacheKey);
82 StringBuffer delimitedCacheBuf = new StringBuffer();
83 for (String cacheItem : foundCacheItems)
85 delimitedCacheBuf.append(CACHE_DELIMITER).append(cacheItem);
87 if (delimitedCacheBuf.length() > 0)
89 delimitedCacheBuf.deleteCharAt(0);
91 String delimitedCacheString = delimitedCacheBuf.toString();
93 Cache.setProperty(cacheKey, delimitedCacheString);
97 * Method for deleting cached items for a given cache key
102 public void deleteCacheItems(String cacheKey)
104 cacheItems.put(cacheKey, new LinkedHashSet<String>());
105 persistCache(cacheKey);
109 * Method for obtaining the preset maximum cache limit for a given cache key
113 * @return the max number of items that could be cached
115 public String getCacheLimit(String cacheKey)
117 String uniqueKey = cacheKey + DEFAULT_LIMIT_KEY;
118 return Cache.getDefault(uniqueKey, DEFAULT_LIMIT);
122 * Method for updating the preset maximum cache limit for a given cache key
127 * the max number of items that could be cached for the given cache
131 public int updateCacheLimit(String cacheKey, int newUserLimit)
133 String newLimit = String.valueOf(newUserLimit);
134 String uniqueKey = cacheKey + DEFAULT_LIMIT_KEY;
135 String formerLimit = getCacheLimit(cacheKey);
136 if (newLimit != null && !newLimit.isEmpty()
137 && !formerLimit.equals(newLimit))
139 Cache.setProperty(uniqueKey, newLimit);
140 formerLimit = newLimit;
142 return Integer.valueOf(formerLimit);
146 * Method for inserting cache items for given cache key into the cache data
152 * the items to add to the cache
154 public void putCache(String cacheKey, LinkedHashSet<String> newCacheItems)
156 cacheItems.put(cacheKey, newCacheItems);