1 23 24 package com.sun.jdo.spi.persistence.utility; 25 26 import java.io.Serializable ; 27 import java.util.Collection ; 28 import java.util.Hashtable ; 29 import java.util.Iterator ; 30 import java.util.Map ; 31 import java.util.Set ; 32 33 40 public class BucketizedHashtable implements Cloneable , Map , Serializable { 41 private int bucketSize; 42 private Hashtable [] hashtables = null; 43 44 51 public BucketizedHashtable(int bucketSize, int initialCapacity, 52 float loadFactor) { 53 if (bucketSize <= 0 || initialCapacity < 0) { 54 throw new IllegalArgumentException (); 55 } 56 57 this.bucketSize = bucketSize; 58 59 hashtables = new Hashtable [bucketSize]; 60 61 int initialHashtableSize = 64 (int)Math.ceil((double)initialCapacity / bucketSize); 65 66 for (int i = 0; i < bucketSize; i++) { 67 hashtables[i] = new Hashtable (initialHashtableSize, loadFactor); 68 } 69 } 70 71 77 public BucketizedHashtable(int bucketSize, int initialCapacity) { 78 this(bucketSize, initialCapacity, 0.75f); 79 } 80 81 87 public BucketizedHashtable(int bucketSize) { 88 this(bucketSize, 11 * bucketSize, 0.75f); 89 } 90 91 97 public BucketizedHashtable() { 98 this(11, 11 * 11, 0.75f); 99 } 100 101 103 107 public Object get(Object key) { 108 return hashtables[getBucketIndex(key)].get(key); 109 } 110 111 117 public Object remove(Object key) { 118 return hashtables[getBucketIndex(key)].remove(key); 119 } 120 121 130 public Object put(Object key, Object value) { 131 return hashtables[getBucketIndex(key)].put(key, value); 132 } 133 134 138 public void putAll(Map t) { 139 if (t instanceof BucketizedHashtable) { 140 BucketizedHashtable bt = (BucketizedHashtable)t; 141 for (int i = 0; i < bt.bucketSize; i++) { 142 putAllFromMapWithEntrySet(bt.hashtables[i]); 143 } 144 } else { 145 putAllFromMapWithEntrySet(t); 146 } 147 } 148 149 154 public boolean containsKey(Object key) { 155 return hashtables[getBucketIndex(key)].containsKey(key); 156 } 157 158 163 public boolean containsValue(Object value) { 164 for (int i = 0; i < bucketSize; i++) { 165 if (hashtables[i].containsValue(value)) { 166 return true; 167 } 168 } 169 return false; 170 } 171 172 175 public int size() { 176 int totalSize = 0; 177 for (int i = 0; i < bucketSize; i++) { 178 totalSize += hashtables[i].size(); 179 } 180 return totalSize; 181 } 182 183 186 public int hashCode() { 187 int h = 0; 188 for (int i = 0; i < bucketSize; i++) { 189 h += hashtables[i].hashCode(); 190 } 191 return h; 192 } 193 194 197 public boolean isEmpty() { 198 for (int i = 0; i < bucketSize; i++) { 199 if (!hashtables[i].isEmpty()) { 200 return false; 201 } 202 } 203 return true; 204 } 205 206 209 public void clear() { 210 for (int i = 0; i < bucketSize; i++) { 211 hashtables[i].clear(); 212 } 213 } 214 215 221 public Set entrySet() { 222 if (bucketSize == 1) { 223 return hashtables[0].entrySet(); 224 } else { 225 throw new UnsupportedOperationException (); 226 } 227 } 228 229 235 public Set keySet() { 236 if (bucketSize == 1) { 237 return hashtables[0].keySet(); 238 } else { 239 throw new UnsupportedOperationException (); 240 } 241 } 242 243 249 public Collection values() { 250 if (bucketSize == 1) { 251 return hashtables[0].values(); 252 } else { 253 throw new UnsupportedOperationException (); 254 } 255 } 256 257 262 public boolean equals(Object o) { 263 if (o == this) { 264 return true; 265 } 266 267 if (!(o instanceof BucketizedHashtable)) { 268 return false; 269 } 270 BucketizedHashtable bt = (BucketizedHashtable)o; 271 if (bt.bucketSize != bucketSize || bt.size() != size()) { 272 return false; 273 } 274 275 for (int i = 0; i < bucketSize; i++) { 276 if (!hashtables[i].equals(bt.hashtables[i])) { 277 return false; 278 } 279 } 280 return true; 281 } 282 283 289 public Object clone() { 290 try { 291 BucketizedHashtable bt = (BucketizedHashtable)super.clone(); 292 bt.bucketSize = bucketSize; 293 bt.hashtables = new Hashtable [bucketSize]; 294 for (int i = 0; i < bucketSize; i++) { 295 bt.hashtables[i] = (Hashtable )hashtables[i].clone(); 296 } 297 return bt; 298 } catch (CloneNotSupportedException e) { 299 throw new InternalError (); 301 } 302 } 303 304 308 public String toString() { 309 StringBuffer buf = new StringBuffer ("["); buf.append(hashtables[0].toString()); 312 for (int i = 1; i < bucketSize; i++) { 313 buf.append(", "); buf.append(hashtables[i].toString()); 315 } 316 buf.append("]"); return buf.toString(); 318 } 319 320 323 private void putAllFromMapWithEntrySet(Map t) { 324 Iterator iter = t.entrySet().iterator(); 325 while (iter.hasNext()) { 326 Map.Entry e = (Map.Entry )iter.next(); 327 put(e.getKey(), e.getValue()); 328 } 329 } 330 331 335 private int getBucketIndex(Object key) { 336 int index = key.hashCode() % bucketSize; 337 return (index >= 0) ? index : index + bucketSize; 338 } 339 } 340 | Popular Tags |