Return-Path: Delivered-To: apmail-incubator-directory-cvs-archive@www.apache.org Received: (qmail 21240 invoked from network); 28 Oct 2004 06:49:45 -0000 Received: from hermes.apache.org (HELO mail.apache.org) (209.237.227.199) by minotaur-2.apache.org with SMTP; 28 Oct 2004 06:49:45 -0000 Received: (qmail 33935 invoked by uid 500); 28 Oct 2004 06:49:45 -0000 Delivered-To: apmail-incubator-directory-cvs-archive@incubator.apache.org Received: (qmail 33887 invoked by uid 500); 28 Oct 2004 06:49:44 -0000 Mailing-List: contact directory-cvs-help@incubator.apache.org; run by ezmlm Precedence: bulk List-Help: List-Unsubscribe: List-Post: List-Id: Reply-To: directory-dev@incubator.apache.org Delivered-To: mailing list directory-cvs@incubator.apache.org Received: (qmail 33874 invoked by uid 99); 28 Oct 2004 06:49:44 -0000 X-ASF-Spam-Status: No, hits=-10.0 required=10.0 tests=ALL_TRUSTED,NO_REAL_NAME X-Spam-Check-By: apache.org Received: from [209.237.227.194] (HELO minotaur.apache.org) (209.237.227.194) by apache.org (qpsmtpd/0.28) with SMTP; Wed, 27 Oct 2004 23:49:44 -0700 Received: (qmail 21194 invoked by uid 65534); 28 Oct 2004 06:49:42 -0000 Date: 28 Oct 2004 06:49:42 -0000 Message-ID: <20041028064942.21191.qmail@minotaur.apache.org> From: akarasulu@apache.org To: directory-cvs@incubator.apache.org Subject: svn commit: rev 55791 - in incubator/directory/ldap/trunk/common/src: java/org/apache/ldap/common/util test/org/apache/ldap/common/util X-Virus-Checked: Checked X-Spam-Rating: minotaur-2.apache.org 1.6.2 0/1000/N 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 Apache Directory Project + * @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 Apache Directory Project + * @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() ); + } +}