Clover icon

Coverage Report

  1. Project Clover database Thu Dec 4 2025 14:43:25 GMT
  2. Package jalview.io.cache

File AppCache.java

 

Coverage histogram

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

Code metrics

6
27
8
1
160
73
13
0.48
3.38
8
1.62

Classes

Class Line # Actions
AppCache 36 27 13
0.975609897.6%
 

Contributing tests

This file is covered by 8 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.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  11 toggle public static AppCache getInstance()
40    {
41  11 return ApplicationSingletonProvider.getInstance(AppCache.class);
42    }
43   
 
44  4 toggle private AppCache()
45    {
46  4 cacheItems = new Hashtable<String, LinkedHashSet<String>>();
47    }
48   
49    public static final String DEFAULT_LIMIT = "99";
50   
51    public static final String CACHE_DELIMITER = ";";
52   
53    private static final String DEFAULT_LIMIT_KEY = ".DEFAULT_LIMIT";
54   
55    private Hashtable<String, LinkedHashSet<String>> cacheItems;
56   
57   
58    /**
59    * Method to obtain all the cache items for a given cache key
60    *
61    * @param cacheKey
62    * @return
63    */
 
64  23 toggle public LinkedHashSet<String> getAllCachedItemsFor(String cacheKey)
65    {
66  23 LinkedHashSet<String> foundCache = cacheItems.get(cacheKey);
67  23 if (foundCache == null)
68    {
69  8 foundCache = new LinkedHashSet<String>();
70  8 cacheItems.put(cacheKey, foundCache);
71    }
72  23 return foundCache;
73    }
74   
75    /**
76    * Method for persisting cache items for a given cache key
77    *
78    * @param cacheKey
79    */
 
80  6 toggle public void persistCache(String cacheKey)
81    {
82  6 LinkedHashSet<String> foundCacheItems = getAllCachedItemsFor(cacheKey);
83  6 StringBuffer delimitedCacheBuf = new StringBuffer();
84  6 for (String cacheItem : foundCacheItems)
85    {
86  10 delimitedCacheBuf.append(CACHE_DELIMITER).append(cacheItem);
87    }
88  6 if (delimitedCacheBuf.length() > 0)
89    {
90  1 delimitedCacheBuf.deleteCharAt(0);
91    }
92  6 String delimitedCacheString = delimitedCacheBuf.toString();
93   
94  6 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  1 toggle public void deleteCacheItems(String cacheKey)
104    {
105  1 cacheItems.put(cacheKey, new LinkedHashSet<String>());
106  1 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  15 toggle public String getCacheLimit(String cacheKey)
117    {
118  15 String uniqueKey = cacheKey + DEFAULT_LIMIT_KEY;
119  15 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  2 toggle public int updateCacheLimit(String cacheKey, int newUserLimit)
133    {
134  2 String newLimit = String.valueOf(newUserLimit);
135  2 String uniqueKey = cacheKey + DEFAULT_LIMIT_KEY;
136  2 String formerLimit = getCacheLimit(cacheKey);
137  2 if (newLimit != null && !newLimit.isEmpty()
138    && !formerLimit.equals(newLimit))
139    {
140  2 Cache.setProperty(uniqueKey, newLimit);
141  2 formerLimit = newLimit;
142    }
143  2 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  3 toggle public void putCache(String cacheKey, LinkedHashSet<String> newCacheItems)
156    {
157  3 cacheItems.put(cacheKey, newCacheItems);
158    }
159   
160    }