You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@directory.apache.org by ak...@apache.org on 2004/10/28 08:49:42 UTC

svn commit: rev 55791 - in incubator/directory/ldap/trunk/common/src: java/org/apache/ldap/common/util test/org/apache/ldap/common/util

Author: akarasulu
Date: Wed Oct 27 23:49:42 2004
New Revision: 55791

Added:
   incubator/directory/ldap/trunk/common/src/java/org/apache/ldap/common/util/JoinIterator.java
   incubator/directory/ldap/trunk/common/src/test/org/apache/ldap/common/util/JoinIteratorTest.java
Log:
adding some utility classes

Added: incubator/directory/ldap/trunk/common/src/java/org/apache/ldap/common/util/JoinIterator.java
==============================================================================
--- (empty file)
+++ incubator/directory/ldap/trunk/common/src/java/org/apache/ldap/common/util/JoinIterator.java	Wed Oct 27 23:49:42 2004
@@ -0,0 +1,89 @@
+/*
+ *   Copyright 2004 The Apache Software Foundation
+ *
+ *   Licensed under the Apache License, Version 2.0 (the "License");
+ *   you may not use this file except in compliance with the License.
+ *   You may obtain a copy of the License at
+ *
+ *       http://www.apache.org/licenses/LICENSE-2.0
+ *
+ *   Unless required by applicable law or agreed to in writing, software
+ *   distributed under the License is distributed on an "AS IS" BASIS,
+ *   WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ *   See the License for the specific language governing permissions and
+ *   limitations under the License.
+ *
+ */
+package org.apache.ldap.common.util;
+
+
+import java.util.Iterator;
+import java.util.NoSuchElementException;
+
+
+/**
+ * An Iterator that joins the results of many iterators.
+ *
+ * @author <a href="mailto:directory-dev@incubator.apache.org">Apache Directory Project</a>
+ * @version $Rev$
+ */
+public class JoinIterator implements Iterator
+{
+    /** the iterators whose results are joined */
+    private final Iterator[] iterators;
+    private int index;
+
+
+    /**
+     * Creates an Iterator that joins other Iterators.
+     *
+     * @param iterators the Iterators whose results are joined
+     * @throws IllegalArgumentException if a null array argument, or one with
+     * less than 2 elements is used
+     */
+    public JoinIterator( Iterator[] iterators )
+    {
+        if ( iterators == null || iterators.length < 2 )
+        {
+            throw new IllegalArgumentException( "Iterator[] arg must not be " +
+                    "null, empty or composed of less than two Iterators" );
+        }
+
+        this.iterators = iterators;
+        this.index = 0;
+    }
+
+
+    public void remove()
+    {
+        throw new UnsupportedOperationException();
+    }
+
+
+    public boolean hasNext()
+    {
+        for ( /** nada */ ; index < iterators.length; index++ )
+        {
+            if ( iterators[index].hasNext() )
+            {
+                return true;
+            }
+        }
+
+        return false;
+    }
+
+
+    public Object next()
+    {
+        for ( /** nada */ ; index < iterators.length; index++ )
+        {
+            if ( iterators[index].hasNext() )
+            {
+                return iterators[index].next();
+            }
+        }
+
+        throw new NoSuchElementException();
+    }
+}

Added: incubator/directory/ldap/trunk/common/src/test/org/apache/ldap/common/util/JoinIteratorTest.java
==============================================================================
--- (empty file)
+++ incubator/directory/ldap/trunk/common/src/test/org/apache/ldap/common/util/JoinIteratorTest.java	Wed Oct 27 23:49:42 2004
@@ -0,0 +1,108 @@
+/*
+ *   Copyright 2004 The Apache Software Foundation
+ *
+ *   Licensed under the Apache License, Version 2.0 (the "License");
+ *   you may not use this file except in compliance with the License.
+ *   You may obtain a copy of the License at
+ *
+ *       http://www.apache.org/licenses/LICENSE-2.0
+ *
+ *   Unless required by applicable law or agreed to in writing, software
+ *   distributed under the License is distributed on an "AS IS" BASIS,
+ *   WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ *   See the License for the specific language governing permissions and
+ *   limitations under the License.
+ *
+ */
+package org.apache.ldap.common.util;
+
+
+import java.util.Collections;
+import java.util.Iterator;
+import java.util.ArrayList;
+
+import junit.framework.TestCase;
+
+
+/**
+ * Document this class.
+ *
+ * @author <a href="mailto:directory-dev@incubator.apache.org">Apache Directory Project</a>
+ * @version $Rev$
+ */
+public class JoinIteratorTest extends TestCase
+{
+    public void testNullArgument()
+    {
+        try
+        {
+            JoinIterator iterator = new JoinIterator( null );
+            fail( "Should not be able to create a JoinIterator with null args" );
+        }
+        catch ( IllegalArgumentException e )
+        {
+            assertNotNull( e );
+        }
+    }
+
+
+    public void testSingleArgument()
+    {
+        Iterator[] iterators = new Iterator[]
+            { Collections.singleton( "foo" ).iterator() };
+
+        try
+        {
+            JoinIterator iterator = new JoinIterator( iterators );
+            fail( "Should not be able to create a JoinIterator with a single Iterator" );
+        }
+        catch ( IllegalArgumentException e )
+        {
+            assertNotNull( e );
+        }
+    }
+
+
+    public void testTwoArguments()
+    {
+        Iterator[] iterators = new Iterator[]
+            { Collections.singleton( "foo" ).iterator(),
+              Collections.singleton( "bar" ).iterator()
+            };
+
+        JoinIterator iterator = new JoinIterator( iterators );
+        assertTrue( "iterator should have an element", iterator.hasNext() );
+        assertEquals( "foo", iterator.next() );
+        assertTrue( "iterator should have an element", iterator.hasNext() );
+        assertEquals( "bar", iterator.next() );
+        assertFalse( "iterator should NOT have an element", iterator.hasNext() );
+    }
+
+
+    public void testSeveralArguments()
+    {
+        ArrayList multivalued = new ArrayList();
+        multivalued.add( "foo1" );
+        multivalued.add( "foo2" );
+
+        Iterator[] iterators = new Iterator[]
+            { Collections.singleton( "foo0" ).iterator(),
+              multivalued.iterator(),
+              Collections.singleton( "bar0" ).iterator(),
+              Collections.singleton( "bar1" ).iterator()
+            };
+
+        JoinIterator iterator = new JoinIterator( iterators );
+        assertTrue( "iterator should have an element", iterator.hasNext() );
+        assertEquals( "foo0", iterator.next() );
+        assertTrue( "iterator should have an element", iterator.hasNext() );
+        assertEquals( "foo1", iterator.next() );
+        assertTrue( "iterator should have an element", iterator.hasNext() );
+        assertEquals( "foo2", iterator.next() );
+        assertTrue( "iterator should have an element", iterator.hasNext() );
+        assertEquals( "bar0", iterator.next() );
+        assertTrue( "iterator should have an element", iterator.hasNext() );
+        assertEquals( "bar1", iterator.next() );
+        assertFalse( "iterator should NOT have an element", iterator.hasNext() );
+    }
+}