Add UnmodifiableIterator classes to iterators package

git-svn-id: https://svn.apache.org/repos/asf/jakarta/commons/proper/collections/trunk@131316 13f79535-47bb-0310-9956-ffa450edef68
This commit is contained in:
Stephen Colebourne 2003-11-02 17:26:36 +00:00
parent e1f52a4797
commit 60fcc4fb8d
5 changed files with 516 additions and 2 deletions

View File

@ -0,0 +1,120 @@
/*
* $Header: /home/jerenkrantz/tmp/commons/commons-convert/cvs/home/cvs/jakarta-commons//collections/src/java/org/apache/commons/collections/iterators/UnmodifiableIterator.java,v 1.1 2003/11/02 17:26:36 scolebourne Exp $
* ====================================================================
*
* The Apache Software License, Version 1.1
*
* Copyright (c) 1999-2003 The Apache Software Foundation. All rights
* reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
*
* 1. Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
*
* 2. Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in
* the documentation and/or other materials provided with the
* distribution.
*
* 3. The end-user documentation included with the redistribution, if
* any, must include the following acknowledgement:
* "This product includes software developed by the
* Apache Software Foundation (http://www.apache.org/)."
* Alternately, this acknowledgement may appear in the software itself,
* if and wherever such third-party acknowledgements normally appear.
*
* 4. The names "The Jakarta Project", "Commons", and "Apache Software
* Foundation" must not be used to endorse or promote products derived
* from this software without prior written permission. For written
* permission, please contact apache@apache.org.
*
* 5. Products derived from this software may not be called "Apache"
* nor may "Apache" appear in their names without prior written
* permission of the Apache Software Foundation.
*
* THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED
* WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
* OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
* DISCLAIMED. IN NO EVENT SHALL THE APACHE SOFTWARE FOUNDATION OR
* ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
* SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
* LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
* USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
* ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
* OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
* OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
* SUCH DAMAGE.
* ====================================================================
*
* This software consists of voluntary contributions made by many
* individuals on behalf of the Apache Software Foundation. For more
* information on the Apache Software Foundation, please see
* <http://www.apache.org/>.
*
*/
package org.apache.commons.collections.iterators;
import java.util.Iterator;
import org.apache.commons.collections.Unmodifiable;
/**
* Decorates an iterator such that it cannot be modified.
*
* @since Commons Collections 3.0
* @version $Revision: 1.1 $ $Date: 2003/11/02 17:26:36 $
*
* @author Stephen Colebourne
*/
public final class UnmodifiableIterator implements Iterator, Unmodifiable {
/** The iterator being decorated */
private Iterator iterator;
//-----------------------------------------------------------------------
/**
* Decorates the specified iterator such that it cannot be modified.
* <p>
* If the iterator is already unmodifiable it is returned directly.
*
* @param iterator the iterator to decoarate
* @throws IllegalArgumentException if the iterator is null
*/
public static Iterator decorate(Iterator iterator) {
if (iterator == null) {
throw new IllegalArgumentException("Iterator must not be null");
}
if (iterator instanceof Unmodifiable) {
return iterator;
}
return new UnmodifiableIterator(iterator);
}
//-----------------------------------------------------------------------
/**
* Constructor.
*
* @param iterator the iterator to decoarate
*/
protected UnmodifiableIterator(Iterator iterator) {
super();
this.iterator = iterator;
}
//-----------------------------------------------------------------------
public boolean hasNext() {
return iterator.hasNext();
}
public Object next() {
return iterator.next();
}
public void remove() {
throw new UnsupportedOperationException("remove() is not supported");
}
}

View File

