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