1 /***
2 * HashCache.java
3 *
4 * This file is part of the creme library.
5 * The creme library intends to ease the development effort of large
6 * applications by providing easy to use support classes.
7 *
8 * Copyright (C) 2002 Denis Bregeon
9 *
10 * This library is free software; you can redistribute it and/or
11 * modify it under the terms of the GNU Lesser General Public
12 * License as published by the Free Software Foundation; either
13 * version 2.1 of the License, or (at your option) any later version.
14 *
15 * This library is distributed in the hope that it will be useful,
16 * but WITHOUT ANY WARRANTY; without even the implied warranty of
17 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
18 * Lesser General Public License for more details.
19 *
20 * You should have received a copy of the GNU Lesser General Public
21 * License along with this library; if not, write to the Free Software
22 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
23 *
24 * contact information: dbregeon@sourceforge.net
25 */
26 package org.jcreme.caches;
27
28 import java.util.HashMap;
29 import java.util.Map;
30
31 /***
32 * A Cache object using no particular policy. This implementation is based on a
33 * Hashtable.
34 *
35 * @author $Author: dbregeon $
36 * @version $Revision: 1.3 $
37 */
38 public class HashCache extends BaseCache {
39 /***
40 * Stores the values registered in the cache.
41 */
42 private final HashMap values;
43
44 /***
45 * Creates new HashCache
46 *
47 * @param minSize
48 * the initial size of the Cache.
49 * @param maxSize
50 * the maximum number of objects contained by the cache.
51 * @param policy
52 * the policy to use in this cache. It is not implemented in this
53 * class.
54 */
55 protected HashCache(int minSize, int maxSize, CachePolicy policy) {
56 super(minSize, maxSize, policy);
57 this.values = new HashMap(minSize);
58 }
59
60 /***
61 * Creates new HashCache
62 *
63 * @param policy
64 * the policy to use in this cache.
65 */
66 protected HashCache(CachePolicy policy) {
67 super(policy);
68 this.values = new HashMap(0);
69 }
70
71 /***
72 * Creates new HashCache
73 */
74 public HashCache() {
75 super();
76 this.values = new HashMap(0);
77 }
78
79 /***
80 * This method adds an object in the cache.
81 *
82 * @param key
83 * the key that will be used to retrieve the object.
84 * @param value
85 * the object to store in the cache.
86 * @throws CacheFullException
87 * if the object could not be stored in the cache.
88 */
89 public synchronized void registerObject(final Object key, final Object value)
90 throws CacheFullException {
91 if (this.values.size() >= getMaxSize()) {
92 throw new CacheFullException("The HashCache object is full.");
93 }
94 this.values.put(key, value);
95 fireElementAdded(buildEvent(value));
96 }
97
98 /***
99 * This method removes an object from the cache.
100 *
101 * @param key
102 * the key that was used to store the object in the cache.
103 */
104 public synchronized void unregisterObject(final Object key) {
105 final Object value = this.values.remove(key);
106 fireElementRemoved(buildEvent(value));
107 }
108
109 /***
110 * Removes all the objects from the Cache, leaving it empty.
111 */
112 public synchronized void clear() {
113 this.values.clear();
114 fireElementRemoved(buildEvent(null));
115 }
116
117 /***
118 * This methods gives the current size of the cache.
119 *
120 * @return the number of objects currently stored in the cache.
121 */
122 public synchronized int getSize() {
123 return this.values.size();
124 }
125
126 /***
127 * Gives access to an object registered in the cache.
128 *
129 * @param key
130 * the key used to register the searched object.
131 * @return the object stored in the cache. Null if no object was stored with
132 * this key.
133 */
134 public synchronized Object getObject(final Object key) {
135 Object result = null;
136 if (key != null) {
137 result = this.values.get(key);
138 }
139 return result;
140 }
141
142 /***
143 * This method gives access to the full content of the cache.
144 *
145 * @return an array containing all the objects stored in the cache.
146 */
147 public synchronized Object[] getAllObjects() {
148 return this.values.values().toArray();
149 }
150
151 /***
152 * This method gives the same result as the getAllObjects method but the
153 * objects in the result array are types with the parameter type.
154 *
155 * @param type
156 * the type to give to the object in the result array.
157 * @return an array of objects of the type.
158 */
159 public synchronized Object[] getAllObjects(final Class type) {
160 return this.values.values().toArray(
161 (Object[]) java.lang.reflect.Array.newInstance(type,
162 this.values.size()));
163 }
164
165 /***
166 * This method enables to access to the cache contents.
167 *
168 * @return a map of the (key,value) contents of the cache.
169 */
170 public synchronized Map getMap() {
171 return (Map) this.values.clone();
172 }
173
174 /***
175 * This is a convenience method. It enables to add to the cache all the
176 * objects present in the map.
177 *
178 * @param m
179 * the map that contains the objects (and keys) to use.
180 */
181 public synchronized void registerAllObjects(final Map m) {
182 if (m != null) {
183 this.values.putAll(m);
184 }
185 }
186 }