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