home *** CD-ROM | disk | FTP | other *** search
/ PC Online 1998 January / PCO0198.ISO / 1&1 / java.z / java_301 / java / util / Hashtable.class (.txt) < prev    next >
Encoding:
Java Class File  |  1996-10-20  |  4.4 KB  |  217 lines

  1. package java.util;
  2.  
  3. public class Hashtable extends Dictionary implements Cloneable {
  4.    private HashtableEntry[] table;
  5.    private int count;
  6.    private int threshold;
  7.    private float loadFactor;
  8.  
  9.    public Hashtable(int initialCapacity, float loadFactor) {
  10.       if (initialCapacity > 0 && !((double)loadFactor <= (double)0.0F)) {
  11.          this.loadFactor = loadFactor;
  12.          this.table = new HashtableEntry[initialCapacity];
  13.          this.threshold = (int)((float)initialCapacity * loadFactor);
  14.       } else {
  15.          throw new IllegalArgumentException();
  16.       }
  17.    }
  18.  
  19.    public Hashtable(int initialCapacity) {
  20.       this(initialCapacity, 0.75F);
  21.    }
  22.  
  23.    public Hashtable() {
  24.       this(101, 0.75F);
  25.    }
  26.  
  27.    public int size() {
  28.       return this.count;
  29.    }
  30.  
  31.    public boolean isEmpty() {
  32.       return this.count == 0;
  33.    }
  34.  
  35.    public synchronized Enumeration keys() {
  36.       return new HashtableEnumerator(this.table, true);
  37.    }
  38.  
  39.    public synchronized Enumeration elements() {
  40.       return new HashtableEnumerator(this.table, false);
  41.    }
  42.  
  43.    public synchronized boolean contains(Object value) {
  44.       if (value == null) {
  45.          throw new NullPointerException();
  46.       } else {
  47.          HashtableEntry[] tab = this.table;
  48.          int i = tab.length;
  49.  
  50.          while(i-- > 0) {
  51.             for(HashtableEntry e = tab[i]; e != null; e = e.next) {
  52.                if (e.value.equals(value)) {
  53.                   return true;
  54.                }
  55.             }
  56.          }
  57.  
  58.          return false;
  59.       }
  60.    }
  61.  
  62.    public synchronized boolean containsKey(Object key) {
  63.       HashtableEntry[] tab = this.table;
  64.       int hash = key.hashCode();
  65.       int index = (hash & Integer.MAX_VALUE) % tab.length;
  66.  
  67.       for(HashtableEntry e = tab[index]; e != null; e = e.next) {
  68.          if (e.hash == hash && e.key.equals(key)) {
  69.             return true;
  70.          }
  71.       }
  72.  
  73.       return false;
  74.    }
  75.  
  76.    public synchronized Object get(Object key) {
  77.       HashtableEntry[] tab = this.table;
  78.       int hash = key.hashCode();
  79.       int index = (hash & Integer.MAX_VALUE) % tab.length;
  80.  
  81.       for(HashtableEntry e = tab[index]; e != null; e = e.next) {
  82.          if (e.hash == hash && e.key.equals(key)) {
  83.             return e.value;
  84.          }
  85.       }
  86.  
  87.       return null;
  88.    }
  89.  
  90.    protected void rehash() {
  91.       int oldCapacity = this.table.length;
  92.       HashtableEntry[] oldTable = this.table;
  93.       int newCapacity = oldCapacity * 2 + 1;
  94.       HashtableEntry[] newTable = new HashtableEntry[newCapacity];
  95.       this.threshold = (int)((float)newCapacity * this.loadFactor);
  96.       this.table = newTable;
  97.       int i = oldCapacity;
  98.  
  99.       HashtableEntry e;
  100.       int index;
  101.       while(i-- > 0) {
  102.          for(HashtableEntry old = oldTable[i]; old != null; newTable[index] = e) {
  103.             e = old;
  104.             old = old.next;
  105.             index = (e.hash & Integer.MAX_VALUE) % newCapacity;
  106.             e.next = newTable[index];
  107.          }
  108.       }
  109.  
  110.    }
  111.  
  112.    public synchronized Object put(Object key, Object value) {
  113.       if (value == null) {
  114.          throw new NullPointerException();
  115.       } else {
  116.          HashtableEntry[] tab = this.table;
  117.          int hash = key.hashCode();
  118.          int index = (hash & Integer.MAX_VALUE) % tab.length;
  119.  
  120.          for(HashtableEntry e = tab[index]; e != null; e = e.next) {
  121.             if (e.hash == hash && e.key.equals(key)) {
  122.                Object old = e.value;
  123.                e.value = value;
  124.                return old;
  125.             }
  126.          }
  127.  
  128.          if (this.count >= this.threshold) {
  129.             this.rehash();
  130.             return this.put(key, value);
  131.          } else {
  132.             HashtableEntry e = new HashtableEntry();
  133.             e.hash = hash;
  134.             e.key = key;
  135.             e.value = value;
  136.             e.next = tab[index];
  137.             tab[index] = e;
  138.             ++this.count;
  139.             return null;
  140.          }
  141.       }
  142.    }
  143.  
  144.    public synchronized Object remove(Object key) {
  145.       HashtableEntry[] tab = this.table;
  146.       int hash = key.hashCode();
  147.       int index = (hash & Integer.MAX_VALUE) % tab.length;
  148.       HashtableEntry e = tab[index];
  149.  
  150.       for(HashtableEntry prev = null; e != null; e = e.next) {
  151.          if (e.hash == hash && e.key.equals(key)) {
  152.             if (prev != null) {
  153.                prev.next = e.next;
  154.             } else {
  155.                tab[index] = e.next;
  156.             }
  157.  
  158.             --this.count;
  159.             return e.value;
  160.          }
  161.  
  162.          prev = e;
  163.       }
  164.  
  165.       return null;
  166.    }
  167.  
  168.    public synchronized void clear() {
  169.       HashtableEntry[] tab = this.table;
  170.       int index = tab.length;
  171.  
  172.       while(true) {
  173.          --index;
  174.          if (index < 0) {
  175.             this.count = 0;
  176.             return;
  177.          }
  178.  
  179.          tab[index] = null;
  180.       }
  181.    }
  182.  
  183.    public synchronized Object clone() {
  184.       try {
  185.          Hashtable t = (Hashtable)super.clone();
  186.          t.table = new HashtableEntry[this.table.length];
  187.  
  188.          for(int i = this.table.length; i-- > 0; t.table[i] = this.table[i] != null ? (HashtableEntry)this.table[i].clone() : null) {
  189.          }
  190.  
  191.          return t;
  192.       } catch (CloneNotSupportedException var3) {
  193.          throw new InternalError();
  194.       }
  195.    }
  196.  
  197.    public synchronized String toString() {
  198.       int max = this.size() - 1;
  199.       StringBuffer buf = new StringBuffer();
  200.       Enumeration k = this.keys();
  201.       Enumeration e = this.elements();
  202.       buf.append("{");
  203.  
  204.       for(int i = 0; i <= max; ++i) {
  205.          String s1 = k.nextElement().toString();
  206.          String s2 = e.nextElement().toString();
  207.          buf.append(s1 + "=" + s2);
  208.          if (i < max) {
  209.             buf.append(", ");
  210.          }
  211.       }
  212.  
  213.       buf.append("}");
  214.       return buf.toString();
  215.    }
  216. }
  217.