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

Method limit

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

Returns a view containing the first limitSize elements of iterator. If iterator contains fewer than limitSize elements, the returned view contains all of its elements. The returned iterator supports remove() if iterator does. @param iterator the itera

(final Iterator<T> iterator, final int limitSize)

Source from the content-addressed store, hash-verified

880 * @since 3.0
881 */
882 public static <T> Iterator<T> limit(final Iterator<T> iterator, final int limitSize) {
883 checkNotNull(iterator);
884 checkArgument(limitSize >= 0, "limit is negative");
885 return new Iterator<T>() {
886 private int count;
887
888 @Override
889 public boolean hasNext() {
890 return count < limitSize && iterator.hasNext();
891 }
892
893 @Override
894 public T next() {
895 if (!hasNext()) {
896 throw new NoSuchElementException();
897 }
898 count++;
899 return iterator.next();
900 }
901
902 @Override
903 public void remove() {
904 iterator.remove();
905 }
906 };
907 }
908
909 /**
910 * Returns a view of the supplied {@code iterator} that removes each element

Callers 1

iteratorMethod · 0.95

Calls 2

checkNotNullMethod · 0.45
checkArgumentMethod · 0.45

Tested by

no test coverage detected