Add UnmodifiableOrderedMapIterator

git-svn-id: https://svn.apache.org/repos/asf/jakarta/commons/proper/collections/trunk@131358 13f79535-47bb-0310-9956-ffa450edef68
This commit is contained in:
Stephen Colebourne 2003-11-20 21:45:35 +00:00
parent fe3315d3c0
commit 8f1bdde3d7
3 changed files with 279 additions and 2 deletions

View File

@ -0,0 +1,136 @@
/*
* $Header: /home/jerenkrantz/tmp/commons/commons-convert/cvs/home/cvs/jakarta-commons//collections/src/java/org/apache/commons/collections/iterators/UnmodifiableOrderedMapIterator.java,v 1.1 2003/11/20 21:45:35 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 org.apache.commons.collections.Unmodifiable;
/**
* Decorates an ordered map iterator such that it cannot be modified.
*
* @since Commons Collections 3.0
* @version $Revision: 1.1 $ $Date: 2003/11/20 21:45:35 $
*
* @author Stephen Colebourne
*/
public final class UnmodifiableOrderedMapIterator implements OrderedMapIterator, Unmodifiable {
/** The iterator being decorated */
private OrderedMapIterator 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 OrderedMapIterator decorate(OrderedMapIterator iterator) {
if (iterator == null) {
throw new IllegalArgumentException("OrderedMapIterator must not be null");
}
if (iterator instanceof Unmodifiable) {
return iterator;
}
return new UnmodifiableOrderedMapIterator(iterator);
}
//-----------------------------------------------------------------------
/**
* Constructor.
*
* @param iterator the iterator to decoarate
*/
protected UnmodifiableOrderedMapIterator(OrderedMapIterator iterator) {
super();
this.iterator = iterator;
}
//-----------------------------------------------------------------------
public boolean hasNext() {
return iterator.hasNext();
}
public Object next() {
return iterator.next();
}
public boolean hasPrevious() {
return iterator.hasPrevious();
}
public Object previous() {
return iterator.previous();
}
public Object getKey() {
return iterator.getKey();
}
public Object getValue() {
return iterator.getValue();
}
public Object setValue(Object value) {
throw new UnsupportedOperationException("setValue() is not supported");
}
public void remove() {
throw new UnsupportedOperationException("remove() 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.9 2003/11/02 18:29:59 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.10 2003/11/20 21:45:35 scolebourne Exp $
* ====================================================================
*
* The Apache Software License, Version 1.1
@ -64,7 +64,7 @@ import junit.framework.TestSuite;
/**
* Entry point for all iterator tests.
*
* @version $Revision: 1.9 $ $Date: 2003/11/02 18:29:59 $
* @version $Revision: 1.10 $ $Date: 2003/11/20 21:45:35 $
*
* @author Rodney Waldhoff
*/
@ -95,6 +95,7 @@ public class TestAll extends TestCase {
suite.addTest(TestUnmodifiableIterator.suite());
suite.addTest(TestUnmodifiableListIterator.suite());
suite.addTest(TestUnmodifiableMapIterator.suite());
suite.addTest(TestUnmodifiableOrderedMapIterator.suite());
return suite;
}

View File

@ -0,0 +1,140 @@
/*
* $Header: /home/jerenkrantz/tmp/commons/commons-convert/cvs/home/cvs/jakarta-commons//collections/src/test/org/apache/commons/collections/iterators/TestUnmodifiableOrderedMapIterator.java,v 1.1 2003/11/20 21:45:35 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.HashMap;
import java.util.Map;
import java.util.TreeMap;
import junit.framework.Test;
import junit.framework.TestSuite;
import org.apache.commons.collections.Unmodifiable;
import org.apache.commons.collections.map.ListOrderedMap;
import org.apache.commons.collections.map.OrderedMap;
/**
* Tests the UnmodifiableOrderedMapIterator.
*
* @version $Revision: 1.1 $ $Date: 2003/11/20 21:45:35 $
*
* @author Stephen Colebourne
*/
public class TestUnmodifiableOrderedMapIterator extends AbstractTestOrderedMapIterator {
public static Test suite() {
return new TestSuite(TestUnmodifiableOrderedMapIterator.class);
}
public TestUnmodifiableOrderedMapIterator(String testName) {
super(testName);
}
public MapIterator makeEmptyMapIterator() {
return UnmodifiableOrderedMapIterator.decorate(
ListOrderedMap.decorate(new HashMap()).orderedMapIterator());
}
public MapIterator makeFullMapIterator() {
return UnmodifiableOrderedMapIterator.decorate(
((OrderedMap) getMap()).orderedMapIterator());
}
public Map getMap() {
Map testMap = ListOrderedMap.decorate(new HashMap());
testMap.put("A", "a");
testMap.put("B", "b");
testMap.put("C", "c");
return testMap;
}
public Map getConfirmedMap() {
Map testMap = new TreeMap();
testMap.put("A", "a");
testMap.put("B", "b");
testMap.put("C", "c");
return testMap;
}
public boolean supportsRemove() {
return false;
}
public boolean supportsSetValue() {
return false;
}
//-----------------------------------------------------------------------
public void testOrderedMapIterator() {
assertTrue(makeEmptyOrderedMapIterator() instanceof Unmodifiable);
}
public void testDecorateFactory() {
OrderedMapIterator it = makeFullOrderedMapIterator();
assertSame(it, UnmodifiableOrderedMapIterator.decorate(it));
it = ((OrderedMap) getMap()).orderedMapIterator() ;
assertTrue(it != UnmodifiableOrderedMapIterator.decorate(it));
try {
UnmodifiableOrderedMapIterator.decorate(null);
fail();
} catch (IllegalArgumentException ex) {}
}
}