// returns either null or a non-exhausted iterator.
@Nullable
public Iterator<T> 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<T> current = getCurrent();
- return (current != null);
+ return getCurrent() != null;
}
public T next() {
* @return
*/
protected ChainedListBase<TPayload> add(TPayload another) {
- if (myPayload == null) myPayload = another;
+ if (myPayload == null) {
+ myPayload = another;
+ }
else {
ChainedListBase<TPayload> farthest = this;
while (farthest.myNext != null) farthest = farthest.myNext;
}
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;
- }
}
assertEquals(all.size(), count);
}
-
+ public void testToStringDoesntExhaustIterator() {
+ final ChainIterable<String> initial = new ChainIterable<String>();
+ initial.addItem("foo");
+ assertEquals("foo", initial.toString());;
+ assertEquals("foo", initial.toString());
+ }
}