A MapCache is a Cache Cache implementation that uses a backing Map instance to store and retrieve cached data. @param K @param V @since 1.0
| 32 | * @since 1.0 |
| 33 | */ |
| 34 | public class MapCache<K, V> implements Cache<K, V> { |
| 35 | |
| 36 | /** |
| 37 | * Backing instance. |
| 38 | */ |
| 39 | private final Map<K, V> map; |
| 40 | |
| 41 | /** |
| 42 | * The name of this cache. |
| 43 | */ |
| 44 | private final String name; |
| 45 | |
| 46 | public MapCache(String name, Map<K, V> backingMap) { |
| 47 | if (name == null) { |
| 48 | throw new IllegalArgumentException("Cache name cannot be null."); |
| 49 | } |
| 50 | if (backingMap == null) { |
| 51 | throw new IllegalArgumentException("Backing map cannot be null."); |
| 52 | } |
| 53 | this.name = name; |
| 54 | this.map = backingMap; |
| 55 | } |
| 56 | |
| 57 | public V get(K key) throws CacheException { |
| 58 | return map.get(key); |
| 59 | } |
| 60 | |
| 61 | public V put(K key, V value) throws CacheException { |
| 62 | return map.put(key, value); |
| 63 | } |
| 64 | |
| 65 | public V remove(K key) throws CacheException { |
| 66 | return map.remove(key); |
| 67 | } |
| 68 | |
| 69 | public void clear() throws CacheException { |
| 70 | map.clear(); |
| 71 | } |
| 72 | |
| 73 | public int size() { |
| 74 | return map.size(); |
| 75 | } |
| 76 | |
| 77 | public Set<K> keys() { |
| 78 | Set<K> keys = map.keySet(); |
| 79 | if (!keys.isEmpty()) { |
| 80 | return Collections.unmodifiableSet(keys); |
| 81 | } |
| 82 | return Collections.emptySet(); |
| 83 | } |
| 84 | |
| 85 | public Collection<V> values() { |
| 86 | Collection<V> values = map.values(); |
| 87 | if (!values.isEmpty()) { |
| 88 | return Collections.unmodifiableCollection(values); |
| 89 | } |
| 90 | return Collections.emptyList(); |
| 91 | } |
nothing calls this directly
no outgoing calls
no test coverage detected