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