KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > batik > css > engine > value > StringMap


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

18 package org.apache.batik.css.engine.value;
19
20 /**
21  * A simple hashtable, not synchronized, with fixed load factor and with
22  * equality test made with '=='.
23  *
24  * @author <a HREF="mailto:stephane@hillion.org">Stephane Hillion</a>
25  * @version $Id: StringMap.java,v 1.3 2004/08/18 07:12:53 vhardy Exp $
26  */

27 public class StringMap {
28
29     /**
30      * The initial capacity
31      */

32     protected final static int INITIAL_CAPACITY = 11;
33
34     /**
35      * The underlying array
36      */

37     protected Entry[] table;
38         
39     /**
40      * The number of entries
41      */

42     protected int count;
43         
44     /**
45      * Creates a new table.
46      */

47     public StringMap() {
48     table = new Entry[INITIAL_CAPACITY];
49     }
50
51     /**
52      * Creates a copy of the given StringMap object.
53      * @param t The table to copy.
54      */

55     public StringMap(StringMap t) {
56     count = t.count;
57     table = new Entry[t.table.length];
58     for (int i = 0; i < table.length; i++) {
59         Entry e = t.table[i];
60         Entry n = null;
61         if (e != null) {
62         n = new Entry(e.hash, e.key, e.value, null);
63         table[i] = n;
64         e = e.next;
65         while (e != null) {
66             n.next = new Entry(e.hash, e.key, e.value, null);
67             n = n.next;
68             e = e.next;
69         }
70         }
71     }
72     }
73
74     /**
75      * Gets the value corresponding to the given string.
76      * @return the value or null
77      */

78     public Object JavaDoc get(String JavaDoc key) {
79     int hash = key.hashCode() & 0x7FFFFFFF;
80     int index = hash % table.length;
81     
82     for (Entry e = table[index]; e != null; e = e.next) {
83         if ((e.hash == hash) && e.key == key) {
84         return e.value;
85         }
86     }
87     return null;
88     }
89     
90     /**
91      * Sets a new value for the given variable
92      * @return the old value or null
93      */

94     public Object JavaDoc put(String JavaDoc key, Object JavaDoc value) {
95     int hash = key.hashCode() & 0x7FFFFFFF;
96     int index = hash % table.length;
97     
98     for (Entry e = table[index]; e != null; e = e.next) {
99         if ((e.hash == hash) && e.key == key) {
100         Object JavaDoc old = e.value;
101         e.value = value;
102         return old;
103         }
104     }
105     
106     // The key is not in the hash table
107
int len = table.length;
108     if (count++ >= (len * 3) >>> 2) {
109         rehash();
110         index = hash % table.length;
111     }
112     
113     Entry e = new Entry(hash, key, value, table[index]);
114     table[index] = e;
115     return null;
116     }
117
118     /**
119      * Rehash the table
120      */

121     protected void rehash () {
122     Entry[] oldTable = table;
123     
124     table = new Entry[oldTable.length * 2 + 1];
125     
126     for (int i = oldTable.length-1; i >= 0; i--) {
127         for (Entry old = oldTable[i]; old != null;) {
128         Entry e = old;
129         old = old.next;
130         
131         int index = e.hash % table.length;
132         e.next = table[index];
133         table[index] = e;
134         }
135     }
136     }
137
138     /**
139      * To manage collisions
140      */

141     protected static class Entry {
142     /**
143      * The hash code
144      */

145     public int hash;
146     
147     /**
148      * The key
149      */

150     public String JavaDoc key;
151     
152     /**
153      * The value
154      */

155     public Object JavaDoc value;
156     
157     /**
158      * The next entry
159      */

160     public Entry next;
161     
162     /**
163      * Creates a new entry
164      */

165     public Entry(int hash, String JavaDoc key, Object JavaDoc value, Entry next) {
166         this.hash = hash;
167         this.key = key;
168         this.value = value;
169         this.next = next;
170     }
171     }
172 }
173
Popular Tags