Refactor to avoid unecessary instanceof call

git-svn-id: https://svn.apache.org/repos/asf/jakarta/commons/proper/collections/trunk@131693 13f79535-47bb-0310-9956-ffa450edef68
This commit is contained in:
Stephen Colebourne 2004-05-03 11:50:30 +00:00
parent 4eee185efd
commit 9a462e3990
1 changed files with 37 additions and 28 deletions

View File

@ -70,7 +70,7 @@ import org.apache.commons.collections.Transformer;
* more efficient (and convenient) than using nested for loops to extract a list. * more efficient (and convenient) than using nested for loops to extract a list.
* *
* @since Commons Collections 3.1 * @since Commons Collections 3.1
* @version $Revision: 1.2 $ $Date: 2004/05/03 11:38:49 $ * @version $Revision: 1.3 $ $Date: 2004/05/03 11:50:30 $
* *
* @author Stephen Colebourne * @author Stephen Colebourne
*/ */
@ -148,23 +148,38 @@ public class ObjectGraphIterator implements Iterator {
root = null; root = null;
} }
} else { } else {
findNext(currentIterator); findNextByIterator(currentIterator);
} }
} }
/** /**
* Finds the next object in the iteration. * Finds the next object in the iteration given any start object.
* *
* @param value the value to start from * @param value the value to start from
*/ */
protected void findNext(Object value) { protected void findNext(Object value) {
if (value instanceof Iterator) { if (value instanceof Iterator) {
if (value != currentIterator) { // need to examine this iterator
findNextByIterator((Iterator) value);
} else {
// next value found
currentValue = value;
hasNext = true;
}
}
/**
* Finds the next object in the iteration given an iterator.
*
* @param iterator the iterator to start from
*/
protected void findNextByIterator(Iterator iterator) {
if (iterator != currentIterator) {
// recurse a level // recurse a level
if (currentIterator != null) { if (currentIterator != null) {
stack.push(currentIterator); stack.push(currentIterator);
} }
currentIterator = (Iterator) value; currentIterator = iterator;
} }
while (currentIterator.hasNext() && hasNext == false) { while (currentIterator.hasNext() && hasNext == false) {
@ -181,16 +196,10 @@ public class ObjectGraphIterator implements Iterator {
} else { } else {
// current iterator exhausted, go up a level // current iterator exhausted, go up a level
currentIterator = (Iterator) stack.pop(); currentIterator = (Iterator) stack.pop();
findNext(currentIterator); findNextByIterator(currentIterator);
}
} else {
// next value found
currentValue = value;
hasNext = true;
} }
} }
//----------------------------------------------------------------------- //-----------------------------------------------------------------------
/** /**
* Checks whether there are any more elements in the iteration to obtain. * Checks whether there are any more elements in the iteration to obtain.