001/*
002 * Licensed to the Apache Software Foundation (ASF) under one or more
003 * contributor license agreements.  See the NOTICE file distributed with
004 * this work for additional information regarding copyright ownership.
005 * The ASF licenses this file to You under the Apache License, Version 2.0
006 * (the "License"); you may not use this file except in compliance with
007 * the License.  You may obtain a copy of the License at
008 *
009 *      http://www.apache.org/licenses/LICENSE-2.0
010 *
011 * Unless required by applicable law or agreed to in writing, software
012 * distributed under the License is distributed on an "AS IS" BASIS,
013 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
014 * See the License for the specific language governing permissions and
015 * limitations under the License.
016 */
017package org.apache.commons.collections4.iterators;
018
019import java.util.Iterator;
020
021/**
022 * Decorates another iterator to skip the first N elements.
023 * <p>
024 * In case an offset parameter other than 0 is provided, the decorated
025 * iterator is immediately advanced to this position, skipping all elements
026 * before that position.
027 * </p>
028 *
029 * @param <E> the type of elements returned by this iterator.
030 * @since 4.1
031 */
032public class SkippingIterator<E> extends AbstractIteratorDecorator<E> {
033
034    /** The offset to bound the first element return */
035    private final long offset;
036
037    /** The position of the current element */
038    private long pos;
039
040    /**
041     * Decorates the specified iterator to skip all elements until the iterator
042     * reaches the position at {@code offset}.
043     * <p>
044     * The iterator is immediately advanced until it reaches the position at {@code offset},
045     * incurring O(n) time.
046     *
047     * @param iterator  the iterator to be decorated
048     * @param offset  the index of the first element of the decorated iterator to return
049     * @throws NullPointerException if iterator is null
050     * @throws IllegalArgumentException if offset is negative
051     */
052    public SkippingIterator(final Iterator<E> iterator, final long offset) {
053        super(iterator);
054
055        if (offset < 0) {
056            throw new IllegalArgumentException("Offset parameter must not be negative.");
057        }
058
059        this.offset = offset;
060        this.pos = 0;
061        init();
062    }
063
064    /**
065     * Skips the given number of elements.
066     */
067    private void init() {
068        while (pos < offset && hasNext()) {
069            next();
070        }
071    }
072
073    @Override
074    public E next() {
075        final E next = super.next();
076        pos++;
077        return next;
078    }
079
080    /**
081     * {@inheritDoc}
082     * <p>
083     * In case an offset other than 0 was specified, the underlying iterator will be advanced
084     * to this position upon creation. A call to {@link #remove()} will still result in an
085     * {@link IllegalStateException} if no explicit call to {@link #next()} has been made prior
086     * to calling {@link #remove()}.
087     */
088    @Override
089    public void remove() {
090        if (pos <= offset) {
091            throw new IllegalStateException("remove() can not be called before calling next()");
092        }
093        super.remove();
094    }
095
096}