scolebourne 2004/05/03 04:50:30
Modified: collections/src/java/org/apache/commons/collections/iterators
ObjectGraphIterator.java
Log:
Refactor to avoid unecessary instanceof call
Revision Changes Path
1.3 +37 -28
jakarta-commons/collections/src/java/org/apache/commons/collections/iterators/ObjectGraphIterator.java
Index: ObjectGraphIterator.java
===================================================================
RCS file:
/home/cvs/jakarta-commons/collections/src/java/org/apache/commons/collections/iterators/ObjectGraphIterator.java,v
retrieving revision 1.2
retrieving revision 1.3
diff -u -r1.2 -r1.3
--- ObjectGraphIterator.java 3 May 2004 11:38:49 -0000 1.2
+++ ObjectGraphIterator.java 3 May 2004 11:50:30 -0000 1.3
@@ -148,41 +148,19 @@
root = null;
}
} 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
*/
protected void findNext(Object value) {
if (value instanceof Iterator) {
- if (value != currentIterator) {
- // recurse a level
- 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);
- }
+ // need to examine this iterator
+ findNextByIterator((Iterator) value);
} else {
// next value found
currentValue = value;
@@ -190,7 +168,38 @@
}
}
-
+ /**
+ * 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);
+ }
+ }
+
//-----------------------------------------------------------------------
/**
* Checks whether there are any more elements in the iteration to obtain.
---------------------------------------------------------------------
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]