commons-dev mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From Christopher Elkins <chr...@scardini.com>
Subject [PATCH][Collections] Add empty iterator to CollectionUtils
Date Wed, 21 Nov 2001 22:06:25 GMT
Hi, all.

Attached are patches to CollectionUtils and its corresponding unit test to
add support for an "empty" iterator. This is somewhat analogous to JDK2's
Collections.EMPTY_[LIST|MAP|SET] members.

-- 
Christopher Elkins


Index: src/java/org/apache/commons/collections/CollectionUtils.java
===================================================================
RCS file: /home/cvspublic/jakarta-commons/collections/src/java/org/apache/commons/collections/CollectionUtils.java,v
retrieving revision 1.5
diff -u -r1.5 CollectionUtils.java
--- src/java/org/apache/commons/collections/CollectionUtils.java	2001/08/29 16:10:29	1.5
+++ src/java/org/apache/commons/collections/CollectionUtils.java	2001/11/21 21:41:01
@@ -80,6 +80,26 @@
  * @version $Id: CollectionUtils.java,v 1.5 2001/08/29 16:10:29 jstrachan Exp $
  */
 public class CollectionUtils {
+
+    /**
+     * The empty iterator (immutable).
+     */
+    public static final Iterator EMPTY_ITERATOR = new EmptyIterator();
+
+    private static class EmptyIterator implements Iterator {
+        public boolean hasNext() {
+            return false;
+        }
+
+        public Object next() {
+            throw new NoSuchElementException();
+        }
+
+        public void remove() {
+            throw new UnsupportedOperationException();
+        }
+    }
+
     /**
      * Returns a {@link Collection} containing the union
      * of the given {@link Collection}s.
Index: src/test/org/apache/commons/collections/TestCollectionUtils.java
===================================================================
RCS file: /home/cvspublic/jakarta-commons/collections/src/test/org/apache/commons/collections/TestCollectionUtils.java,v
retrieving revision 1.2
diff -u -r1.2 TestCollectionUtils.java
--- src/test/org/apache/commons/collections/TestCollectionUtils.java	2001/07/14 23:33:27	1.2
+++ src/test/org/apache/commons/collections/TestCollectionUtils.java	2001/11/21 21:41:01
@@ -310,4 +310,19 @@
         assertTrue(CollectionUtils.isEqualCollection(a,b));
         assertTrue(CollectionUtils.isEqualCollection(b,a));
     }
+
+    public void testEmptyIterator() {
+        Iterator i = CollectionUtils.EMPTY_ITERATOR;
+        assertTrue(!i.hasNext());
+
+        try {
+            i.next();
+            fail("Should raise a NoSuchElementException");
+        } catch (NoSuchElementException e) {}
+
+        try {
+            i.remove();
+            fail("Should raise an UnsupportedOperationException");
+        } catch (UnsupportedOperationException e) {}
+    }
 }

--
To unsubscribe, e-mail:   <mailto:commons-dev-unsubscribe@jakarta.apache.org>
For additional commands, e-mail: <mailto:commons-dev-help@jakarta.apache.org>


Mime
View raw message