You are viewing a plain text version of this content. The canonical link for it is here.
Posted to dev@commons.apache.org by sc...@apache.org on 2005/11/13 16:21:57 UTC
svn commit: r333022 -
/jakarta/commons/proper/collections/trunk/src/java/org/apache/commons/collections/map/LRUMap.java
Author: scolebourne
Date: Sun Nov 13 07:21:54 2005
New Revision: 333022
URL: http://svn.apache.org/viewcvs?rev=333022&view=rev
Log:
Catch errors that may occur if LRUMap is used incorrectly
These can be used for debugging if LRUMap has a bug
bug 32573
Modified:
jakarta/commons/proper/collections/trunk/src/java/org/apache/commons/collections/map/LRUMap.java
Modified: jakarta/commons/proper/collections/trunk/src/java/org/apache/commons/collections/map/LRUMap.java
URL: http://svn.apache.org/viewcvs/jakarta/commons/proper/collections/trunk/src/java/org/apache/commons/collections/map/LRUMap.java?rev=333022&r1=333021&r2=333022&view=diff
==============================================================================
--- jakarta/commons/proper/collections/trunk/src/java/org/apache/commons/collections/map/LRUMap.java (original)
+++ jakarta/commons/proper/collections/trunk/src/java/org/apache/commons/collections/map/LRUMap.java Sun Nov 13 07:21:54 2005
@@ -191,6 +191,9 @@
entry.before = header.before;
header.before.after = 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 @@
LinkEntry reuse = header.after;
boolean removeLRUEntry = false;
if (scanUntilRemovable) {
- while (reuse != header) {
+ while (reuse != header && reuse != null) {
if (removeLRU(reuse)) {
removeLRUEntry = true;
break;
}
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 {
removeLRUEntry = removeLRU(reuse);
}
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);
} else {
super.addMapping(hashIndex, hashCode, key, value);
@@ -264,19 +281,35 @@
// find the entry before the entry specified in the hash table
// remember that the parameters (except the first) refer to the new entry,
// not the old one
- int removeIndex = hashIndex(entry.hashCode, data.length);
- HashEntry loop = data[removeIndex];
- HashEntry previous = null;
- while (loop != entry) {
- previous = loop;
- loop = loop.next;
+ try {
+ int removeIndex = hashIndex(entry.hashCode, data.length);
+ HashEntry[] tmp = data; // may protect against some sync issues
+ HashEntry loop = tmp[removeIndex];
+ HashEntry previous = null;
+ while (loop != entry && loop != null) {
+ previous = loop;
+ 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
+ modCount++;
+ removeEntry(entry, removeIndex, previous);
+ reuseEntry(entry, hashIndex, hashCode, key, value);
+ 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.");
}
-
- // reuse the entry
- modCount++;
- removeEntry(entry, removeIndex, previous);
- reuseEntry(entry, hashIndex, hashCode, key, value);
- addEntry(entry, hashIndex);
}
/**
---------------------------------------------------------------------
To unsubscribe, e-mail: commons-dev-unsubscribe@jakarta.apache.org
For additional commands, e-mail: commons-dev-help@jakarta.apache.org