KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > ecs > storage > Hash


1 /*
2  * ====================================================================
3  *
4  * The Apache Software License, Version 1.1
5  *
6  * Copyright (c) 1999-2003 The Apache Software Foundation. All rights
7  * reserved.
8  *
9  * Redistribution and use in source and binary forms, with or without
10  * modification, are permitted provided that the following conditions
11  * are met:
12  *
13  * 1. Redistributions of source code must retain the above copyright
14  * notice, this list of conditions and the following disclaimer.
15  *
16  * 2. Redistributions in binary form must reproduce the above copyright
17  * notice, this list of conditions and the following disclaimer in
18  * the documentation and/or other materials provided with the
19  * distribution.
20  *
21  * 3. The end-user documentation included with the redistribution, if
22  * any, must include the following acknowlegement:
23  * "This product includes software developed by the
24  * Apache Software Foundation (http://www.apache.org/)."
25  * Alternately, this acknowlegement may appear in the software itself,
26  * if and wherever such third-party acknowlegements normally appear.
27  *
28  * 4. The names "The Jakarta Project", "Jakarta Element Construction Set",
29  * "Jakarta ECS" , and "Apache Software Foundation" must not be used
30  * to endorse or promote products derived
31  * from this software without prior written permission. For written
32  * permission, please contact apache@apache.org.
33  *
34  * 5. Products derived from this software may not be called "Apache",
35  * "Jakarta Element Construction Set" nor "Jakarta ECS" nor may "Apache"
36  * appear in their names without prior written permission of the Apache Group.
37  *
38  * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED
39  * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
40  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
41  * DISCLAIMED. IN NO EVENT SHALL THE APACHE SOFTWARE FOUNDATION OR
42  * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
43  * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
44  * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
45  * USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
46  * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
47  * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
48  * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
49  * SUCH DAMAGE.
50  * ====================================================================
51  *
52  * This software consists of voluntary contributions made by many
53  * individuals on behalf of the Apache Software Foundation. For more
54  * information on the Apache Software Foundation, please see
55  * <http://www.apache.org/>.
56  *
57  */

58
59 package org.apache.ecs.storage;
60
61 import java.util.Enumeration JavaDoc;
62 import java.util.Vector JavaDoc;
63
64 public class Hash implements java.io.Serializable JavaDoc
65 {
66     private Array keys = new Array();
67     private Array elements = new Array();
68
69     public Hash()
70     {
71     }
72
73     public void setSize(int newSize)
74     {
75         keys.setSize(newSize);
76         elements.setSize(newSize);
77     }
78
79     public void setGrow(int growBy)
80     {
81         keys.setGrow(growBy);
82         elements.setGrow(growBy);
83     }
84
85     public synchronized void put(String JavaDoc key,Object JavaDoc element)
86     {
87         try
88         {
89             if( containsKey(key) )
90             {
91                 elements.add(keys.location(key),element);
92             }
93             else
94             {
95                 keys.add( key );
96                 elements.add(element);
97             }
98         }
99         catch(org.apache.ecs.storage.NoSuchObjectException nsoe)
100         {
101         }
102     }
103
104     public synchronized void remove(String JavaDoc key)
105     {
106         try
107         {
108             if(containsKey(key))
109             {
110                 elements.remove(keys.location(key));
111                 elements.remove(elements.location(key));
112             }
113         }
114         catch(org.apache.ecs.storage.NoSuchObjectException nsoe)
115         {
116         }
117     }
118
119     public int size()
120     {
121         return keys.getCurrentSize();
122     }
123
124     public boolean contains(Object JavaDoc element)
125     {
126         try
127         {
128             elements.location(element);
129             return(true);
130         }
131         catch(org.apache.ecs.storage.NoSuchObjectException noSuchObject)
132         {
133             return false;
134         }
135     }
136
137     public Enumeration JavaDoc keys()
138     {
139         return keys;
140     }
141
142     public boolean containsKey(String JavaDoc key)
143     {
144         try
145         {
146             keys.location(key);
147         }
148         catch(org.apache.ecs.storage.NoSuchObjectException noSuchObject)
149         {
150             return false;
151         }
152         return(true);
153     }
154
155     public Enumeration JavaDoc elements()
156     {
157         return elements;
158     }
159
160     public Object JavaDoc get(String JavaDoc key)
161     {
162         try
163         {
164             if( containsKey(key) )
165                 return(elements.get(keys.location(key)));
166         }
167         catch(org.apache.ecs.storage.NoSuchObjectException nsoe)
168         {
169         }
170         return null;
171     }
172 }
173
Popular Tags