Clover icon

Coverage Report

  1. Project Clover database Thu Nov 7 2024 17:01:39 GMT
  2. Package jalview.io.cache

File AppCache.java

 

Coverage histogram

../../../img/srcFileCovDistChart0.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
0.00%
 

Contributing tests

No tests hitting this source file were found.

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