home *** CD-ROM | disk | FTP | other *** search
- package sun.misc;
-
- import java.util.Dictionary;
- import java.util.Enumeration;
-
- public class Cache extends Dictionary {
- private CacheEntry[] table;
- private int count;
- private int threshold;
- private float loadFactor;
-
- private void init(int initialCapacity, float loadFactor) {
- if (initialCapacity > 0 && !((double)loadFactor <= (double)0.0F)) {
- this.loadFactor = loadFactor;
- this.table = new CacheEntry[initialCapacity];
- this.threshold = (int)((float)initialCapacity * loadFactor);
- } else {
- throw new IllegalArgumentException();
- }
- }
-
- public Cache(int initialCapacity, float loadFactor) {
- this.init(initialCapacity, loadFactor);
- }
-
- public Cache(int initialCapacity) {
- this.init(initialCapacity, 0.75F);
- }
-
- public Cache() {
- try {
- this.init(101, 0.75F);
- } catch (IllegalArgumentException var1) {
- throw new Error("panic");
- }
- }
-
- public int size() {
- return this.count;
- }
-
- public boolean isEmpty() {
- return this.count == 0;
- }
-
- public synchronized Enumeration keys() {
- return new CacheEnumerator(this.table, true);
- }
-
- public synchronized Enumeration elements() {
- return new CacheEnumerator(this.table, false);
- }
-
- public synchronized Object get(Object key) {
- CacheEntry[] tab = this.table;
- int hash = key.hashCode();
- int index = (hash & Integer.MAX_VALUE) % tab.length;
-
- for(CacheEntry e = tab[index]; e != null; e = e.next) {
- if (e.hash == hash && e.key.equals(key)) {
- return ((Ref)e).check();
- }
- }
-
- return null;
- }
-
- protected void rehash() {
- int oldCapacity = this.table.length;
- CacheEntry[] oldTable = this.table;
- int newCapacity = oldCapacity * 2 + 1;
- CacheEntry[] newTable = new CacheEntry[newCapacity];
- this.threshold = (int)((float)newCapacity * this.loadFactor);
- this.table = newTable;
- int i = oldCapacity;
-
- while(i-- > 0) {
- CacheEntry old = oldTable[i];
-
- while(old != null) {
- CacheEntry e = old;
- old = old.next;
- if (((Ref)e).check() != null) {
- int index = (e.hash & Integer.MAX_VALUE) % newCapacity;
- e.next = newTable[index];
- newTable[index] = e;
- } else {
- --this.count;
- }
- }
- }
-
- }
-
- public synchronized Object put(Object key, Object value) {
- if (value == null) {
- throw new NullPointerException();
- } else {
- CacheEntry[] tab = this.table;
- int hash = key.hashCode();
- int index = (hash & Integer.MAX_VALUE) % tab.length;
- CacheEntry ne = null;
-
- for(CacheEntry e = tab[index]; e != null; e = e.next) {
- if (e.hash == hash && e.key.equals(key)) {
- Object old = ((Ref)e).check();
- ((Ref)e).setThing(value);
- return old;
- }
-
- if (((Ref)e).check() == null) {
- ne = e;
- }
- }
-
- if (this.count >= this.threshold) {
- this.rehash();
- return this.put(key, value);
- } else {
- if (ne == null) {
- ne = new CacheEntry();
- ne.next = tab[index];
- tab[index] = ne;
- ++this.count;
- }
-
- ne.hash = hash;
- ne.key = key;
- ((Ref)ne).setThing(value);
- return null;
- }
- }
- }
-
- public synchronized Object remove(Object key) {
- CacheEntry[] tab = this.table;
- int hash = key.hashCode();
- int index = (hash & Integer.MAX_VALUE) % tab.length;
- CacheEntry e = tab[index];
-
- for(CacheEntry prev = null; e != null; e = e.next) {
- if (e.hash == hash && e.key.equals(key)) {
- if (prev != null) {
- prev.next = e.next;
- } else {
- tab[index] = e.next;
- }
-
- --this.count;
- return ((Ref)e).check();
- }
-
- prev = e;
- }
-
- return null;
- }
- }
-