You are viewing a plain text version of this content. The canonical link for it is here.
Posted to dev@commons.apache.org by Christopher Elkins <ch...@scardini.com> on 2001/11/21 23:06:25 UTC

[PATCH][Collections] Add empty iterator to CollectionUtils

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:   <ma...@jakarta.apache.org>
For additional commands, e-mail: <ma...@jakarta.apache.org>


Re: [PATCH][Collections] Add empty iterator to CollectionUtils

Posted by James Strachan <ja...@yahoo.co.uk>.
Just a thought. How about doing this...

public class CollectionUtils {

    public static final Iterator EMPTY_ITERATOR
        = Collections.EMPTY_LIST.iterator();

...
}

As the implementation of EmptyList in Collections already contains an
implementation of an EmptyIterator. Though for some reason
EMPTY_LIST.iterator() creates a new instance each time.

James
----- Original Message -----
From: "Christopher Elkins" <ch...@scardini.com>
To: "Jakarta Commons Developers List" <co...@jakarta.apache.org>
Sent: Thursday, November 22, 2001 6:14 AM
Subject: Re: [PATCH][Collections] Add empty iterator to CollectionUtils


> On Wed, Nov 21, 2001 at 11:58:07PM -0500, bayard@generationjava.com wrote:
> >
> > What's your view on making the EmptyIterator class a proper public
class.
> > Is there a strong reason for deciding that all EmptyIterators must be
> > the same instance?
> >
> > It's probably just a style thing, but I wanted to check.
>
> There are a few reasons:
>   - It parallels the set theory notion that all empty sets are really the
>     same empty set.
>   - It parallels Collections.EMPTY_[LIST|MAP|SET].
>   - Because it's immutable it's perfectly valid to return the same
instance.
>     (It also offers a minimal performance gain.)
>
> Ultimately I guess it is a matter of style. Indeed, my local
implementation
> is a proper class; I took the opportunity of contributing it to the
Commons
> as an excuse for implementing it the right way (where "right" is justified
> using the above reasons). :-)
>
> --
> Christopher Elkins
>
> --
> To unsubscribe, e-mail:
<ma...@jakarta.apache.org>
> For additional commands, e-mail:
<ma...@jakarta.apache.org>


_________________________________________________________
Do You Yahoo!?
Get your free @yahoo.com address at http://mail.yahoo.com


--
To unsubscribe, e-mail:   <ma...@jakarta.apache.org>
For additional commands, e-mail: <ma...@jakarta.apache.org>


Re: [PATCH][Collections] Add empty iterator to CollectionUtils

Posted by Christopher Elkins <ch...@scardini.com>.
On Wed, Nov 21, 2001 at 11:58:07PM -0500, bayard@generationjava.com wrote:
> 
> What's your view on making the EmptyIterator class a proper public class.
> Is there a strong reason for deciding that all EmptyIterators must be
> the same instance?
> 
> It's probably just a style thing, but I wanted to check.

There are a few reasons:
  - It parallels the set theory notion that all empty sets are really the
    same empty set.
  - It parallels Collections.EMPTY_[LIST|MAP|SET].
  - Because it's immutable it's perfectly valid to return the same instance.
    (It also offers a minimal performance gain.)

Ultimately I guess it is a matter of style. Indeed, my local implementation
is a proper class; I took the opportunity of contributing it to the Commons
as an excuse for implementing it the right way (where "right" is justified
using the above reasons). :-)

-- 
Christopher Elkins

--
To unsubscribe, e-mail:   <ma...@jakarta.apache.org>
For additional commands, e-mail: <ma...@jakarta.apache.org>


Re: [PATCH][Collections] Add empty iterator to CollectionUtils

Posted by ba...@generationjava.com.
Hi Christopher,

I wanted to ask one question concerning your patch,

What's your view on making the EmptyIterator class a proper public class.
Is there a strong reason for deciding that all EmptyIterators must be
the same instance?

It's probably just a style thing, but I wanted to check.

Thanks for the submission,

Bay

On Wed, 21 Nov 2001, Christopher Elkins wrote:

> 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:   <ma...@jakarta.apache.org>
> For additional commands, e-mail: <ma...@jakarta.apache.org>
>
>


--
To unsubscribe, e-mail:   <ma...@jakarta.apache.org>
For additional commands, e-mail: <ma...@jakarta.apache.org>