@ -0,0 +1,142 @@
/*
* $Header: /home/jerenkrantz/tmp/commons/commons-convert/cvs/home/cvs/jakarta-commons//collections/src/java/org/apache/commons/collections/iterators/UnmodifiableListIterator.java,v 1.1 2003/11/02 17:26:36 scolebourne Exp $
* ====================================================================
*
* The Apache Software License, Version 1.1
*
* Copyright (c) 1999-2003 The Apache Software Foundation. All rights
* reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
*
* 1. Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
*
* 2. Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in
* the documentation and/or other materials provided with the
* distribution.
*
* 3. The end-user documentation included with the redistribution, if
* any, must include the following acknowledgement:
* "This product includes software developed by the
* Apache Software Foundation (http://www.apache.org/)."
* Alternately, this acknowledgement may appear in the software itself,
* if and wherever such third-party acknowledgements normally appear.
*
* 4. The names "The Jakarta Project", "Commons", and "Apache Software
* Foundation" must not be used to endorse or promote products derived
* from this software without prior written permission. For written
* permission, please contact apache@apache.org.
*
* 5. Products derived from this software may not be called "Apache"
* nor may "Apache" appear in their names without prior written
* permission of the Apache Software Foundation.
*
* THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED
* WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
* OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
* DISCLAIMED. IN NO EVENT SHALL THE APACHE SOFTWARE FOUNDATION OR
* ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
* SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
* LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
* USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
* ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
* OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
* OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
* SUCH DAMAGE.
* ====================================================================
*
* This software consists of voluntary contributions made by many
* individuals on behalf of the Apache Software Foundation. For more
* information on the Apache Software Foundation, please see
* <http://www.apache.org/>.
*
*/
package org.apache.commons.collections.iterators;
import java.util.ListIterator;
import org.apache.commons.collections.Unmodifiable;
/**
* Decorates a list iterator such that it cannot be modified.
*
* @since Commons Collections 3.0
* @version $Revision: 1.1 $ $Date: 2003/11/02 17:26:36 $
*
* @author Stephen Colebourne
*/
public final class UnmodifiableListIterator implements ListIterator, Unmodifiable {
/** The iterator being decorated */
private ListIterator iterator;
//-----------------------------------------------------------------------
/**
* Decorates the specified iterator such that it cannot be modified.
*
* @param iterator the iterator to decoarate
* @throws IllegalArgumentException if the iterator is null
*/
public static ListIterator decorate(ListIterator iterator) {
if (iterator == null) {
throw new IllegalArgumentException("ListIterator must not be null");
}
if (iterator instanceof Unmodifiable) {
return iterator;
}
return new UnmodifiableListIterator(iterator);
}
//-----------------------------------------------------------------------
/**
* Constructor.
*
* @param iterator the iterator to decoarate
*/
protected UnmodifiableListIterator(ListIterator iterator) {
super();
this.iterator = iterator;
}
//-----------------------------------------------------------------------
public boolean hasNext() {
return iterator.hasNext();
}
public Object next() {
return iterator.next();
}
public int nextIndex() {
return iterator.nextIndex();
}
public boolean hasPrevious() {
return iterator.hasPrevious();
}
public Object previous() {
return iterator.previous();
}
public int previousIndex() {
return iterator.previousIndex();
}
public void remove() {
throw new UnsupportedOperationException("remove() is not supported");
}
public void set(Object obj) {
throw new UnsupportedOperationException("set() is not supported");
}
public void add(Object obj) {
throw new UnsupportedOperationException("add() is not supported");
}
}

View File

