src/share/classes/sun/misc/Cache.java
Print this page
*** 23,45 ****
* questions.
*/
package sun.misc;
import java.util.Dictionary;
import java.util.Enumeration;
import java.util.NoSuchElementException;
/**
* Caches the collision list.
*/
! class CacheEntry extends Ref {
int hash;
Object key;
CacheEntry next;
! public Object reconstitute() {
! return null;
}
}
/**
* The Cache class. Maps keys to values. Any object can be used as
--- 23,60 ----
* questions.
*/
package sun.misc;
+ import java.lang.ref.SoftReference;
import java.util.Dictionary;
import java.util.Enumeration;
import java.util.NoSuchElementException;
/**
* Caches the collision list.
*/
! class CacheEntry {
int hash;
Object key;
CacheEntry next;
! SoftReference<Object> value;
!
! public CacheEntry() {
! value = null;
! }
!
! public CacheEntry(Object o) {
! value = new SoftReference<>(o);
! }
!
! public Object get() {
! return value.get();
! }
!
! public void setThing(Object thing) {
! value = new SoftReference<>(thing);
}
}
/**
* The Cache class. Maps keys to values. Any object can be used as
*** 190,200 ****
CacheEntry tab[] = table;
int hash = key.hashCode();
int index = (hash & 0x7FFFFFFF) % tab.length;
for (CacheEntry e = tab[index]; e != null; e = e.next) {
if ((e.hash == hash) && e.key.equals(key)) {
! return e.check();
}
}
return null;
}
--- 205,215 ----
CacheEntry tab[] = table;
int hash = key.hashCode();
int index = (hash & 0x7FFFFFFF) % tab.length;
for (CacheEntry e = tab[index]; e != null; e = e.next) {
if ((e.hash == hash) && e.key.equals(key)) {
! return e.get();
}
}
return null;
}
*** 218,228 ****
for (int i = oldCapacity; i-- > 0;) {
for (CacheEntry old = oldTable[i]; old != null;) {
CacheEntry e = old;
old = old.next;
! if (e.check() != null) {
int index = (e.hash & 0x7FFFFFFF) % newCapacity;
e.next = newTable[index];
newTable[index] = e;
} else
count--; /* remove entries that have disappeared */
--- 233,243 ----
for (int i = oldCapacity; i-- > 0;) {
for (CacheEntry old = oldTable[i]; old != null;) {
CacheEntry e = old;
old = old.next;
! if (e.get() != null) {
int index = (e.hash & 0x7FFFFFFF) % newCapacity;
e.next = newTable[index];
newTable[index] = e;
} else
count--; /* remove entries that have disappeared */
*** 251,264 ****
int hash = key.hashCode();
int index = (hash & 0x7FFFFFFF) % 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 = e.check();
e.setThing(value);
return old;
! } else if (e.check() == null)
ne = e; /* reuse old flushed value */
}
if (count >= threshold) {
// Rehash the table if the threshold is exceeded
--- 266,279 ----
int hash = key.hashCode();
int index = (hash & 0x7FFFFFFF) % 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 = e.get();
e.setThing(value);
return old;
! } else if (e.get() == null)
ne = e; /* reuse old flushed value */
}
if (count >= threshold) {
// Rehash the table if the threshold is exceeded
*** 294,304 ****
prev.next = e.next;
} else {
tab[index] = e.next;
}
count--;
! return e.check();
}
}
return null;
}
}
--- 309,319 ----
prev.next = e.next;
} else {
tab[index] = e.next;
}
count--;
! return e.get();
}
}
return null;
}
}
*** 320,330 ****
}
public boolean hasMoreElements() {
while (index >= 0) {
while (entry != null)
! if (entry.check() != null)
return true;
else
entry = entry.next;
while (--index >= 0 && (entry = table[index]) == null) ;
}
--- 335,345 ----
}
public boolean hasMoreElements() {
while (index >= 0) {
while (entry != null)
! if (entry.get() != null)
return true;
else
entry = entry.next;
while (--index >= 0 && (entry = table[index]) == null) ;
}
*** 336,347 ****
if (entry == null)
while (--index >= 0 && (entry = table[index]) == null) ;
if (entry != null) {
CacheEntry e = entry;
entry = e.next;
! if (e.check() != null)
! return keys ? e.key : e.check();
}
}
throw new NoSuchElementException("CacheEnumerator");
}
--- 351,362 ----
if (entry == null)
while (--index >= 0 && (entry = table[index]) == null) ;
if (entry != null) {
CacheEntry e = entry;
entry = e.next;
! if (e.get() != null)
! return keys ? e.key : e.get();
}
}
throw new NoSuchElementException("CacheEnumerator");
}