You are viewing a plain text version of this content. The canonical link for it is here.
Posted to dev@commons.apache.org by sc...@apache.org on 2003/11/08 19:43:13 UTC
cvs commit: jakarta-commons/collections/src/java/org/apache/commons/collections/iterators OrderedMapIterator.java EntrySetMapIterator.java ResetableOrderedMapIterator.java AbstractMapIteratorDecorator.java UnmodifiableMapIterator.java MapIterator.java DefaultMapIterator.java
scolebourne 2003/11/08 10:43:13
Modified: collections/src/java/org/apache/commons/collections
IteratorUtils.java
collections/src/java/org/apache/commons/collections/decorators
OrderedMap.java
collections/src/java/org/apache/commons/collections/iterators
AbstractMapIteratorDecorator.java
UnmodifiableMapIterator.java MapIterator.java
Added: collections/src/java/org/apache/commons/collections/iterators
OrderedMapIterator.java EntrySetMapIterator.java
ResetableOrderedMapIterator.java
Removed: collections/src/java/org/apache/commons/collections/iterators
DefaultMapIterator.java
Log:
Update MapIterator to remove asMapEntry
Add OrderedMapIterator
Rename DefaultMapIterator
Revision Changes Path
1.15 +37 -6 jakarta-commons/collections/src/java/org/apache/commons/collections/IteratorUtils.java
Index: IteratorUtils.java
===================================================================
RCS file: /home/cvs/jakarta-commons/collections/src/java/org/apache/commons/collections/IteratorUtils.java,v
retrieving revision 1.14
retrieving revision 1.15
diff -u -r1.14 -r1.15
--- IteratorUtils.java 2 Nov 2003 15:27:53 -0000 1.14
+++ IteratorUtils.java 8 Nov 2003 18:43:12 -0000 1.15
@@ -70,7 +70,6 @@
import java.util.ListIterator;
import java.util.Map;
import java.util.NoSuchElementException;
-import java.util.Map.Entry;
import org.apache.commons.collections.iterators.ArrayIterator;
import org.apache.commons.collections.iterators.ArrayListIterator;
@@ -87,6 +86,7 @@
import org.apache.commons.collections.iterators.ResetableIterator;
import org.apache.commons.collections.iterators.ResetableListIterator;
import org.apache.commons.collections.iterators.ResetableMapIterator;
+import org.apache.commons.collections.iterators.ResetableOrderedMapIterator;
import org.apache.commons.collections.iterators.SingletonIterator;
import org.apache.commons.collections.iterators.SingletonListIterator;
import org.apache.commons.collections.iterators.TransformIterator;
@@ -118,6 +118,10 @@
* A map iterator over no elements
*/
public static final ResetableMapIterator EMPTY_MAP_ITERATOR = new EmptyMapIterator();
+ /**
+ * A map iterator over no elements
+ */
+ public static final ResetableOrderedMapIterator EMPTY_ORDERED_MAP_ITERATOR = new EmptyOrderedMapIterator();
/**
* Prevents instantiation.
@@ -165,6 +169,18 @@
}
/**
+ * Gets an empty ordered map iterator.
+ * <p>
+ * This iterator is a valid map iterator object that will iterate
+ * over nothing.
+ *
+ * @return a list iterator over nothing
+ */
+ public static ResetableOrderedMapIterator emptyOrderedMapIterator() {
+ return EMPTY_ORDERED_MAP_ITERATOR;
+ }
+
+ /**
* Gets a singleton iterator.
* <p>
* This iterator is a valid iterator object that will iterate over
@@ -850,7 +866,7 @@
EmptyMapIterator() {
super();
}
-
+
public Object getKey() {
throw new IllegalStateException("Iterator contains no elements");
}
@@ -862,9 +878,24 @@
public Object setValue(Object value) {
throw new IllegalStateException("Iterator contains no elements");
}
+ }
+
+ //-----------------------------------------------------------------------
+ /**
+ * EmptyOrderedMapIterator class
+ */
+ static class EmptyOrderedMapIterator extends EmptyMapIterator implements ResetableOrderedMapIterator {
- public Entry asMapEntry() {
- throw new IllegalStateException("Iterator contains no elements");
+ EmptyOrderedMapIterator() {
+ super();
+ }
+
+ public boolean hasPrevious() {
+ return false;
+ }
+
+ public Object previous() {
+ throw new NoSuchElementException("Iterator contains no elements");
}
}
1.6 +4 -4 jakarta-commons/collections/src/java/org/apache/commons/collections/decorators/OrderedMap.java
Index: OrderedMap.java
===================================================================
RCS file: /home/cvs/jakarta-commons/collections/src/java/org/apache/commons/collections/decorators/OrderedMap.java,v
retrieving revision 1.5
retrieving revision 1.6
diff -u -r1.5 -r1.6
--- OrderedMap.java 4 Nov 2003 23:36:23 -0000 1.5
+++ OrderedMap.java 8 Nov 2003 18:43:12 -0000 1.6
@@ -66,7 +66,7 @@
import java.util.Map;
import java.util.Set;
-import org.apache.commons.collections.iterators.DefaultMapIterator;
+import org.apache.commons.collections.iterators.EntrySetMapIterator;
import org.apache.commons.collections.iterators.MapIterator;
import org.apache.commons.collections.pairs.AbstractMapEntry;
@@ -146,7 +146,7 @@
//-----------------------------------------------------------------------
public MapIterator mapIterator() {
- return new DefaultMapIterator(this);
+ return new EntrySetMapIterator(this);
}
public Set keySet() {
1.2 +2 -8 jakarta-commons/collections/src/java/org/apache/commons/collections/iterators/AbstractMapIteratorDecorator.java
Index: AbstractMapIteratorDecorator.java
===================================================================
RCS file: /home/cvs/jakarta-commons/collections/src/java/org/apache/commons/collections/iterators/AbstractMapIteratorDecorator.java,v
retrieving revision 1.1
retrieving revision 1.2
diff -u -r1.1 -r1.2
--- AbstractMapIteratorDecorator.java 2 Nov 2003 16:29:12 -0000 1.1
+++ AbstractMapIteratorDecorator.java 8 Nov 2003 18:43:12 -0000 1.2
@@ -57,8 +57,6 @@
*/
package org.apache.commons.collections.iterators;
-import java.util.Map;
-
/**
* Provides basic behaviour for decorating a map iterator with extra functionality.
* <p>
@@ -121,10 +119,6 @@
public Object setValue(Object obj) {
return iterator.setValue(obj);
- }
-
- public Map.Entry asMapEntry() {
- return iterator.asMapEntry();
}
}
1.2 +2 -9 jakarta-commons/collections/src/java/org/apache/commons/collections/iterators/UnmodifiableMapIterator.java
Index: UnmodifiableMapIterator.java
===================================================================
RCS file: /home/cvs/jakarta-commons/collections/src/java/org/apache/commons/collections/iterators/UnmodifiableMapIterator.java,v
retrieving revision 1.1
retrieving revision 1.2
diff -u -r1.1 -r1.2
--- UnmodifiableMapIterator.java 2 Nov 2003 18:29:59 -0000 1.1
+++ UnmodifiableMapIterator.java 8 Nov 2003 18:43:13 -0000 1.2
@@ -57,10 +57,7 @@
*/
package org.apache.commons.collections.iterators;
-import java.util.Map.Entry;
-
import org.apache.commons.collections.Unmodifiable;
-import org.apache.commons.collections.pairs.UnmodifiableMapEntry;
/**
* Decorates a map iterator such that it cannot be modified.
@@ -118,10 +115,6 @@
public Object getValue() {
return iterator.getValue();
- }
-
- public Entry asMapEntry() {
- return new UnmodifiableMapEntry(getKey(), getValue());
}
public Object setValue(Object value) {
1.2 +2 -18 jakarta-commons/collections/src/java/org/apache/commons/collections/iterators/MapIterator.java
Index: MapIterator.java
===================================================================
RCS file: /home/cvs/jakarta-commons/collections/src/java/org/apache/commons/collections/iterators/MapIterator.java,v
retrieving revision 1.1
retrieving revision 1.2
diff -u -r1.1 -r1.2
--- MapIterator.java 2 Nov 2003 15:27:54 -0000 1.1
+++ MapIterator.java 8 Nov 2003 18:43:13 -0000 1.2
@@ -58,7 +58,6 @@
package org.apache.commons.collections.iterators;
import java.util.Iterator;
-import java.util.Map;
/**
* Defines an iterator that operates over a <code>Map</code>.
@@ -123,21 +122,6 @@
*/
Object getValue();
- //-----------------------------------------------------------------------
- /**
- * Gets the last returned key-value pair from the underlying <code>Map</code>
- * as a Map Entry instance.
- * <p>
- * The returned entry must not change when <code>next</code> is called.
- * Changes made to the entry via <code>setValue</code> must change the map.
- *
- * @return the last return key-value pair as an independent Map Entry
- * @throws IllegalStateException if <code>next()</code> has not yet been called
- * @throws IllegalStateException if <code>remove()</code> has been called since the
- * last call to <code>next()</code>
- */
- Map.Entry asMapEntry();
-
//-----------------------------------------------------------------------
/**
* Removes the last returned key from the underlying <code>Map</code> (optional operation).
1.1 jakarta-commons/collections/src/java/org/apache/commons/collections/iterators/OrderedMapIterator.java
Index: OrderedMapIterator.java
===================================================================
/*
* $Header: /home/cvs/jakarta-commons/collections/src/java/org/apache/commons/collections/iterators/OrderedMapIterator.java,v 1.1 2003/11/08 18:43:12 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;
/**
* Defines an iterator that operates over an ordered <code>Map</code>.
* <p>
* This iterator allows both forward and reverse iteration through the map.
*
* @since Commons Collections 3.0
* @version $Revision: 1.1 $ $Date: 2003/11/08 18:43:12 $
*
* @author Stephen Colebourne
*/
public interface OrderedMapIterator extends MapIterator {
/**
* Checks to see if there is a previous entry that can be iterated to.
*
* @return <code>true</code> if the iterator has a previous element
*/
boolean hasPrevious();
/**
* Gets the previous <em>key</em> from the <code>Map</code>.
*
* @return the previous key in the iteration
* @throws NoSuchElementException if the iteration is finished
*/
Object previous();
}
1.1 jakarta-commons/collections/src/java/org/apache/commons/collections/iterators/EntrySetMapIterator.java
Index: EntrySetMapIterator.java
===================================================================
/*
* $Header: /home/cvs/jakarta-commons/collections/src/java/org/apache/commons/collections/iterators/EntrySetMapIterator.java,v 1.1 2003/11/08 18:43:13 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.Iterator;
import java.util.Map;
/**
* Implements a <code>MapIterator</code> using a Map entrySet.
* Reverse iteration is not supported.
* <pre>
* MapIterator it = map.mapIterator();
* while (it.hasNext()) {
* Object key = it.next();
* Object value = it.getValue();
* it.setValue(newValue);
* }
* </pre>
*
* @since Commons Collections 3.0
* @version $Revision: 1.1 $ $Date: 2003/11/08 18:43:13 $
*
* @author Stephen Colebourne
*/
public class EntrySetMapIterator implements MapIterator, ResetableMapIterator {
private final Map map;
private Iterator iterator;
private Map.Entry last;
private boolean canRemove = false;
/**
* Constructor.
*
* @param map the map to iterate over
*/
public EntrySetMapIterator(Map map) {
super();
this.map = map;
this.iterator = map.entrySet().iterator();
}
//-----------------------------------------------------------------------
/**
* Checks to see if there are more entries still to be iterated.
*
* @return <code>true</code> if the iterator has more elements
*/
public boolean hasNext() {
return iterator.hasNext();
}
/**
* Gets the next <em>key</em> from the <code>Map</code>.
*
* @return the next key in the iteration
* @throws NoSuchElementException if the iteration is finished
*/
public Object next() {
last = (Map.Entry) iterator.next();
canRemove = true;
return last.getKey();
}
//-----------------------------------------------------------------------
/**
* Removes the last returned key from the underlying <code>Map</code>.
* <p>
* This method can be called once per call to <code>next()</code>.
*
* @throws UnsupportedOperationException if remove is not supported by the map
* @throws IllegalStateException if <code>next()</code> has not yet been called
* @throws IllegalStateException if <code>remove()</code> has already been called
* since the last call to <code>next()</code>
*/
public void remove() {
if (canRemove == false) {
throw new IllegalStateException("Iterator remove() can only be called once after next()");
}
iterator.remove();
last = null;
canRemove = false;
}
//-----------------------------------------------------------------------
/**
* Gets the current key, which is the key returned by the last call
* to <code>next()</code>.
*
* @return the current key
* @throws IllegalStateException if <code>next()</code> has not yet been called
*/
public Object getKey() {
if (last == null) {
throw new IllegalStateException("Iterator getKey() can only be called after next() and before remove()");
}
return last.getKey();
}
/**
* Gets the current value, which is the value associated with the last key
* returned by <code>next()</code>.
*
* @return the current value
* @throws IllegalStateException if <code>next()</code> has not yet been called
*/
public Object getValue() {
if (last == null) {
throw new IllegalStateException("Iterator getValue() can only be called after next() and before remove()");
}
return last.getValue();
}
/**
* Sets the value associated with the current key.
*
* @param value the new value
* @return the previous value
* @throws UnsupportedOperationException if setValue is not supported by the map
* @throws IllegalStateException if <code>next()</code> has not yet been called
* @throws IllegalStateException if <code>remove()</code> has been called since the
* last call to <code>next()</code>
*/
public Object setValue(Object value) {
if (last == null) {
throw new IllegalStateException("Iterator setValue() can only be called after next() and before remove()");
}
return last.setValue(value);
}
//-----------------------------------------------------------------------
/**
* Resets the state of the iterator.
*/
public void reset() {
iterator = map.entrySet().iterator();
last = null;
canRemove = false;
}
/**
* Gets the iterator as a String.
*
* @return a string version of the iterator
*/
public String toString() {
if (last == null) {
return "MapIterator[" + getKey() + "=" + getValue() + "]";
} else {
return "MapIterator[]";
}
}
}
1.1 jakarta-commons/collections/src/java/org/apache/commons/collections/iterators/ResetableOrderedMapIterator.java
Index: ResetableOrderedMapIterator.java
===================================================================
/*
* $Header: /home/cvs/jakarta-commons/collections/src/java/org/apache/commons/collections/iterators/ResetableOrderedMapIterator.java,v 1.1 2003/11/08 18:43:13 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;
/**
* Interface implemented by those map iterators that can be reset back
* to an initial state.
*
* @since Commons Collections 3.0
* @version $Revision: 1.1 $ $Date: 2003/11/08 18:43:13 $
*
* @author Stephen Colebourne
*/
public interface ResetableOrderedMapIterator extends OrderedMapIterator, ResetableMapIterator {
/**
* Resets the iterator back to the position at which the iterator
* was created.
*/
public void reset();
}
---------------------------------------------------------------------
To unsubscribe, e-mail: commons-dev-unsubscribe@jakarta.apache.org
For additional commands, e-mail: commons-dev-help@jakarta.apache.org