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.Scanner;
023import java.util.concurrent.atomic.AtomicBoolean;
024
025import org.apache.camel.CamelContext;
026import org.apache.camel.Exchange;
027
028/**
029 * Skip based {@link Iterator} which skips the given {@link Iterator} a number of times.
030 */
031public final class SkipIterator implements Iterator<Object>, Closeable {
032
033    private final CamelContext camelContext;
034    private final Exchange exchange;
035    private final Iterator<?> it;
036    private final int skip;
037    private boolean closed;
038    private final AtomicBoolean hasSkip = new AtomicBoolean();
039
040    /**
041     * Creates a new skip iterator
042     *
043     * @param exchange  the exchange used to create this group iterator
044     * @param it        the iterator
045     * @param skip      number of times to skip
046     * @throws IllegalArgumentException is thrown if skip is not a positive number
047     */
048    public SkipIterator(Exchange exchange, Iterator<?> it, int skip) {
049        this.exchange = exchange;
050        this.camelContext = exchange.getContext();
051        this.it = it;
052        this.skip = skip;
053        if (skip < 0) {
054            throw new IllegalArgumentException("Skip must not be a negative number, was: " + skip);
055        }
056    }
057
058    @Override
059    public void close() throws IOException {
060        try {
061            if (it instanceof Scanner) {
062                // special for Scanner which implement the Closeable since JDK7 
063                Scanner scanner = (Scanner) it;
064                scanner.close();
065                IOException ioException = scanner.ioException();
066                if (ioException != null) {
067                    throw ioException;
068                }
069            } else if (it instanceof Closeable) {
070                IOHelper.closeWithException((Closeable) it);
071            }
072        } finally {
073            // we are now closed
074            closed = true;
075        }
076    }
077
078    @Override
079    public boolean hasNext() {
080        if (closed) {
081            return false;
082        }
083
084        if (hasSkip.compareAndSet(false, true)) {
085            doSkip();
086        }
087
088        boolean answer = it.hasNext();
089        if (!answer) {
090            // auto close
091            try {
092                close();
093            } catch (IOException e) {
094                // ignore
095            }
096        }
097        return answer;
098    }
099
100    @Override
101    public Object next() {
102        if (hasSkip.compareAndSet(false, true)) {
103            doSkip();
104        }
105
106        return it.next();
107    }
108
109    private void doSkip() {
110        for (int i = 0; i < skip; i++) {
111            if (it.hasNext()) {
112                // skip
113                it.next();
114            }
115        }
116    }
117
118    @Override
119    public void remove() {
120        it.remove();
121    }
122}