FileDocCategorySizeDatePackage
BucketizedHashtable.javaAPI DocGlassfish v2 API11855Fri May 04 22:35:20 BST 2007com.sun.jdo.spi.persistence.utility

BucketizedHashtable

public class BucketizedHashtable extends Object implements Serializable, Cloneable, Map
This class implements bucketize hashtable, which subdivide the key collection stored into several hashtables (buckets) of smaller size. This will reduce the contention of hashtable.
author
Shing Wai Chan

Fields Summary
private int
bucketSize
private Hashtable[]
hashtables
Constructors Summary
public BucketizedHashtable(int bucketSize, int initialCapacity, float loadFactor)
Constructs a new, empty BucketizedHashtable with the specified bucket size, initial capacity and load factor.

param
bucketSize the number of buckets used for hashing
param
initialCapacity the initial capacity of BucketizedHashtable
param
loadFactor the load factor of hashtable


                                                         
        
              
        if (bucketSize <= 0 || initialCapacity < 0) {
            throw new IllegalArgumentException();
        }

        this.bucketSize = bucketSize;   

        hashtables = new Hashtable[bucketSize];

        // always round up to the nearest integer so that it has at
        // least the initialCapacity
        int initialHashtableSize =
                (int)Math.ceil((double)initialCapacity / bucketSize);

        for (int i = 0; i < bucketSize; i++) {
            hashtables[i] = new Hashtable(initialHashtableSize, loadFactor);
        }
    
public BucketizedHashtable(int bucketSize, int initialCapacity)
Constructs a new, empty BucketizedHashtable with the specified bucket size, initial capacity and default load factor 0.75.

param
bucketSize the number of buckets used for hashing
param
initialCapacity the initial capacity of hashtable

        this(bucketSize, initialCapacity, 0.75f);
    
public BucketizedHashtable(int bucketSize)
Constructs a new, empty BucketizedHashtable with the specified bucket size, default initial capacity (11 * bucketSize) and default load factor 0.75.

param
bucketSize the number of buckets used for hashing

        this(bucketSize, 11 * bucketSize, 0.75f);
    
public BucketizedHashtable()
Constructs a new, empty BucketizedHashtable with the default bucket size 11, default initial capacity (11 * bucketSize) and default load factor 0.75.

param
bucketSize the number of buckets used for hashing

        this(11, 11 * 11, 0.75f);
    
Methods Summary
public voidclear()
Clears this BucketizedHashtable so that it contains no key.

        for (int i = 0; i < bucketSize; i++) {
            hashtables[i].clear();
        }
    
public java.lang.Objectclone()
Creates and returns a shallow copy of this object. The keys and values are not cloned.

return
a clone of BucketizedHashtable

        try {
            BucketizedHashtable bt = (BucketizedHashtable)super.clone();
            bt.bucketSize = bucketSize;
            bt.hashtables = new Hashtable[bucketSize];
            for (int i = 0; i < bucketSize; i++) {
                bt.hashtables[i] = (Hashtable)hashtables[i].clone();
            }
            return bt;
        } catch (CloneNotSupportedException e) {
            // this shouldn't happen, since we are Cloneable
            throw new InternalError();
        }
    
public booleancontainsKey(java.lang.Object key)

param
key possible key
return
true if and only if the specified object is a key in one of of the hashtables

        return hashtables[getBucketIndex(key)].containsKey(key);
    
public booleancontainsValue(java.lang.Object value)

param
value possible value
return
true if and only if the specified object is a value in one of of the hashtables

        for (int i = 0; i < bucketSize; i++) {
            if (hashtables[i].containsValue(value)) {
                return true;
            }
        }
        return false;
    
public java.util.SetentrySet()
The return set is backed by the map, so changes to the map are reflected in the set, and vice-versa.

return
a set of Map.Entry when bucketSet equal 1
exception
UnsupportedOperationException when bucketSize is greater one

        if (bucketSize == 1) {
            return hashtables[0].entrySet();
        } else {
            throw new UnsupportedOperationException();
        }
    
public booleanequals(java.lang.Object o)
Compares the specified object with this map for equality.

