home *** CD-ROM | disk | FTP | other *** search
/ Cre@te Online 2000 December / Cre@teOnline CD05.iso / MacSoft / XML ConsoleMax.sea / XML ConsoleMax / Required / parser.jar / com / sun / xml / parser / SimpleHashtable.class (.txt) < prev    next >
Encoding:
Java Class File  |  2000-02-23  |  2.7 KB  |  157 lines

  1. package com.sun.xml.parser;
  2.  
  3. import java.util.Enumeration;
  4.  
  5. final class SimpleHashtable implements Enumeration {
  6.    private Entry[] table;
  7.    private Entry current;
  8.    private int currentBucket;
  9.    private int count;
  10.    private int threshold;
  11.    private static final float loadFactor = 0.75F;
  12.  
  13.    public SimpleHashtable() {
  14.       this(11);
  15.    }
  16.  
  17.    public SimpleHashtable(int var1) {
  18.       this.current = null;
  19.       this.currentBucket = 0;
  20.       if (var1 < 0) {
  21.          throw new IllegalArgumentException("Illegal Capacity: " + var1);
  22.       } else {
  23.          if (var1 == 0) {
  24.             var1 = 1;
  25.          }
  26.  
  27.          this.table = new Entry[var1];
  28.          this.threshold = (int)((float)var1 * 0.75F);
  29.       }
  30.    }
  31.  
  32.    public void clear() {
  33.       this.count = 0;
  34.       this.currentBucket = 0;
  35.       this.current = null;
  36.  
  37.       for(int var1 = 0; var1 < this.table.length; ++var1) {
  38.          this.table[var1] = null;
  39.       }
  40.  
  41.    }
  42.  
  43.    public Object get(String var1) {
  44.       Entry[] var2 = this.table;
  45.       int var3 = var1.hashCode();
  46.       int var4 = (var3 & Integer.MAX_VALUE) % var2.length;
  47.  
  48.       for(Entry var5 = var2[var4]; var5 != null; var5 = var5.next) {
  49.          if (var5.hash == var3 && var5.key == var1) {
  50.             return var5.value;
  51.          }
  52.       }
  53.  
  54.       return null;
  55.    }
  56.  
  57.    public Object getNonInterned(String var1) {
  58.       Entry[] var2 = this.table;
  59.       int var3 = var1.hashCode();
  60.       int var4 = (var3 & Integer.MAX_VALUE) % var2.length;
  61.  
  62.       for(Entry var5 = var2[var4]; var5 != null; var5 = var5.next) {
  63.          if (var5.hash == var3 && var5.key.equals(var1)) {
  64.             return var5.value;
  65.          }
  66.       }
  67.  
  68.       return null;
  69.    }
  70.  
  71.    public boolean hasMoreElements() {
  72.       if (this.current != null) {
  73.          return true;
  74.       } else {
  75.          while(this.currentBucket < this.table.length) {
  76.             this.current = this.table[this.currentBucket++];
  77.             if (this.current != null) {
  78.                return true;
  79.             }
  80.          }
  81.  
  82.          return false;
  83.       }
  84.    }
  85.  
  86.    public Enumeration keys() {
  87.       this.currentBucket = 0;
  88.       this.current = null;
  89.       return this;
  90.    }
  91.  
  92.    public Object nextElement() {
  93.       if (this.current == null) {
  94.          throw new IllegalStateException();
  95.       } else {
  96.          Object var1 = this.current.key;
  97.          this.current = this.current.next;
  98.          return var1;
  99.       }
  100.    }
  101.  
  102.    public Object put(Object var1, Object var2) {
  103.       if (var2 == null) {
  104.          throw new NullPointerException();
  105.       } else {
  106.          Entry[] var3 = this.table;
  107.          int var4 = var1.hashCode();
  108.          int var5 = (var4 & Integer.MAX_VALUE) % var3.length;
  109.  
  110.          for(Entry var6 = var3[var5]; var6 != null; var6 = var6.next) {
  111.             if (var6.hash == var4 && var6.key == var1) {
  112.                Object var7 = var6.value;
  113.                var6.value = var2;
  114.                return var7;
  115.             }
  116.          }
  117.  
  118.          if (this.count >= this.threshold) {
  119.             this.rehash();
  120.             var3 = this.table;
  121.             var5 = (var4 & Integer.MAX_VALUE) % var3.length;
  122.          }
  123.  
  124.          Entry var8 = new Entry(var4, var1, var2, var3[var5]);
  125.          var3[var5] = var8;
  126.          ++this.count;
  127.          return null;
  128.       }
  129.    }
  130.  
  131.    private void rehash() {
  132.       int var1 = this.table.length;
  133.       Entry[] var2 = this.table;
  134.       int var3 = var1 * 2 + 1;
  135.       Entry[] var4 = new Entry[var3];
  136.       this.threshold = (int)((float)var3 * 0.75F);
  137.       this.table = var4;
  138.       int var5 = var1;
  139.  
  140.       Entry var7;
  141.       int var8;
  142.       while(var5-- > 0) {
  143.          for(Entry var6 = var2[var5]; var6 != null; var4[var8] = var7) {
  144.             var7 = var6;
  145.             var6 = var6.next;
  146.             var8 = (var7.hash & Integer.MAX_VALUE) % var3;
  147.             var7.next = var4[var8];
  148.          }
  149.       }
  150.  
  151.    }
  152.  
  153.    public int size() {
  154.       return this.count;
  155.    }
  156. }
  157.