76827f9a96d6093cf11570b9d5347063b63792f3
[jalview.git] / src / jalview / io / cache / AppCache.java
1 /*
2  * Jalview - A Sequence Alignment Editor and Viewer ($$Version-Rel$$)
3  * Copyright (C) $$Year-Rel$$ The Jalview Authors
4  * 
5  * This file is part of Jalview.
6  * 
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.
11  *  
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.
16  * 
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.
20  */
21 package jalview.io.cache;
22
23 import jalview.bin.ApplicationSingletonProvider;
24 import jalview.bin.ApplicationSingletonProvider.ApplicationSingletonI;
25 import jalview.bin.Cache;
26
27 import java.util.Hashtable;
28 import java.util.LinkedHashSet;
29
30 /**
31  * A singleton class used for querying and persisting cache items.
32  * 
33  * @author tcnofoegbu
34  *
35  */
36 public class AppCache implements ApplicationSingletonI
37 {
38
39   public static AppCache getInstance()
40   {
41     return (AppCache) ApplicationSingletonProvider.getInstance(AppCache.class);
42   }
43
44   public static final String DEFAULT_LIMIT = "99";
45
46   public static final String CACHE_DELIMITER = ";";
47
48   private static final String DEFAULT_LIMIT_KEY = ".DEFAULT_LIMIT";
49
50   private Hashtable<String, LinkedHashSet<String>> cacheItems;
51
52   private AppCache()
53   {
54     cacheItems = new Hashtable<String, LinkedHashSet<String>>();
55   }
56
57   /**
58    * Method to obtain all the cache items for a given cache key
59    * 
60    * @param cacheKey
61    * @return
62    */
63   public LinkedHashSet<String> getAllCachedItemsFor(String cacheKey)
64   {
65     LinkedHashSet<String> foundCache = cacheItems.get(cacheKey);
66     if (foundCache == null)
67     {
68       foundCache = new LinkedHashSet<String>();
69       cacheItems.put(cacheKey, foundCache);
70     }
71     return foundCache;
72   }
73
74   /**
75    * Method for persisting cache items for a given cache key
76    * 
77    * @param cacheKey
78    */
79   public void persistCache(String cacheKey)
80   {
81     LinkedHashSet<String> foundCacheItems = getAllCachedItemsFor(cacheKey);
82     StringBuffer delimitedCacheBuf = new StringBuffer();
83     for (String cacheItem : foundCacheItems)
84     {
85       delimitedCacheBuf.append(CACHE_DELIMITER).append(cacheItem);
86     }
87     if (delimitedCacheBuf.length() > 0)
88     {
89       delimitedCacheBuf.deleteCharAt(0);
90     }
91     String delimitedCacheString = delimitedCacheBuf.toString();
92
93     Cache.setProperty(cacheKey, delimitedCacheString);
94   }
95
96   /**
97    * Method for deleting cached items for a given cache key
98    * 
99    * @param cacheKey
100    *          the cache key
101    */
102   public void deleteCacheItems(String cacheKey)
103   {
104     cacheItems.put(cacheKey, new LinkedHashSet<String>());
105     persistCache(cacheKey);
106   }
107
108   /**
109    * Method for obtaining the preset maximum cache limit for a given cache key
110    * 
111    * @param cacheKey
112    *          the cache key
113    * @return the max number of items that could be cached
114    */
115   public String getCacheLimit(String cacheKey)
116   {
117     String uniqueKey = cacheKey + DEFAULT_LIMIT_KEY;
118     return Cache.getDefault(uniqueKey, DEFAULT_LIMIT);
119   }
120
121   /**
122    * Method for updating the preset maximum cache limit for a given cache key
123    * 
124    * @param cacheKey
125    *          the cache key
126    * @param newLimit
127    *          the max number of items that could be cached for the given cache
128    *          key
129    * @return
130    */
131   public int updateCacheLimit(String cacheKey, int newUserLimit)
132   {
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))
138     {
139       Cache.setProperty(uniqueKey, newLimit);
140       formerLimit = newLimit;
141     }
142     return Integer.valueOf(formerLimit);
143   }
144
145   /**
146    * Method for inserting cache items for given cache key into the cache data
147    * structure
148    * 
149    * @param cacheKey
150    *          the cache key
151    * @param cacheItems
152    *          the items to add to the cache
153    */
154   public void putCache(String cacheKey, LinkedHashSet<String> newCacheItems)
155   {
156     cacheItems.put(cacheKey, newCacheItems);
157   }
158
159 }