1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23 package de.bea.domingo.cache;
24
25 import java.io.Serializable;
26 import java.util.Map;
27
28 /***
29 * Simple cache implementation using a HashMap.
30 *
31 * <b>Note that this implementation is not synchronized.</b> If multiple
32 * threads access this cache concurrently, and at least one of the threads
33 * modifies the cache structurally, it <i>must</i> be synchronized externally.
34 * (A structural modification is any operation that adds or deletes one or
35 * more mappings; merely changing the value associated with a key that an
36 * instance already contains is not a structural modification.) This is
37 * typically accomplished by synchronizing on some object that naturally
38 * encapsulates the map. If no such object exists, the map should be
39 * "wrapped" using the <tt>Collections.synchronizedMap</tt> method. This is
40 * best done at creation time, to prevent accidental unsynchronized access to
41 * the map: <pre> Map map = Collections.synchronizedMap(new HashMap(...));
42 * </pre><p>
43 *
44 *
45 * @author <a href=mailto:kriede@users.sourceforge.net>Kurt Riede</a>
46 */
47 public abstract class AbstractBaseCache implements Cache, Serializable {
48
49 /***
50 * the cache.
51 *
52 * <p>key -> object</p>
53 */
54 private final Map map;
55
56 /***
57 * Constructor.
58 */
59 public AbstractBaseCache() {
60 super();
61 map = createMap();
62 }
63
64 /***
65 * Protected getter for map attribute to allow concrete classes to access
66 * the map.
67 *
68 * @return the internal map
69 */
70 protected final Map getMap() {
71 return map;
72 }
73
74 /***
75 * Creates the map to be used with the cache.
76 *
77 * <p>Concrete classes must implement this method and create
78 * a concrete map for the cache.</p>
79 *
80 * @return a map for the cache
81 */
82 protected abstract Map createMap();
83
84 /***
85 * {@inheritDoc}
86 * @see de.bea.domingo.cache.Cache#size()
87 */
88 public final synchronized int size() {
89 return map.size();
90 }
91 }