[Arakhnę-Dev] [30] Bug fix: remove() on iterator cause internal exception.

[ Thread Index | Date Index | More arakhne.org/dev Archives ]


Revision: 30
Author:   galland
Date:     2009-02-03 10:46:15 +0100 (Tue, 03 Feb 2009)

Log Message:
-----------
Bug fix: remove() on iterator cause internal exception.

Modified Paths:
--------------
    trunk/arakhneRefs/src/main/java/org/arakhne/util/ref/AbstractWeakSoftValueMap.java


Modified: trunk/arakhneRefs/src/main/java/org/arakhne/util/ref/AbstractWeakSoftValueMap.java
===================================================================
--- trunk/arakhneRefs/src/main/java/org/arakhne/util/ref/AbstractWeakSoftValueMap.java	2009-02-03 07:37:28 UTC (rev 29)
+++ trunk/arakhneRefs/src/main/java/org/arakhne/util/ref/AbstractWeakSoftValueMap.java	2009-02-03 09:46:15 UTC (rev 30)
@@ -23,6 +23,7 @@
 
 import java.lang.ref.Reference;
 import java.lang.ref.ReferenceQueue;
+import java.lang.ref.WeakReference;
 import java.util.AbstractMap;
 import java.util.AbstractSet;
 import java.util.HashMap;
@@ -265,12 +266,14 @@
 	 */
 	private class RealEntry implements Entry<K,V> {
 
+		private final WeakReference<WeakSoftValue<K,V>> realEntry;
 		private final K key;
 		private V value;
 		
-		public RealEntry(K k, V v) {
-			this.key = k;
-			this.value = v;
+		public RealEntry(WeakSoftValue<K,V> rEntry, V rValue) {
+			this.value = rValue;
+			this.key = rEntry.getKey();
+			this.realEntry = new WeakReference<WeakSoftValue<K,V>>(rEntry);
 		}
 		
 		@Override
@@ -286,11 +289,16 @@
 		@Override
 		public V setValue(V value) {
 			V oldValue = this.value;
+			
 			this.value = value;
-			if (value==null)
-				remove(this.key);
-			else
-				put(this.key, value);
+			
+			WeakSoftValue<K,V> rEntry = this.realEntry.get();
+			
+			if (rEntry!=null) {
+				if (value==null) rEntry.clear();
+				else rEntry.setValue(value);
+			}
+			
 			return oldValue;
 		}
 		
@@ -303,11 +311,13 @@
 	private class EntrySetIterator implements Iterator<Entry<K,V>> {
 		
 		private final Iterator<Entry<K,WeakSoftValue<K,V>>> iter;
+		private RealEntry lastReplied;
 		private RealEntry next;
 		
 		public EntrySetIterator(Iterator<Entry<K,WeakSoftValue<K,V>>> iter) {
 			this.iter = iter;
 			expurgeNow();
+			this.lastReplied = null;
 			this.next = searchNext();
 		}
 		
@@ -322,7 +332,7 @@
 				if (val==null) throw new NoSuchElementException();
 				value = val.getValue();
 				if (value!=null) {
-					return new RealEntry(entry.getKey(), value);
+					return new RealEntry(val, value);
 				}
 			}
 			return null;
@@ -333,14 +343,15 @@
 		}
 		
 		public Entry<K,V> next() {
-			RealEntry entry = this.next;
-			if (entry==null) throw new NoSuchElementException();
+			this.lastReplied = this.next;
+			if (this.lastReplied==null) throw new NoSuchElementException();
 			this.next = searchNext();
-			return entry;
+			return this.lastReplied;
 		}
 		
 		public void remove() {
-			this.iter.remove();
+			if (this.lastReplied==null) throw new NoSuchElementException();
+			this.lastReplied.setValue(null);
 		}
 		
 	}


Mail converted by MHonArc 2.6.19+ http://listengine.tuxfamily.org/