MCPcopy Create free account
hub / github.com/antlr/codebuff / cycle

Method cycle

corpus/java/training/guava/collect/Iterators.java:400–435  ·  view source on GitHub ↗

Returns an iterator that cycles indefinitely over the elements of iterable. The returned iterator supports remove() if the provided iterator does. After remove() is called, subsequent cycles omit the removed element, which is no longer in iterable. The iterator's

(final Iterable<T> iterable)

Source from the content-addressed store, hash-verified

398 * you will eventually remove all the elements.
399 */
400 public static <T> Iterator<T> cycle(final Iterable<T> iterable) {
401 checkNotNull(iterable);
402 return new Iterator<T>() {
403 Iterator<T> iterator = emptyModifiableIterator();
404
405 @Override
406 public boolean hasNext() {
407 /*
408 * Don't store a new Iterator until we know the user can't remove() the last returned
409 * element anymore. Otherwise, when we remove from the old iterator, we may be invalidating
410 * the new one. The result is a ConcurrentModificationException or other bad behavior.
411 *
412 * (If we decide that we really, really hate allocating two Iterators per cycle instead of
413 * one, we can optimistically store the new Iterator and then be willing to throw it out if
414 * the user calls remove().)
415 */
416 return iterator.hasNext() || iterable.iterator().hasNext();
417 }
418
419 @Override
420 public T next() {
421 if (!iterator.hasNext()) {
422 iterator = iterable.iterator();
423 if (!iterator.hasNext()) {
424 throw new NoSuchElementException();
425 }
426 }
427 return iterator.next();
428 }
429
430 @Override
431 public void remove() {
432 iterator.remove();
433 }
434 };
435 }
436
437 /**
438 * Returns an iterator that cycles indefinitely over the provided elements.

Callers 1

iteratorMethod · 0.95

Calls 3

newArrayListMethod · 0.95
checkNotNullMethod · 0.45

Tested by

no test coverage detected