Clover icon

jalviewX

  1. Project Clover database Wed Oct 31 2018 15:13:58 GMT
  2. Package jalview.io.cache

File AppCache.java

 

Coverage histogram

../../../img/srcFileCovDistChart10.png
0% of files have more coverage

Code metrics

8
29
8
1
167
76
14
0.48
3.62
8
1.75

Classes

Class Line # Actions
AppCache 34 29 14 1
0.977777897.8%
 

Contributing tests

This file is covered by 5 tests. .

Source view

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  1 toggle private AppCache()
47    {
48  1 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  17 toggle public LinkedHashSet<String> getAllCachedItemsFor(String cacheKey)
58    {
59  17 LinkedHashSet<String> foundCache = cacheItems.get(cacheKey);
60  17 if (foundCache == null)
61    {
62  4 foundCache = new LinkedHashSet<String>();
63  4 cacheItems.put(cacheKey, foundCache);
64    }
65  17 return foundCache;
66    }
67   
68    /**
69    * Returns a singleton instance of AppCache
70    *
71    * @return
72    */
 
73  6 toggle public static AppCache getInstance()
74    {
75  6 if (instance == null)
76    {
77  1 instance = new AppCache();
78    }
79  6 return instance;
80    }
81   
82    /**
83    * Method for persisting cache items for a given cache key
84    *
85    * @param cacheKey
86    */
 
87  4 toggle public void persistCache(String cacheKey)
88    {
89  4 LinkedHashSet<String> foundCacheItems = getAllCachedItemsFor(cacheKey);
90  4 StringBuffer delimitedCacheBuf = new StringBuffer();
91  4 for (String cacheItem : foundCacheItems)
92    {
93  10 delimitedCacheBuf.append(CACHE_DELIMITER).append(cacheItem);
94    }
95  4 if (delimitedCacheBuf.length() > 0)
96    {
97  1 delimitedCacheBuf.deleteCharAt(0);
98    }
99  4 String delimitedCacheString = delimitedCacheBuf.toString();
100   
101  4 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  1 toggle public void deleteCacheItems(String cacheKey)
111    {
112  1 cacheItems.put(cacheKey, new LinkedHashSet<String>());
113  1 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  11 toggle public String getCacheLimit(String cacheKey)
124    {
125  11 String uniqueKey = cacheKey + DEFAULT_LIMIT_KEY;
126  11 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  2 toggle public int updateCacheLimit(String cacheKey, int newUserLimit)
140    {
141  2 String newLimit = String.valueOf(newUserLimit);
142  2 String uniqueKey = cacheKey + DEFAULT_LIMIT_KEY;
143  2 String formerLimit = getCacheLimit(cacheKey);
144  2 if (newLimit != null && !newLimit.isEmpty()
145    && !formerLimit.equals(newLimit))
146    {
147  2 Cache.setProperty(uniqueKey, newLimit);
148  2 formerLimit = newLimit;
149    }
150  2 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  3 toggle public void putCache(String cacheKey, LinkedHashSet<String> newCacheItems)
163    {
164  3 cacheItems.put(cacheKey, newCacheItems);
165    }
166   
167    }