Source code: com/tripi/asp/util/CacheMap.java
1 /**
2 * ArrowHead ASP Server
3 * This is a source file for the ArrowHead ASP Server - an 100% Java
4 * VBScript interpreter and ASP server.
5 *
6 * For more information, see http://www.tripi.com/arrowhead
7 *
8 * Copyright (C) 2002 Terence Haddock
9 *
10 * This program is free software; you can redistribute it and/or modify
11 * it under the terms of the GNU General Public License as published by
12 * the Free Software Foundation; either version 2 of the License, or
13 * (at your option) any later version.
14 *
15 * This program 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
18 * GNU General Public License for more details.
19 *
20 * You should have received a copy of the GNU General Public License
21 * along with this program; if not, write to the Free Software
22 * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
23 *
24 */
25 package com.tripi.asp.util;
26
27 import java.util.Vector;
28 import java.util.Hashtable;
29 import java.util.WeakHashMap;
30
31 /**
32 * This class implements a "cache" for storing a fixed maximum number
33 * of objects.
34 *
35 * @author Terence Haddock
36 * @version 0.9
37 */
38 public class CacheMap
39 {
40 /**
41 * This is a WeakHashMap reference to all of the values in this map.
42 */
43 WeakHashMap allValues = new WeakHashMap();
44
45 /** The maximum number of values to hold */
46 int maxValues;
47
48 /** A queue of the active values in this map */
49 Vector activeValues;
50
51 /** A hashtable for existance test */
52 Hashtable exists = new Hashtable();
53
54 /**
55 * Constructor
56 * @param maxValues maximum number of values.
57 */
58 public CacheMap(int maxValues)
59 {
60 activeValues = new Vector();
61 }
62
63 /**
64 * Obtain a value in the hash
65 * @param key
66 * @return value
67 */
68 public Object get(Object key)
69 {
70 Object value = allValues.get(key);
71 return value;
72 }
73
74 /**
75 * Put a key in the hash
76 * @param key
77 * @param value
78 */
79 public void put(Object key, Object value)
80 {
81 allValues.put(key, value);
82 if (!exists.containsKey(key)) {
83 exists.put(key, value);
84 activeValues.add(key);
85 if (activeValues.size() > maxValues)
86 {
87 Object removeKey = activeValues.get(0);
88 exists.remove(removeKey);
89 activeValues.remove(0);
90 }
91 }
92 }
93 }