return
true if the specified object is a BucketizedHashtable with hashtables represent the same set of mappings.

        if (o == this) {
            return true;
        }

        if (!(o instanceof BucketizedHashtable)) {
            return false;
        }
        BucketizedHashtable bt = (BucketizedHashtable)o;
        if (bt.bucketSize != bucketSize || bt.size() != size()) {
            return false;
        }

        for (int i = 0; i < bucketSize; i++) {
             if (!hashtables[i].equals(bt.hashtables[i])) {
                 return false;
             }
        }
        return true;
    
public java.lang.Objectget(java.lang.Object key)

param
key a key in the hashtable
return
the value to which the specified key is mapped.

        return hashtables[getBucketIndex(key)].get(key);
    
private intgetBucketIndex(java.lang.Object key)

param
key
return
the bucket index for the specified key

        int index = key.hashCode() % bucketSize;
        return (index >= 0) ? index : index + bucketSize;
    
public inthashCode()

return
the hash code value for this map

        int h = 0;
        for (int i = 0; i < bucketSize; i++) {
            h += hashtables[i].hashCode();
        }
        return h;
    
public booleanisEmpty()

return
true if this map contains no key-value mappings

        for (int i = 0; i < bucketSize; i++) {
             if (!hashtables[i].isEmpty()) {
                 return false;
             }
        }
        return true;
    
public java.util.SetkeySet()
The return set is backed by the map, so changes to the map are reflected in the set, and vice-versa.

return
a set of keys when bucketSet equal 1
exception
UnsupportedOperationException when bucketSize is greater one

        if (bucketSize == 1) {
            return hashtables[0].keySet();
        } else {
            throw new UnsupportedOperationException();
        }
    
public java.lang.Objectput(java.lang.Object key, java.lang.Object value)
Maps the specified key to the specified value in this hashtable. Neither the key nor the value can be null.

param
key the hashtable key
param
value the value
return
the previous value of the specified key in hashtables, or null if it did not have one.

        return hashtables[getBucketIndex(key)].put(key, value);
    
public voidputAll(java.util.Map t)

param
t BucketizedHashtable or a Map with a supported operation entrySet

        if (t instanceof BucketizedHashtable) {
            BucketizedHashtable bt = (BucketizedHashtable)t;
            for (int i = 0; i < bt.bucketSize; i++) {
                putAllFromMapWithEntrySet(bt.hashtables[i]);
            }
        } else {
            putAllFromMapWithEntrySet(t);
        }
    
private voidputAllFromMapWithEntrySet(java.util.Map t)

param
t Map with a supported entrySet operation

        Iterator iter = t.entrySet().iterator();
        while (iter.hasNext()) {
            Map.Entry e = (Map.Entry)iter.next();
            put(e.getKey(), e.getValue());
        }
    
public java.lang.Objectremove(java.lang.Object key)
Remove the key and its corresponding value.

param
key the key that needs to be removed
return
the value to which the key had been mapped, or null if the key did not have a mapping.

        return hashtables[getBucketIndex(key)].remove(key);
    
public intsize()

return
the total number of key-value mappings of all buckets

        int totalSize = 0;
        for (int i = 0; i < bucketSize; i++) {
             totalSize += hashtables[i].size();
        }
        return totalSize;
    
public java.lang.StringtoString()

return
a string representation of this BucketizedHashtable

        StringBuffer buf = new StringBuffer("[");  // NOI18N
        //bucketSize always >= 1 
        buf.append(hashtables[0].toString());
        for (int i = 1; i < bucketSize; i++) {
            buf.append(", "); // NOI18N
            buf.append(hashtables[i].toString());
        }
        buf.append("]"); // NOI18N
        return buf.toString();
    
public java.util.Collectionvalues()
The return collection is backed by the map, so changes to the map are reflected in the collection, and vice-versa.

return
a collection of values when bucketSet equal 1
exception
UnsupportedOperationException when bucketSize is greater one

        if (bucketSize == 1) {
            return hashtables[0].values();
        } else {
            throw new UnsupportedOperationException();
        }