Catch errors that may occur if LRUMap is used incorrectly

These can be used for debugging if LRUMap has a bug
bug 32573

git-svn-id: https://svn.apache.org/repos/asf/jakarta/commons/proper/collections/trunk@333022 13f79535-47bb-0310-9956-ffa450edef68
This commit is contained in:
Stephen Colebourne 2005-11-13 15:21:54 +00:00
parent bd94ccdfa5
commit e76bffaf96
1 changed files with 46 additions and 13 deletions

View File

@ -191,6 +191,9 @@ public class LRUMap
entry.before = header.before; entry.before = header.before;
header.before.after = entry; header.before.after = entry;
header.before = entry; header.before = entry;
} else if (entry == header) {
throw new IllegalStateException("Can't move header to MRU" +
" (please report this to commons-dev@jakarta.apache.org)");
} }
} }
@ -228,18 +231,32 @@ public class LRUMap
LinkEntry reuse = header.after; LinkEntry reuse = header.after;
boolean removeLRUEntry = false; boolean removeLRUEntry = false;
if (scanUntilRemovable) { if (scanUntilRemovable) {
while (reuse != header) { while (reuse != header && reuse != null) {
if (removeLRU(reuse)) { if (removeLRU(reuse)) {
removeLRUEntry = true; removeLRUEntry = true;
break; break;
} }
reuse = reuse.after; reuse = reuse.after;
} }
if (reuse == null) {
throw new IllegalStateException(
"Entry.after=null, header.after" + header.after + " header.before" + header.before +
" key=" + key + " value=" + value + " size=" + size + " maxSize=" + maxSize +
" Please check that your keys are immutable, and that you have used synchronization properly." +
" If so, then please report this to commons-dev@jakarta.apache.org as a bug.");
}
} else { } else {
removeLRUEntry = removeLRU(reuse); removeLRUEntry = removeLRU(reuse);
} }
if (removeLRUEntry) { if (removeLRUEntry) {
if (reuse == null) {
throw new IllegalStateException(
"reuse=null, header.after=" + header.after + " header.before" + header.before +
" key=" + key + " value=" + value + " size=" + size + " maxSize=" + maxSize +
" Please check that your keys are immutable, and that you have used synchronization properly." +
" If so, then please report this to commons-dev@jakarta.apache.org as a bug.");
}
reuseMapping(reuse, hashIndex, hashCode, key, value); reuseMapping(reuse, hashIndex, hashCode, key, value);
} else { } else {
super.addMapping(hashIndex, hashCode, key, value); super.addMapping(hashIndex, hashCode, key, value);
@ -264,19 +281,35 @@ public class LRUMap
// find the entry before the entry specified in the hash table // find the entry before the entry specified in the hash table
// remember that the parameters (except the first) refer to the new entry, // remember that the parameters (except the first) refer to the new entry,
// not the old one // not the old one
try {
int removeIndex = hashIndex(entry.hashCode, data.length); int removeIndex = hashIndex(entry.hashCode, data.length);
HashEntry loop = data[removeIndex]; HashEntry[] tmp = data; // may protect against some sync issues
HashEntry loop = tmp[removeIndex];
HashEntry previous = null; HashEntry previous = null;
while (loop != entry) { while (loop != entry && loop != null) {
previous = loop; previous = loop;
loop = loop.next; loop = loop.next;
} }
if (loop == null) {
throw new IllegalStateException(
"Entry.next=null, data[removeIndex]=" + data[removeIndex] + " previous=" + previous +
" key=" + key + " value=" + value + " size=" + size + " maxSize=" + maxSize +
" Please check that your keys are immutable, and that you have used synchronization properly." +
" If so, then please report this to commons-dev@jakarta.apache.org as a bug.");
}
// reuse the entry // reuse the entry
modCount++; modCount++;
removeEntry(entry, removeIndex, previous); removeEntry(entry, removeIndex, previous);
reuseEntry(entry, hashIndex, hashCode, key, value); reuseEntry(entry, hashIndex, hashCode, key, value);
addEntry(entry, hashIndex); addEntry(entry, hashIndex);
} catch (NullPointerException ex) {
throw new IllegalStateException(
"NPE, entry=" + entry + " entryIsHeader=" + (entry==header) +
" key=" + key + " value=" + value + " size=" + size + " maxSize=" + maxSize +
" Please check that your keys are immutable, and that you have used synchronization properly." +
" If so, then please report this to commons-dev@jakarta.apache.org as a bug.");
}
} }
/** /**