class CheckedIterator {

    @NonNull
    public CheckedIterator<E, T> limit(final int limitSize) throws IllegalArgumentException {
        checkArgument(limitSize >= 0, "limit is negative");
        final CheckedIterator<E, T> iterator = this;
        return new CheckedIterator<E, T>() {
            private int count;

            @Override
            public boolean hasNext() throws T {
                return count < limitSize && iterator.hasNext();
            }

            @Override
            public E next() throws T {
                if (!hasNext()) {
                    throw new NoSuchElementException();
                }
                count++;
                return iterator.next();
            }
        };
    }

}