@ -1,5 +1,5 @@
/* /*
* $Header: /home/jerenkrantz/tmp/commons/commons-convert/cvs/home/cvs/jakarta-commons//collections/src/test/org/apache/commons/collections/iterators/TestAll.java,v 1.7 2003/10/05 21:26:46 scolebourne Exp $ * $Header: /home/jerenkrantz/tmp/commons/commons-convert/cvs/home/cvs/jakarta-commons//collections/src/test/org/apache/commons/collections/iterators/TestAll.java,v 1.8 2003/11/02 17:26:36 scolebourne Exp $
* ==================================================================== * ====================================================================
* *
* The Apache Software License, Version 1.1 * The Apache Software License, Version 1.1
@ -64,7 +64,7 @@ import junit.framework.TestSuite;
/** /**
* Entry point for all iterator tests. * Entry point for all iterator tests.
* *
* @version $Revision: 1.7 $ $Date: 2003/10/05 21:26:46 $ * @version $Revision: 1.8 $ $Date: 2003/11/02 17:26:36 $
* *
* @author Rodney Waldhoff * @author Rodney Waldhoff
*/ */
@ -92,6 +92,8 @@ public class TestAll extends TestCase {
suite.addTest(TestSingletonIterator.suite()); suite.addTest(TestSingletonIterator.suite());
suite.addTest(TestSingletonListIterator.suite()); suite.addTest(TestSingletonListIterator.suite());
suite.addTest(TestUniqueFilterIterator.suite()); suite.addTest(TestUniqueFilterIterator.suite());
suite.addTest(TestUnmodifiableIterator.suite());
suite.addTest(TestUnmodifiableListIterator.suite());
return suite; return suite;
} }

View File

@ -0,0 +1,121 @@
/*
* $Header: /home/jerenkrantz/tmp/commons/commons-convert/cvs/home/cvs/jakarta-commons//collections/src/test/org/apache/commons/collections/iterators/TestUnmodifiableIterator.java,v 1.1 2003/11/02 17:26:36 scolebourne Exp $
* ====================================================================
*
* The Apache Software License, Version 1.1
*
* Copyright (c) 2001-2003 The Apache Software Foundation. All rights
* reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
*
* 1. Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
*
* 2. Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in
* the documentation and/or other materials provided with the
* distribution.
*
* 3. The end-user documentation included with the redistribution, if
* any, must include the following acknowledgement:
* "This product includes software developed by the
* Apache Software Foundation (http://www.apache.org/)."
* Alternately, this acknowledgement may appear in the software itself,
* if and wherever such third-party acknowledgements normally appear.
*
* 4. The names "The Jakarta Project", "Commons", and "Apache Software
* Foundation" must not be used to endorse or promote products derived
* from this software without prior written permission. For written
* permission, please contact apache@apache.org.
*
* 5. Products derived from this software may not be called "Apache"
* nor may "Apache" appear in their names without prior written
* permission of the Apache Software Foundation.
*
* THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED
* WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
* OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
* DISCLAIMED. IN NO EVENT SHALL THE APACHE SOFTWARE FOUNDATION OR
* ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
* SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
* LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
* USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
* ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
* OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
* OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
* SUCH DAMAGE.
* ====================================================================
*
* This software consists of voluntary contributions made by many
* individuals on behalf of the Apache Software Foundation. For more
* information on the Apache Software Foundation, please see
* <http://www.apache.org/>.
*
*/
package org.apache.commons.collections.iterators;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collections;
import java.util.Iterator;
import java.util.List;
import junit.framework.Test;
import junit.framework.TestSuite;
import org.apache.commons.collections.Unmodifiable;
/**
* Tests the UnmodifiableIterator.
*
* @version $Revision: 1.1 $ $Date: 2003/11/02 17:26:36 $
*
* @author Stephen Colebourne
*/
public class TestUnmodifiableIterator extends AbstractTestIterator {
protected String[] testArray = { "One", "Two", "Three" };
protected List testList = new ArrayList(Arrays.asList(testArray));
public static Test suite() {
return new TestSuite(TestUnmodifiableIterator.class);
}
public TestUnmodifiableIterator(String testName) {
super(testName);
}
public Iterator makeEmptyIterator() {
return UnmodifiableIterator.decorate(Collections.EMPTY_LIST.iterator());
}
public Iterator makeFullIterator() {
return UnmodifiableIterator.decorate(testList.iterator());
}
public boolean supportsRemove() {
return false;
}
//-----------------------------------------------------------------------
public void testIterator() {
assertTrue(makeEmptyIterator() instanceof Unmodifiable);
}
public void testDecorateFactory() {
Iterator it = makeFullIterator();
assertSame(it, UnmodifiableIterator.decorate(it));
it = testList.iterator();
assertTrue(it != UnmodifiableIterator.decorate(it));
try {
UnmodifiableIterator.decorate(null);
fail();
} catch (IllegalArgumentException ex) {}
}
}

