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)
| 931 | |
| 932 | |
| 933 | public static <T> Iterator<T> limit(final Iterator<T> iterator, final int limitSize) { |
| 934 | checkNotNull(iterator); |
| 935 | checkArgument(limitSize >= 0, "limit is negative"); |
| 936 | return new Iterator<T>() { |
| 937 | private int count; |
| 938 | |
| 939 | @Override |
| 940 | public boolean hasNext() { |
| 941 | return count < limitSize && iterator.hasNext(); |
| 942 | } |
| 943 | |
| 944 | @Override |
| 945 | public T next() { |
| 946 | if (!hasNext()) { |
| 947 | throw new NoSuchElementException(); |
| 948 | } |
| 949 | |
| 950 | count++; |
| 951 | return iterator.next(); |
| 952 | } |
| 953 | |
| 954 | @Override |
| 955 | public void remove() { |
| 956 | iterator.remove(); |
| 957 | } |
| 958 | }; |
| 959 | } |
| 960 | |
| 961 | /** |
| 962 | * Returns a view of the supplied {@code iterator} that removes each element |
no test coverage detected