home *** CD-ROM | disk | FTP | other *** search
/ PC Online 1998 January / PCO0198.ISO / 1&1 / java.z / java_301 / sun / misc / Cache.class (.txt) next >
Encoding:
Java Class File  |  1996-10-20  |  3.2 KB  |  159 lines

  1. package sun.misc;
  2.  
  3. import java.util.Dictionary;
  4. import java.util.Enumeration;
  5.  
  6. public class Cache extends Dictionary {
  7.    private CacheEntry[] table;
  8.    private int count;
  9.    private int threshold;
  10.    private float loadFactor;
  11.  
  12.    private void init(int initialCapacity, float loadFactor) {
  13.       if (initialCapacity > 0 && !((double)loadFactor <= (double)0.0F)) {
  14.          this.loadFactor = loadFactor;
  15.          this.table = new CacheEntry[initialCapacity];
  16.          this.threshold = (int)((float)initialCapacity * loadFactor);
  17.       } else {
  18.          throw new IllegalArgumentException();
  19.       }
  20.    }
  21.  
  22.    public Cache(int initialCapacity, float loadFactor) {
  23.       this.init(initialCapacity, loadFactor);
  24.    }
  25.  
  26.    public Cache(int initialCapacity) {
  27.       this.init(initialCapacity, 0.75F);
  28.    }
  29.  
  30.    public Cache() {
  31.       try {
  32.          this.init(101, 0.75F);
  33.       } catch (IllegalArgumentException var1) {
  34.          throw new Error("panic");
  35.       }
  36.    }
  37.  
  38.    public int size() {
  39.       return this.count;
  40.    }
  41.  
  42.    public boolean isEmpty() {
  43.       return this.count == 0;
  44.    }
  45.  
  46.    public synchronized Enumeration keys() {
  47.       return new CacheEnumerator(this.table, true);
  48.    }
  49.  
  50.    public synchronized Enumeration elements() {
  51.       return new CacheEnumerator(this.table, false);
  52.    }
  53.  
  54.    public synchronized Object get(Object key) {
  55.       CacheEntry[] tab = this.table;
  56.       int hash = key.hashCode();
  57.       int index = (hash & Integer.MAX_VALUE) % tab.length;
  58.  
  59.       for(CacheEntry e = tab[index]; e != null; e = e.next) {
  60.          if (e.hash == hash && e.key.equals(key)) {
  61.             return ((Ref)e).check();
  62.          }
  63.       }
  64.  
  65.       return null;
  66.    }
  67.  
  68.    protected void rehash() {
  69.       int oldCapacity = this.table.length;
  70.       CacheEntry[] oldTable = this.table;
  71.       int newCapacity = oldCapacity * 2 + 1;
  72.       CacheEntry[] newTable = new CacheEntry[newCapacity];
  73.       this.threshold = (int)((float)newCapacity * this.loadFactor);
  74.       this.table = newTable;
  75.       int i = oldCapacity;
  76.  
  77.       while(i-- > 0) {
  78.          CacheEntry old = oldTable[i];
  79.  
  80.          while(old != null) {
  81.             CacheEntry e = old;
  82.             old = old.next;
  83.             if (((Ref)e).check() != null) {
  84.                int index = (e.hash & Integer.MAX_VALUE) % newCapacity;
  85.                e.next = newTable[index];
  86.                newTable[index] = e;
  87.             } else {
  88.                --this.count;
  89.             }
  90.          }
  91.       }
  92.  
  93.    }
  94.  
  95.    public synchronized Object put(Object key, Object value) {
  96.       if (value == null) {
  97.          throw new NullPointerException();
  98.       } else {
  99.          CacheEntry[] tab = this.table;
  100.          int hash = key.hashCode();
  101.          int index = (hash & Integer.MAX_VALUE) % tab.length;
  102.          CacheEntry ne = null;
  103.  
  104.          for(CacheEntry e = tab[index]; e != null; e = e.next) {
  105.             if (e.hash == hash && e.key.equals(key)) {
  106.                Object old = ((Ref)e).check();
  107.                ((Ref)e).setThing(value);
  108.                return old;
  109.             }
  110.  
  111.             if (((Ref)e).check() == null) {
  112.                ne = e;
  113.             }
  114.          }
  115.  
  116.          if (this.count >= this.threshold) {
  117.             this.rehash();
  118.             return this.put(key, value);
  119.          } else {
  120.             if (ne == null) {
  121.                ne = new CacheEntry();
  122.                ne.next = tab[index];
  123.                tab[index] = ne;
  124.                ++this.count;
  125.             }
  126.  
  127.             ne.hash = hash;
  128.             ne.key = key;
  129.             ((Ref)ne).setThing(value);
  130.             return null;
  131.          }
  132.       }
  133.    }
  134.  
  135.    public synchronized Object remove(Object key) {
  136.       CacheEntry[] tab = this.table;
  137.       int hash = key.hashCode();
  138.       int index = (hash & Integer.MAX_VALUE) % tab.length;
  139.       CacheEntry e = tab[index];
  140.  
  141.       for(CacheEntry prev = null; e != null; e = e.next) {
  142.          if (e.hash == hash && e.key.equals(key)) {
  143.             if (prev != null) {
  144.                prev.next = e.next;
  145.             } else {
  146.                tab[index] = e.next;
  147.             }
  148.  
  149.             --this.count;
  150.             return ((Ref)e).check();
  151.          }
  152.  
  153.          prev = e;
  154.       }
  155.  
  156.       return null;
  157.    }
  158. }
  159.