Refactor ProxyIterators to AbstractDecorators

git-svn-id: https://svn.apache.org/repos/asf/jakarta/commons/proper/collections/trunk@131313 13f79535-47bb-0310-9956-ffa450edef68
This commit is contained in:
Stephen Colebourne 2003-11-02 16:29:12 +00:00
parent d3248e3613
commit 4b084fad6e
8 changed files with 599 additions and 145 deletions

View File

@ -0,0 +1,115 @@
/*
* $Header: /home/jerenkrantz/tmp/commons/commons-convert/cvs/home/cvs/jakarta-commons//collections/src/java/org/apache/commons/collections/iterators/AbstractIteratorDecorator.java,v 1.1 2003/11/02 16:29:12 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;
/**
* Provides basic behaviour for decorating an iterator with extra functionality.
* <p>
* All methods are forwarded to the decorated iterator.
*
* @since Commons Collections 3.0
* @version $Revision: 1.1 $ $Date: 2003/11/02 16:29:12 $
*
* @author James Strachan
* @author Stephen Colebourne
*/
public class AbstractIteratorDecorator implements Iterator {
/** The iterator being decorated */
private Iterator iterator;
//-----------------------------------------------------------------------
/**
* Constructor that decorates the specified iterator.
*
* @param iterator the iterator to decorate, must not be null
* @throws IllegalArgumentException if the collection is null
*/
public AbstractIteratorDecorator(Iterator iterator) {
super();
if (iterator == null) {
throw new IllegalArgumentException("Iterator must not be null");
}
this.iterator = iterator;
}
/**
* Gets the iterator being decorated.
*
* @return the decorated iterator
*/
protected Iterator getIterator() {
return iterator;
}
//-----------------------------------------------------------------------
public boolean hasNext() {
return iterator.hasNext();
}
public Object next() {
return iterator.next();
}
public void remove() {
iterator.remove();
}
}

View File

