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.camel.util;
018
019import java.io.Closeable;
020import java.io.IOException;
021import java.util.Iterator;
022import java.util.concurrent.atomic.AtomicBoolean;
023
024/**
025 * Skip based {@link Iterator} which skips the given {@link Iterator} a number of times.
026 */
027public final class SkipIterator implements Iterator<Object>, Closeable {
028
029    private final Iterator<?> it;
030    private final int skip;
031    private boolean closed;
032    private final AtomicBoolean hasSkip = new AtomicBoolean();
033
034    /**
035     * Creates a new skip iterator
036     *
037     * @param  it                       the iterator
038     * @param  skip                     number of times to skip
039     * @throws IllegalArgumentException is thrown if skip is not a positive number
040     */
041    public SkipIterator(Iterator<?> it, int skip) {
042        this.it = it;
043        this.skip = skip;
044        if (skip < 0) {
045            throw new IllegalArgumentException("Skip must not be a negative number, was: " + skip);
046        }
047    }
048
049    @Override
050    public void close() throws IOException {
051        try {
052            IOHelper.closeIterator(it);
053        } finally {
054            // we are now closed
055            closed = true;
056        }
057    }
058
059    @Override
060    public boolean hasNext() {
061        if (closed) {
062            return false;
063        }
064
065        if (hasSkip.compareAndSet(false, true)) {
066            doSkip();
067        }
068
069        boolean answer = it.hasNext();
070        if (!answer) {
071            // auto close
072            try {
073                close();
074            } catch (IOException e) {
075                // ignore
076            }
077        }
078        return answer;
079    }
080
081    @Override
082    public Object next() {
083        if (hasSkip.compareAndSet(false, true)) {
084            doSkip();
085        }
086
087        return it.next();
088    }
089
090    private void doSkip() {
091        for (int i = 0; i < skip; i++) {
092            if (it.hasNext()) {
093                // skip
094                it.next();
095            }
096        }
097    }
098
099    @Override
100    public void remove() {
101        it.remove();
102    }
103}