All Downloads are FREE. Search and download functionalities are using the official Maven repository.

org.apache.jackrabbit.commons.iterator.LazyIteratorChain Maven / Gradle / Ivy

/*
 * Licensed to the Apache Software Foundation (ASF) under one or more
 * contributor license agreements. See the NOTICE file distributed with this
 * work for additional information regarding copyright ownership. The ASF
 * licenses this file to You under the Apache License, Version 2.0 (the
 * "License"); you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 * http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
 * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
 * License for the specific language governing permissions and limitations under
 * the License.
 */
package org.apache.jackrabbit.commons.iterator;

import java.util.Arrays;
import java.util.Iterator;
import java.util.NoSuchElementException;

/**
 * This class implements the concatenation of iterators. The implementation
 * is lazy in the sense that advancing of any iterator is deferred as much
 * as possible. Specifically no iterator is fully unwrapped at one single
 * point of time.
 *
 * @param   type of values iterating over
 */
public class LazyIteratorChain implements Iterator {
    private final Iterator> iterators;
    private Iterator currentIterator;
    private Boolean hasNext;

    /**
     * Returns the concatenation of all iterators in iterators.
     *
     * @param 
     * @param iterators
     * @return
     */
    public static  Iterator chain(Iterator> iterators) {
        return new LazyIteratorChain(iterators);
    }

    /**
     * Returns the concatenation of all iterators in iterators.
     *
     * @param 
     * @param iterators
     * @return
     */
    public static  Iterator chain(Iterator... iterators) {
        return new LazyIteratorChain(iterators);
    }

    public LazyIteratorChain(Iterator> iterators) {
        super();
        this.iterators = iterators;
    }

    public LazyIteratorChain(Iterator... iterators) {
        super();
        this.iterators = Arrays.asList(iterators).iterator();
    }

    public boolean hasNext() {
        // Memoizing the result of hasNext is crucial to performance when recursively
        // traversing tree structures.
        if (hasNext == null) {
            while ((currentIterator == null || !currentIterator.hasNext()) && iterators.hasNext()) {
                currentIterator = iterators.next();
            }
            hasNext = currentIterator != null && currentIterator.hasNext();
        }
        return hasNext;
    }

    public T next() {
        if (hasNext()) {
            hasNext = null;
            return currentIterator.next();
        }
        else {
            throw new NoSuchElementException();
        }
    }

    public void remove() {
        throw new UnsupportedOperationException();
    }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy