From 3d6c8c5fabeeb9e5c30ab4ec1339e242b2e4e145 Mon Sep 17 00:00:00 2001 From: Henri Yandell Date: Mon, 26 Nov 2001 17:15:33 +0000 Subject: [PATCH] An empty iterator. It is immutable and always the same object instance. Submitted by: Christopher Elkins git-svn-id: https://svn.apache.org/repos/asf/jakarta/commons/proper/collections/trunk@130508 13f79535-47bb-0310-9956-ffa450edef68 --- .../commons/collections/CollectionUtils.java | 34 ++++++++++++++++--- 1 file changed, 30 insertions(+), 4 deletions(-) diff --git a/src/java/org/apache/commons/collections/CollectionUtils.java b/src/java/org/apache/commons/collections/CollectionUtils.java index 090ed40ef..52f597a93 100644 --- a/src/java/org/apache/commons/collections/CollectionUtils.java +++ b/src/java/org/apache/commons/collections/CollectionUtils.java @@ -1,7 +1,7 @@ /* - * $Header: /home/jerenkrantz/tmp/commons/commons-convert/cvs/home/cvs/jakarta-commons//collections/src/java/org/apache/commons/collections/CollectionUtils.java,v 1.5 2001/08/29 16:10:29 jstrachan Exp $ - * $Revision: 1.5 $ - * $Date: 2001/08/29 16:10:29 $ + * $Header: /home/jerenkrantz/tmp/commons/commons-convert/cvs/home/cvs/jakarta-commons//collections/src/java/org/apache/commons/collections/CollectionUtils.java,v 1.6 2001/11/26 17:15:33 bayard Exp $ + * $Revision: 1.6 $ + * $Date: 2001/11/26 17:15:33 $ * * ==================================================================== * @@ -77,9 +77,34 @@ import java.util.Set; * * @author Rodney Waldhoff * - * @version $Id: CollectionUtils.java,v 1.5 2001/08/29 16:10:29 jstrachan Exp $ + * @version $Id: CollectionUtils.java,v 1.6 2001/11/26 17:15:33 bayard Exp $ */ public class CollectionUtils { + + /** + * The empty iterator (immutable). + */ + public static final Iterator EMPTY_ITERATOR = new EmptyIterator(); + + /** + * 'Hidden' class which acts as an EmptyIterator. + * An alternative is to use: Collections.EMPTY_LIST.iterator(); + * however that will create a new iterator object each time. + */ + 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. @@ -503,6 +528,7 @@ public class CollectionUtils { } } + /** Reverses the order of the given array */ public static void reverseArray(Object[] array) { int i = 0;