1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19 package org.apache.shiro.cache;
20
21 import java.util.Collection;
22 import java.util.Collections;
23 import java.util.Map;
24 import java.util.Set;
25
26
27
28
29
30
31
32 public class MapCache<K, V> implements Cache<K, V> {
33
34
35
36
37 private final Map<K, V> map;
38
39
40
41
42 private final String name;
43
44 public MapCache(String name, Map<K, V> backingMap) {
45 if (name == null) {
46 throw new IllegalArgumentException("Cache name cannot be null.");
47 }
48 if (backingMap == null) {
49 throw new IllegalArgumentException("Backing map cannot be null.");
50 }
51 this.name = name;
52 this.map = backingMap;
53 }
54
55 public V get(K key) throws CacheException {
56 return map.get(key);
57 }
58
59 public V put(K key, V value) throws CacheException {
60 return map.put(key, value);
61 }
62
63 public V remove(K key) throws CacheException {
64 return map.remove(key);
65 }
66
67 public void clear() throws CacheException {
68 map.clear();
69 }
70
71 public int size() {
72 return map.size();
73 }
74
75 public Set<K> keys() {
76 Set<K> keys = map.keySet();
77 if (!keys.isEmpty()) {
78 return Collections.unmodifiableSet(keys);
79 }
80 return Collections.emptySet();
81 }
82
83 public Collection<V> values() {
84 Collection<V> values = map.values();
85 if (!map.isEmpty()) {
86 return Collections.unmodifiableCollection(values);
87 }
88 return Collections.emptySet();
89 }
90
91 public String toString() {
92 return new StringBuilder("MapCache '")
93 .append(name).append("' (")
94 .append(map.size())
95 .append(" entries)")
96 .toString();
97 }
98 }