@ -0,0 +1,138 @@
/*
* $Header: /home/jerenkrantz/tmp/commons/commons-convert/cvs/home/cvs/jakarta-commons//collections/src/java/org/apache/commons/collections/iterators/AbstractListIteratorDecorator.java,v 1.1 2003/11/02 16:29:12 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;
/**
* Provides basic behaviour for decorating a list iterator with extra functionality.
* <p>
* All methods are forwarded to the decorated list iterator.
*
* @since Commons Collections 3.0
* @version $Revision: 1.1 $ $Date: 2003/11/02 16:29:12 $
*
* @author Rodney Waldhoff
* @author Stephen Colebourne
*/
public class AbstractListIteratorDecorator implements ListIterator {
/** The iterator being decorated */
private ListIterator iterator;
//-----------------------------------------------------------------------
/**
* Constructor that decorates the specified iterator.
*
* @param iterator the iterator to decorate, must not be null
* @throws IllegalArgumentException if the collection is null
*/
public AbstractListIteratorDecorator(ListIterator iterator) {
super();
if (iterator == null) {
throw new IllegalArgumentException("ListIterator must not be null");
}
this.iterator = iterator;
}
/**
* Gets the iterator being decorated.
*
* @return the decorated iterator
*/
protected ListIterator getListIterator() {
return 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() {
iterator.remove();
}
public void set(Object obj) {
iterator.set(obj);
}
public void add(Object obj) {
iterator.add(obj);
}
}

View File

@ -0,0 +1,130 @@
/*
* $Header: /home/jerenkrantz/tmp/commons/commons-convert/cvs/home/cvs/jakarta-commons//collections/src/java/org/apache/commons/collections/iterators/AbstractMapIteratorDecorator.java,v 1.1 2003/11/02 16:29:12 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.Map;
/**
* Provides basic behaviour for decorating a map iterator with extra functionality.
* <p>
* All methods are forwarded to the decorated map iterator.
*
* @since Commons Collections 3.0
* @version $Revision: 1.1 $ $Date: 2003/11/02 16:29:12 $
*
* @author Stephen Colebourne
*/
public class AbstractMapIteratorDecorator implements MapIterator {
/** The iterator being decorated */
private MapIterator iterator;
//-----------------------------------------------------------------------
/**
* Constructor that decorates the specified iterator.
*
* @param iterator the iterator to decorate, must not be null
* @throws IllegalArgumentException if the collection is null
*/
public AbstractMapIteratorDecorator(MapIterator iterator) {
super();
if (iterator == null) {
throw new IllegalArgumentException("MapIterator must not be null");
}
this.iterator = iterator;
}
/**
* Gets the iterator being decorated.
*
* @return the decorated iterator
*/
protected MapIterator getMapIterator() {
return iterator;
}
//-----------------------------------------------------------------------
public boolean hasNext() {
return iterator.hasNext();
}
public Object next() {
return iterator.next();
}
public void remove() {
iterator.remove();
}
public Object getKey() {
return iterator.getKey();
}
public Object getValue() {
return iterator.getValue();
}
public Object setValue(Object obj) {
return iterator.setValue(obj);
}
public Map.Entry asMapEntry() {
return iterator.asMapEntry();
}
}

View File

@ -1,5 +1,5 @@
/* /*
* $Header: /home/jerenkrantz/tmp/commons/commons-convert/cvs/home/cvs/jakarta-commons//collections/src/java/org/apache/commons/collections/iterators/FilterIterator.java,v 1.5 2003/09/29 22:02:33 scolebourne Exp $ * $Header: /home/jerenkrantz/tmp/commons/commons-convert/cvs/home/cvs/jakarta-commons//collections/src/java/org/apache/commons/collections/iterators/FilterIterator.java,v 1.6 2003/11/02 16:29:12 scolebourne Exp $
* ==================================================================== * ====================================================================
* *
* The Apache Software License, Version 1.1 * The Apache Software License, Version 1.1
@ -59,33 +59,33 @@ package org.apache.commons.collections.iterators;
import java.util.Iterator; import java.util.Iterator;
import java.util.NoSuchElementException; import java.util.NoSuchElementException;
import org.apache.commons.collections.Predicate; import org.apache.commons.collections.Predicate;
/** /**
* A Proxy {@link Iterator Iterator} which takes a {@link Predicate Predicate} instance to filter * Decorates an iterator such that only elements matching a predicate filter
* out objects from an underlying {@link Iterator Iterator} instance. * are returned.
* Only objects for which the
* specified <code>Predicate</code> evaluates to <code>true</code> are
* returned.
* *
* @since Commons Collections 1.0 * @since Commons Collections 1.0
* @version $Revision: 1.5 $ $Date: 2003/09/29 22:02:33 $ * @version $Revision: 1.6 $ $Date: 2003/11/02 16:29:12 $
* *
* @author <a href="mailto:jstrachan@apache.org">James Strachan</a> * @author James Strachan
* @author Jan Sorensen * @author Jan Sorensen
* @author Ralph Wagner * @author Ralph Wagner
* @author Stephen Colebourne
*/ */
public class FilterIterator extends ProxyIterator { public class FilterIterator implements Iterator {
/** Holds value of property predicate. */ /** The iterator being used */
private Iterator iterator;
/** The predicate being used */
private Predicate predicate; private Predicate predicate;
/** The next object in the iteration */
private Object nextObject; private Object nextObject;
/** Whether the next object has been calculated yet */
private boolean nextObjectSet = false; private boolean nextObjectSet = false;
//-----------------------------------------------------------------------
//-------------------------------------------------------------------------
/** /**
* Constructs a new <code>FilterIterator</code> that will not function * Constructs a new <code>FilterIterator</code> that will not function
* until {@link #setIterator(Iterator) setIterator} is invoked. * until {@link #setIterator(Iterator) setIterator} is invoked.
@ -101,7 +101,8 @@ public class FilterIterator extends ProxyIterator {
* @param iterator the iterator to use * @param iterator the iterator to use
*/ */
public FilterIterator(Iterator iterator) { public FilterIterator(Iterator iterator) {
super(iterator); super();
this.iterator = iterator;
} }
/** /**
@ -112,13 +113,12 @@ public class FilterIterator extends ProxyIterator {
* @param predicate the predicate to use * @param predicate the predicate to use
*/ */
public FilterIterator(Iterator iterator, Predicate predicate) { public FilterIterator(Iterator iterator, Predicate predicate) {
super(iterator); super();
this.iterator = iterator;
this.predicate = predicate; this.predicate = predicate;
} }
// Iterator interface //-----------------------------------------------------------------------
//-------------------------------------------------------------------------
/** /**
* Returns true if the underlying iterator contains an object that * Returns true if the underlying iterator contains an object that
* matches the predicate. * matches the predicate.
@ -126,7 +126,7 @@ public class FilterIterator extends ProxyIterator {
* @return true if there is another object that matches the predicate * @return true if there is another object that matches the predicate
*/ */
public boolean hasNext() { public boolean hasNext() {
if ( nextObjectSet ) { if (nextObjectSet) {
return true; return true;
} else { } else {
return setNextObject(); return setNextObject();
@ -141,7 +141,7 @@ public class FilterIterator extends ProxyIterator {
* match the predicate * match the predicate
*/ */
public Object next() { public Object next() {
if ( !nextObjectSet ) { if (!nextObjectSet) {
if (!setNextObject()) { if (!setNextObject()) {
throw new NoSuchElementException(); throw new NoSuchElementException();
} }
@ -165,35 +165,57 @@ public class FilterIterator extends ProxyIterator {
if (nextObjectSet) { if (nextObjectSet) {
throw new IllegalStateException("remove() cannot be called"); throw new IllegalStateException("remove() cannot be called");
} }
getIterator().remove(); iterator.remove();
} }
//-----------------------------------------------------------------------
/**
* Gets the iterator this iterator is using.
*
* @return the iterator.
*/
public Iterator getIterator() {
return iterator;
}
// Properties /**
//------------------------------------------------------------------------- * Sets the iterator for this iterator to use.
/** Getter for property predicate. * If iteration has started, this effectively resets the iterator.
* @return Value of property predicate. *
* @param iterator the iterator to use
*/
public void setIterator(Iterator iterator) {
this.iterator = iterator;
}
//-----------------------------------------------------------------------
/**
* Gets the predicate this iterator is using.
*
* @return the predicate.
*/ */
public Predicate getPredicate() { public Predicate getPredicate() {
return predicate; return predicate;
} }
/** Setter for property predicate.
* @param predicate New value of property predicate. /**
* Sets the predicate this the iterator to use.
*
* @param predicate the transformer to use
*/ */
public void setPredicate(Predicate predicate) { public void setPredicate(Predicate predicate) {
this.predicate = predicate; this.predicate = predicate;
} }
//-----------------------------------------------------------------------
/** /**
* Set nextObject to the next object. If there are no more * Set nextObject to the next object. If there are no more
* objects then return false. Otherwise, return true. * objects then return false. Otherwise, return true.
*/ */
private boolean setNextObject() { private boolean setNextObject() {
Iterator iterator = getIterator(); while (iterator.hasNext()) {
Predicate predicate = getPredicate();
while ( iterator.hasNext() ) {
Object object = iterator.next(); Object object = iterator.next();
if ( predicate.evaluate( object ) ) { if (predicate.evaluate(object)) {
nextObject = object; nextObject = object;
nextObjectSet = true; nextObjectSet = true;
return true; return true;

View File

@ -1,5 +1,5 @@
/* /*
* $Header: /home/jerenkrantz/tmp/commons/commons-convert/cvs/home/cvs/jakarta-commons//collections/src/java/org/apache/commons/collections/iterators/FilterListIterator.java,v 1.4 2003/09/29 22:02:33 scolebourne Exp $ * $Header: /home/jerenkrantz/tmp/commons/commons-convert/cvs/home/cvs/jakarta-commons//collections/src/java/org/apache/commons/collections/iterators/FilterListIterator.java,v 1.5 2003/11/02 16:29:12 scolebourne Exp $
* ==================================================================== * ====================================================================
* *
* The Apache Software License, Version 1.1 * The Apache Software License, Version 1.1
@ -59,6 +59,7 @@ package org.apache.commons.collections.iterators;
import java.util.ListIterator; import java.util.ListIterator;
import java.util.NoSuchElementException; import java.util.NoSuchElementException;
import org.apache.commons.collections.Predicate; import org.apache.commons.collections.Predicate;
/** /**
@ -70,15 +71,48 @@ import org.apache.commons.collections.Predicate;
* returned by the iterator. * returned by the iterator.
* *
* @since Commons Collections 2.0 * @since Commons Collections 2.0
* @version $Revision: 1.4 $ $Date: 2003/09/29 22:02:33 $ * @version $Revision: 1.5 $ $Date: 2003/11/02 16:29:12 $
* *
* @author Rodney Waldhoff * @author Rodney Waldhoff
*/ */
public class FilterListIterator extends ProxyListIterator { public class FilterListIterator implements ListIterator {
// Constructors /** The iterator being used */
//------------------------------------------------------------------------- private ListIterator iterator;
/** The predicate being used */
private Predicate predicate;
/**
* The value of the next (matching) object, when
* {@link #nextObjectSet} is true.
*/
private Object nextObject;
/**
* Whether or not the {@link #nextObject} has been set
* (possibly to <code>null</code>).
*/
private boolean nextObjectSet = false;
/**
* The value of the previous (matching) object, when
* {@link #previousObjectSet} is true.
*/
private Object previousObject;
/**
* Whether or not the {@link #previousObject} has been set
* (possibly to <code>null</code>).
*/
private boolean previousObjectSet = false;
/**
* The index of the element that would be returned by {@link #next}.
*/
private int nextIndex = 0;
//-----------------------------------------------------------------------
/** /**
* Constructs a new <code>FilterListIterator</code> that will not * Constructs a new <code>FilterListIterator</code> that will not
* function until * function until
@ -96,7 +130,8 @@ public class FilterListIterator extends ProxyListIterator {
* @param iterator the iterator to use * @param iterator the iterator to use
*/ */
public FilterListIterator(ListIterator iterator ) { public FilterListIterator(ListIterator iterator ) {
super(iterator); super();
this.iterator = iterator;
} }
/** /**
@ -106,7 +141,8 @@ public class FilterListIterator extends ProxyListIterator {
* @param predicate the predicate to use * @param predicate the predicate to use
*/ */
public FilterListIterator(ListIterator iterator, Predicate predicate) { public FilterListIterator(ListIterator iterator, Predicate predicate) {
super(iterator); super();
this.iterator = iterator;
this.predicate = predicate; this.predicate = predicate;
} }
@ -123,9 +159,7 @@ public class FilterListIterator extends ProxyListIterator {
this.predicate = predicate; this.predicate = predicate;
} }
// ListIterator interface //-----------------------------------------------------------------------
//-------------------------------------------------------------------------
/** Not supported. */ /** Not supported. */
public void add(Object o) { public void add(Object o) {
throw new UnsupportedOperationException("FilterListIterator.add(Object) is not supported."); throw new UnsupportedOperationException("FilterListIterator.add(Object) is not supported.");
@ -189,36 +223,52 @@ public class FilterListIterator extends ProxyListIterator {
throw new UnsupportedOperationException("FilterListIterator.set(Object) is not supported."); throw new UnsupportedOperationException("FilterListIterator.set(Object) is not supported.");
} }
// Properties //-----------------------------------------------------------------------
//------------------------------------------------------------------------- /**
* Gets the iterator this iterator is using.
*
* @return the iterator.
*/
public ListIterator getListIterator() {
return iterator;
}
/** /**
* Getter for the predicate property. * Sets the iterator for this iterator to use.
* @return value of the predicate property. * If iteration has started, this effectively resets the iterator.
*
* @param iterator the iterator to use
*/
public void setListIterator(ListIterator iterator) {
this.iterator = iterator;
}
//-----------------------------------------------------------------------
/**
* Gets the predicate this iterator is using.
*
* @return the predicate.
*/ */
public Predicate getPredicate() { public Predicate getPredicate() {
return predicate; return predicate;
} }
/** /**
* Setter for the predicate property. * Sets the predicate this the iterator to use.
* @param predicate new value for the predicate property. *
* @param predicate the transformer to use
*/ */
public void setPredicate(Predicate predicate) { public void setPredicate(Predicate predicate) {
this.predicate = predicate; this.predicate = predicate;
} }
// Private Methods //-----------------------------------------------------------------------
//-------------------------------------------------------------------------
private void clearNextObject() { private void clearNextObject() {
nextObject = null; nextObject = null;
nextObjectSet = false; nextObjectSet = false;
} }
private boolean setNextObject() { private boolean setNextObject() {
ListIterator iterator = getListIterator();
Predicate predicate = getPredicate();
// if previousObjectSet, // if previousObjectSet,
// then we've walked back one step in the // then we've walked back one step in the
// underlying list (due to a hasPrevious() call) // underlying list (due to a hasPrevious() call)
@ -249,9 +299,6 @@ public class FilterListIterator extends ProxyListIterator {
} }
private boolean setPreviousObject() { private boolean setPreviousObject() {
ListIterator iterator = getListIterator();
Predicate predicate = getPredicate();
// if nextObjectSet, // if nextObjectSet,
// then we've walked back one step in the // then we've walked back one step in the
// underlying list (due to a hasNext() call) // underlying list (due to a hasNext() call)
@ -276,38 +323,4 @@ public class FilterListIterator extends ProxyListIterator {
return false; return false;
} }
// Attributes
//-------------------------------------------------------------------------
/** Holds value of property "predicate". */
private Predicate predicate;
/**
* The value of the next (matching) object, when
* {@link #nextObjectSet} is true.
*/
private Object nextObject;
/**
* Whether or not the {@link #nextObject} has been set
* (possibly to <code>null</code>).
*/
private boolean nextObjectSet = false;
/**
* The value of the previous (matching) object, when
* {@link #previousObjectSet} is true.
*/
private Object previousObject;
/**
* Whether or not the {@link #previousObject} has been set
* (possibly to <code>null</code>).
*/
private boolean previousObjectSet = false;
/**
* The index of the element that would be returned by {@link #next}.
*/
private int nextIndex = 0;
} }

View File

@ -1,5 +1,5 @@
/* /*
* $Header: /home/jerenkrantz/tmp/commons/commons-convert/cvs/home/cvs/jakarta-commons//collections/src/java/org/apache/commons/collections/iterators/ProxyIterator.java,v 1.5 2003/09/29 22:02:33 scolebourne Exp $ * $Header: /home/jerenkrantz/tmp/commons/commons-convert/cvs/home/cvs/jakarta-commons//collections/src/java/org/apache/commons/collections/iterators/ProxyIterator.java,v 1.6 2003/11/02 16:29:12 scolebourne Exp $
* ==================================================================== * ====================================================================
* *
* The Apache Software License, Version 1.1 * The Apache Software License, Version 1.1
@ -62,11 +62,11 @@ import java.util.Iterator;
/** /**
* A Proxy {@link Iterator Iterator} which delegates its methods to a proxy instance. * A Proxy {@link Iterator Iterator} which delegates its methods to a proxy instance.
* *
* @see ProxyListIterator * @deprecated Use AbstractIteratorDecorator
* @since Commons Collections 1.0 * @since Commons Collections 1.0
* @version $Revision: 1.5 $ $Date: 2003/09/29 22:02:33 $ * @version $Revision: 1.6 $ $Date: 2003/11/02 16:29:12 $
* *
* @author <a href="mailto:jstrachan@apache.org">James Strachan</a> * @author James Strachan
*/ */
public class ProxyIterator implements Iterator { public class ProxyIterator implements Iterator {

View File

@ -1,5 +1,5 @@
/* /*
* $Header: /home/jerenkrantz/tmp/commons/commons-convert/cvs/home/cvs/jakarta-commons//collections/src/java/org/apache/commons/collections/iterators/ProxyListIterator.java,v 1.5 2003/09/29 22:02:33 scolebourne Exp $ * $Header: /home/jerenkrantz/tmp/commons/commons-convert/cvs/home/cvs/jakarta-commons//collections/src/java/org/apache/commons/collections/iterators/ProxyListIterator.java,v 1.6 2003/11/02 16:29:12 scolebourne Exp $
* ==================================================================== * ====================================================================
* *
* The Apache Software License, Version 1.1 * The Apache Software License, Version 1.1
@ -63,9 +63,9 @@ import java.util.ListIterator;
* A proxy {@link ListIterator ListIterator} which delegates its * A proxy {@link ListIterator ListIterator} which delegates its
* methods to a proxy instance. * methods to a proxy instance.
* *
* @see ProxyIterator * @deprecated Use AbstractListIteratorDecorator
* @since Commons Collections 2.0 * @since Commons Collections 2.0
* @version $Revision: 1.5 $ $Date: 2003/09/29 22:02:33 $ * @version $Revision: 1.6 $ $Date: 2003/11/02 16:29:12 $
* *
* @author Rodney Waldhoff * @author Rodney Waldhoff
*/ */

View File

@ -1,5 +1,5 @@
/* /*
* $Header: /home/jerenkrantz/tmp/commons/commons-convert/cvs/home/cvs/jakarta-commons//collections/src/java/org/apache/commons/collections/iterators/TransformIterator.java,v 1.5 2003/09/29 22:02:33 scolebourne Exp $ * $Header: /home/jerenkrantz/tmp/commons/commons-convert/cvs/home/cvs/jakarta-commons//collections/src/java/org/apache/commons/collections/iterators/TransformIterator.java,v 1.6 2003/11/02 16:29:12 scolebourne Exp $
* ==================================================================== * ====================================================================
* *
* The Apache Software License, Version 1.1 * The Apache Software License, Version 1.1
@ -58,24 +58,26 @@
package org.apache.commons.collections.iterators; package org.apache.commons.collections.iterators;
import java.util.Iterator; import java.util.Iterator;
import org.apache.commons.collections.Transformer; import org.apache.commons.collections.Transformer;
/** /**
* A Proxy {@link Iterator Iterator} which uses a {@link Transformer Transformer} * Decorates an iterator such that each element returned is transformed.
* instance to transform the contents of the {@link Iterator Iterator} into
* some other form.
* *
* @since Commons Collections 1.0 * @since Commons Collections 1.0
* @version $Revision: 1.5 $ $Date: 2003/09/29 22:02:33 $ * @version $Revision: 1.6 $ $Date: 2003/11/02 16:29:12 $
* *
* @author <a href="mailto:jstrachan@apache.org">James Strachan</a> * @author James Strachan
* @author Stephen Colebourne
*/ */
public class TransformIterator extends ProxyIterator { public class TransformIterator implements Iterator {
/** Holds value of property transformer. */ /** The iterator being used */
private Iterator iterator;
/** The transformer being used */
private Transformer transformer; private Transformer transformer;
//-----------------------------------------------------------------------
/** /**
* Constructs a new <code>TransformIterator</code> that will not function * Constructs a new <code>TransformIterator</code> that will not function
* until the {@link #setIterator(Iterator) setIterator} method is * until the {@link #setIterator(Iterator) setIterator} method is
@ -92,7 +94,8 @@ public class TransformIterator extends ProxyIterator {
* @param iterator the iterator to use * @param iterator the iterator to use
*/ */
public TransformIterator(Iterator iterator) { public TransformIterator(Iterator iterator) {
super(iterator); super();
this.iterator = iterator;
} }
/** /**
@ -104,50 +107,83 @@ public class TransformIterator extends ProxyIterator {
* @param transformer the transformer to use * @param transformer the transformer to use
*/ */
public TransformIterator(Iterator iterator, Transformer transformer) { public TransformIterator(Iterator iterator, Transformer transformer) {
super(iterator); super();
this.iterator = iterator;
this.transformer = transformer; this.transformer = transformer;
} }
// Iterator interface //-----------------------------------------------------------------------
//------------------------------------------------------------------------- public boolean hasNext() {
public Object next() { return iterator.hasNext();
return transform( super.next() );
} }
// Properties
//-------------------------------------------------------------------------
/** /**
* Getter for property transformer. * Gets the next object from the iteration, transforming it using the
* current transformer. If the transformer is null, no transformation
* occurs and the object from the iterator is returned directly.
* *
* @return Value of property transformer. * @return the next object
* @throws NoSuchElementException if there are no more elements
*/
public Object next() {
return transform(iterator.next());
}
public void remove() {
iterator.remove();
}
//-----------------------------------------------------------------------
/**
* Gets the iterator this iterator is using.
*
* @return the iterator.
*/
public Iterator getIterator() {
return iterator;
}
/**
* Sets the iterator for this iterator to use.
* If iteration has started, this effectively resets the iterator.
*
* @param iterator the iterator to use
*/
public void setIterator(Iterator iterator) {
this.iterator = iterator;
}
//-----------------------------------------------------------------------
/**
* Gets the transformer this iterator is using.
*
* @return the transformer.
*/ */
public Transformer getTransformer() { public Transformer getTransformer() {
return transformer; return transformer;
} }
/** /**
* Setter for property transformer. * Sets the transformer this the iterator to use.
* A null transformer is a no-op transformer.
* *
* @param transformer New value of property transformer. * @param transformer the transformer to use
*/ */
public void setTransformer(Transformer transformer) { public void setTransformer(Transformer transformer) {
this.transformer = transformer; this.transformer = transformer;
} }
// Implementation methods //-----------------------------------------------------------------------
//-------------------------------------------------------------------------
/** /**
* Transforms the given object using the transformer. If the * Transforms the given object using the transformer.
* transformer is null, the original object is returned as-is. * If the transformer is null, the original object is returned as-is.
* *
* @param source the object to transform * @param source the object to transform
* @return the transformed object * @return the transformed object
*/ */
protected Object transform( Object source ) { protected Object transform(Object source) {
Transformer transformer = getTransformer(); if (transformer != null) {
if ( transformer != null ) { return transformer.transform(source);
return transformer.transform( source );
} }
return source; return source;
} }