diff --git a/python/src/com/jetbrains/python/toolbox/ChainIterationMixin.java b/python/src/com/jetbrains/python/toolbox/ChainIterationMixin.java index 75141d62db00..1387a40f04c8 100644 --- a/python/src/com/jetbrains/python/toolbox/ChainIterationMixin.java +++ b/python/src/com/jetbrains/python/toolbox/ChainIterationMixin.java @@ -40,22 +40,21 @@ abstract class ChainIterationMixin { // returns either null or a non-exhausted iterator. @Nullable public Iterator getCurrent() { - while ((myCurrent == null || !myCurrent.hasNext()) && myLink.hasPayload()) { // fix myCurrent + while ((myCurrent == null || !myCurrent.hasNext()) && (myLink != null && myLink.myPayload != null)) { // fix myCurrent if (myCurrent == null) { myCurrent = toIterator(myLink.myPayload); assert myCurrent != null; } else { - myLink.moveOn(); + myLink= myLink.myNext; myCurrent = null; } } return myCurrent; - } + } public boolean hasNext() { - Iterator current = getCurrent(); - return (current != null); + return getCurrent() != null; } public T next() { diff --git a/python/src/com/jetbrains/python/toolbox/ChainedListBase.java b/python/src/com/jetbrains/python/toolbox/ChainedListBase.java index 23997a2ad7c1..d1e5651ea9f3 100644 --- a/python/src/com/jetbrains/python/toolbox/ChainedListBase.java +++ b/python/src/com/jetbrains/python/toolbox/ChainedListBase.java @@ -43,7 +43,9 @@ public /*abstract */class ChainedListBase { * @return */ protected ChainedListBase add(TPayload another) { - if (myPayload == null) myPayload = another; + if (myPayload == null) { + myPayload = another; + } else { ChainedListBase farthest = this; while (farthest.myNext != null) farthest = farthest.myNext; @@ -51,17 +53,4 @@ public /*abstract */class ChainedListBase { } return this; } - - // become to our next - public void moveOn() { - if (myNext != null) { - myPayload = myNext.myPayload; - myNext = myNext.myNext; - } - else myPayload = null; // position 'after the end' - } - - public boolean hasPayload() { - return myPayload != null; - } } diff --git a/python/testSrc/com/jetbrains/python/toolbox/IteratorsTest.java b/python/testSrc/com/jetbrains/python/toolbox/IteratorsTest.java index 3db0e0316eab..918615eb38e7 100644 --- a/python/testSrc/com/jetbrains/python/toolbox/IteratorsTest.java +++ b/python/testSrc/com/jetbrains/python/toolbox/IteratorsTest.java @@ -190,5 +190,10 @@ public class IteratorsTest extends TestCase { assertEquals(all.size(), count); } - + public void testToStringDoesntExhaustIterator() { + final ChainIterable initial = new ChainIterable(); + initial.addItem("foo"); + assertEquals("foo", initial.toString());; + assertEquals("foo", initial.toString()); + } }