View File

@ -0,0 +1,129 @@
/*
* $Header: /home/jerenkrantz/tmp/commons/commons-convert/cvs/home/cvs/jakarta-commons//collections/src/test/org/apache/commons/collections/iterators/TestUnmodifiableListIterator.java,v 1.1 2003/11/02 17:26:36 scolebourne Exp $
* ====================================================================
*
* The Apache Software License, Version 1.1
*
* Copyright (c) 2001-2003 The Apache Software Foundation. All rights
* reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
*
* 1. Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
*
* 2. Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in
* the documentation and/or other materials provided with the
* distribution.
*
* 3. The end-user documentation included with the redistribution, if
* any, must include the following acknowledgement:
* "This product includes software developed by the
* Apache Software Foundation (http://www.apache.org/)."
* Alternately, this acknowledgement may appear in the software itself,
* if and wherever such third-party acknowledgements normally appear.
*
* 4. The names "The Jakarta Project", "Commons", and "Apache Software
* Foundation" must not be used to endorse or promote products derived
* from this software without prior written permission. For written
* permission, please contact apache@apache.org.
*
* 5. Products derived from this software may not be called "Apache"
* nor may "Apache" appear in their names without prior written
* permission of the Apache Software Foundation.
*
* THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED
* WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
* OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
* DISCLAIMED. IN NO EVENT SHALL THE APACHE SOFTWARE FOUNDATION OR
* ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
* SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
* LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
* USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
* ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
* OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
* OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
* SUCH DAMAGE.
* ====================================================================
*
* This software consists of voluntary contributions made by many
* individuals on behalf of the Apache Software Foundation. For more
* information on the Apache Software Foundation, please see
* <http://www.apache.org/>.
*
*/
package org.apache.commons.collections.iterators;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collections;
import java.util.List;
import java.util.ListIterator;
import junit.framework.Test;
import junit.framework.TestSuite;
import org.apache.commons.collections.Unmodifiable;
/**
* Tests the UnmodifiableListIterator.
*
* @version $Revision: 1.1 $ $Date: 2003/11/02 17:26:36 $
*
* @author Stephen Colebourne
*/
public class TestUnmodifiableListIterator extends AbstractTestListIterator {
protected String[] testArray = { "One", "Two", "Three" };
protected List testList = new ArrayList(Arrays.asList(testArray));
public static Test suite() {
return new TestSuite(TestUnmodifiableListIterator.class);
}
public TestUnmodifiableListIterator(String testName) {
super(testName);
}
public ListIterator makeEmptyListIterator() {
return UnmodifiableListIterator.decorate(Collections.EMPTY_LIST.listIterator());
}
public ListIterator makeFullListIterator() {
return UnmodifiableListIterator.decorate(testList.listIterator());
}
public boolean supportsRemove() {
return false;
}
public boolean supportsAdd() {
return false;
}
public boolean supportsSet() {
return false;
}
//-----------------------------------------------------------------------
public void testListIterator() {
assertTrue(makeEmptyListIterator() instanceof Unmodifiable);
}
public void testDecorateFactory() {
ListIterator it = makeFullListIterator();
assertSame(it, UnmodifiableListIterator.decorate(it));
it = testList.listIterator();
assertTrue(it != UnmodifiableListIterator.decorate(it));
try {
UnmodifiableListIterator.decorate(null);
fail();
} catch (IllegalArgumentException ex) {}
}
}