KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > excalibur > store > impl > MemoryStore


1 /*
2  * Copyright 2002-2004 The Apache Software Foundation
3  * Licensed under the Apache License, Version 2.0 (the "License");
4  * you may not use this file except in compliance with the License.
5  * You may obtain a copy of the License at
6  *
7  * http://www.apache.org/licenses/LICENSE-2.0
8  *
9  * Unless required by applicable law or agreed to in writing, software
10  * distributed under the License is distributed on an "AS IS" BASIS,
11  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
12  * implied.
13  *
14  * See the License for the specific language governing permissions and
15  * limitations under the License.
16  */

17 package org.apache.excalibur.store.impl;
18
19 import java.util.Enumeration JavaDoc;
20 import java.util.Hashtable JavaDoc;
21
22 import org.apache.avalon.framework.logger.AbstractLogEnabled;
23 import org.apache.avalon.framework.thread.ThreadSafe;
24 import org.apache.excalibur.store.Store;
25
26 /**
27  *
28  * @avalon.component
29  * @avalon.service type=Store
30  * @x-avalon.info name=mem-store
31  * @x-avalon.lifestyle type=singleton
32  *
33  * @author <a HREF="mailto:dev@avalon.apache.org">Avalon Development Team</a>
34  * @version CVS $Id: MemoryStore.java,v 1.4 2004/02/28 11:47:31 cziegeler Exp $
35  */

36 public class MemoryStore
37     extends AbstractLogEnabled
38     implements Store, ThreadSafe {
39
40     /* WARNING: Hashtable is threadsafe, whereas HashMap is not.
41      * Should we move this class over to the Collections API,
42      * use Collections.synchronizedMap(Map map) to ensure
43      * accesses are synchronized.
44      */

45
46     /** The shared store */
47     protected Hashtable JavaDoc m_table = new Hashtable JavaDoc();
48
49     /**
50      * Get the object associated to the given unique key.
51      */

52     public Object JavaDoc get(Object JavaDoc key)
53     {
54         return m_table.get(key);
55     }
56
57     /**
58      * Store the given object in a persistent state. It is up to the
59      * caller to ensure that the key has a persistent state across
60      * different JVM executions.
61      */

62     public void store(Object JavaDoc key, Object JavaDoc value)
63     {
64         m_table.put(key,value);
65     }
66
67     /**
68      * Remove the object associated to the given key.
69      */

70     public void remove(Object JavaDoc key)
71     {
72         m_table.remove(key);
73     }
74
75     /**
76      * Clear the Store of all elements
77      */

78     public void clear()
79     {
80         m_table.clear();
81     }
82
83     public void free() {}
84
85     /**
86      * Indicates if the given key is associated to a contained object.
87      */

88     public boolean containsKey(Object JavaDoc key)
89     {
90         return m_table.containsKey(key);
91     }
92
93     /**
94      * Returns the list of used keys as an Enumeration of Objects.
95      */

96     public Enumeration JavaDoc keys()
97     {
98         return m_table.keys();
99     }
100
101     /**
102      * Returns count of the objects in the store, or -1 if could not be
103      * obtained.
104      */

105     public int size()
106     {
107         return m_table.size();
108     }
109 }
110
Popular Tags