svn commit: r889759 - /ofbiz/trunk/framework/base/src/org/ofbiz/base/util/collections/IteratorWrapper.java

Previous Topic Next Topic
 
classic Classic list List threaded Threaded
1 message Options
Reply | Threaded
Open this post in threaded view
|

svn commit: r889759 - /ofbiz/trunk/framework/base/src/org/ofbiz/base/util/collections/IteratorWrapper.java

doogie-3
Author: doogie
Date: Fri Dec 11 18:42:33 2009
New Revision: 889759

URL: http://svn.apache.org/viewvc?rev=889759&view=rev
Log:
Inverted wrapped hasNext/next processing, so that
ConcurrentModificationException support will work, thru an isValid
helper call.

Modified:
    ofbiz/trunk/framework/base/src/org/ofbiz/base/util/collections/IteratorWrapper.java

Modified: ofbiz/trunk/framework/base/src/org/ofbiz/base/util/collections/IteratorWrapper.java
URL: http://svn.apache.org/viewvc/ofbiz/trunk/framework/base/src/org/ofbiz/base/util/collections/IteratorWrapper.java?rev=889759&r1=889758&r2=889759&view=diff
==============================================================================
--- ofbiz/trunk/framework/base/src/org/ofbiz/base/util/collections/IteratorWrapper.java (original)
+++ ofbiz/trunk/framework/base/src/org/ofbiz/base/util/collections/IteratorWrapper.java Fri Dec 11 18:42:33 2009
@@ -23,6 +23,7 @@
 
 public abstract class IteratorWrapper<DEST, SRC> implements Iterator<DEST> {
     private final Iterator<? extends SRC> it;
+    private boolean nextCalled;
     private DEST lastDest;
     private SRC lastSrc;
 
@@ -31,18 +32,25 @@
     }
 
     public boolean hasNext() {
-        return it.hasNext();
+        if (nextCalled) return true;
+        if (!it.hasNext()) return false;
+        do {
+            lastSrc = it.next();
+            if (isValid(lastSrc)) {
+                nextCalled = true;
+                lastDest = convert(lastSrc);
+                return true;
+            }
+        } while (it.hasNext());
+        return false;
     }
 
     public DEST next() {
-        try {
-            lastSrc = it.next();
-            return lastDest = convert(lastSrc);
-        } catch (NoSuchElementException e) {
-            lastDest = null;
-            lastSrc = null;
-            throw e;
+        if (!nextCalled) {
+            if (!hasNext()) throw new NoSuchElementException();
         }
+        nextCalled = false;
+        return lastDest;
     }
 
     public void remove() {
@@ -56,6 +64,10 @@
         }
     }
 
+    protected boolean isValid(SRC src) {
+        return true;
+    }
+
     protected abstract void noteRemoval(DEST dest, SRC src);
     protected abstract DEST convert(SRC src);
 }