mirror of
https://github.com/apache/commons-collections.git
synced 2025-02-17 15:35:00 +00:00
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:
parent
4eee185efd
commit
9a462e3990
@ -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,41 +148,19 @@ 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
|
||||||
// recurse a level
|
findNextByIterator((Iterator) value);
|
||||||
if (currentIterator != null) {
|
|
||||||
stack.push(currentIterator);
|
|
||||||
}
|
|
||||||
currentIterator = (Iterator) value;
|
|
||||||
}
|
|
||||||
|
|
||||||
while (currentIterator.hasNext() && hasNext == false) {
|
|
||||||
Object next = currentIterator.next();
|
|
||||||
if (transformer != null) {
|
|
||||||
next = transformer.transform(next);
|
|
||||||
}
|
|
||||||
findNext(next);
|
|
||||||
}
|
|
||||||
if (hasNext) {
|
|
||||||
// next value found
|
|
||||||
} else if (stack.isEmpty()) {
|
|
||||||
// all iterators exhausted
|
|
||||||
} else {
|
|
||||||
// current iterator exhausted, go up a level
|
|
||||||
currentIterator = (Iterator) stack.pop();
|
|
||||||
findNext(currentIterator);
|
|
||||||
}
|
|
||||||
} else {
|
} else {
|
||||||
// next value found
|
// next value found
|
||||||
currentValue = value;
|
currentValue = value;
|
||||||
@ -190,6 +168,37 @@ public class ObjectGraphIterator implements Iterator {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 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
|
||||||
|
if (currentIterator != null) {
|
||||||
|
stack.push(currentIterator);
|
||||||
|
}
|
||||||
|
currentIterator = iterator;
|
||||||
|
}
|
||||||
|
|
||||||
|
while (currentIterator.hasNext() && hasNext == false) {
|
||||||
|
Object next = currentIterator.next();
|
||||||
|
if (transformer != null) {
|
||||||
|
next = transformer.transform(next);
|
||||||
|
}
|
||||||
|
findNext(next);
|
||||||
|
}
|
||||||
|
if (hasNext) {
|
||||||
|
// next value found
|
||||||
|
} else if (stack.isEmpty()) {
|
||||||
|
// all iterators exhausted
|
||||||
|
} else {
|
||||||
|
// current iterator exhausted, go up a level
|
||||||
|
currentIterator = (Iterator) stack.pop();
|
||||||
|
findNextByIterator(currentIterator);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
//-----------------------------------------------------------------------
|
//-----------------------------------------------------------------------
|
||||||
/**
|
/**
|
||||||
|
Loading…
x
Reference in New Issue
Block a user