    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 */
    017
    018package org.apache.commons.io;
    019
    020import java.util.Iterator;
    021import java.util.Objects;
    022import java.util.stream.Stream;
    023
    024/**
    025 * Wraps and presents a {@link Stream} as a {@link AutoCloseable} {@link Iterator} resource that automatically closes itself when reaching the end of stream.
    026 *
    027 * <h2>Warning</h2>
    028 * <p>
    029 * In order to close the stream, the call site MUST either close the stream it allocated OR call this iterator until the end.
    030 * </p>
    031 *
    032 * @param <E> The {@link Stream} and {@link Iterator} type.
    033 * @since 2.15.0
    034 */
    035public final class StreamIterator<E> implements Iterator<E>, AutoCloseable {
    036
    037    /**
    038     * Wraps and presents a stream as a closable resource that automatically closes itself when reaching the end of stream.
    039     * <p>
    040     * <b>Warning</b>
    041     * </p>
    042     * <p>
    043     * In order to close the stream, the call site MUST either close the stream it allocated OR call this iterator until the end.
    044     * </p>
    045     *
    046     * @param <T>    The stream and iterator type.
    047     * @param stream The stream iterate.
    048     * @return A new iterator.
    049     */
    050    public static <T> StreamIterator<T> iterator(final Stream<T> stream) {
    051        return new StreamIterator<>(stream);
    052    }
    053
    054    /**
    055     * The given stream's Iterator.
    056     */
    057    private final Iterator<E> iterator;
    058
    059    /**
    060     * The given stream.
    061     */
    062    private final Stream<E> stream;
    063
    064    /**
    065     * Whether {@link #close()} has been called.
    066     */
    067    private boolean closed;
    068
    069    private StreamIterator(final Stream<E> stream) {
    070        this.stream = Objects.requireNonNull(stream, "stream");
    071        this.iterator = stream.iterator();
    072    }
    073
    074    /**
    075     * Closes the underlying stream.
    076     */
    077    @Override
    078    public void close() {
    079        closed = true;
    080        stream.close();
    081    }
    082
    083    @Override
    084    public boolean hasNext() {
    085        if (closed) {
    086            // Calling Iterator#hasNext() on a closed java.nio.file.FileTreeIterator causes an IllegalStateException.
    087            return false;
    088        }
    089        final boolean hasNext = iterator.hasNext();
    090        if (!hasNext) {
    091            close();
    092        }
    093        return hasNext;
    094    }
    095
    096    @Override
    097    public E next() {
    098        final E next = iterator.next();
    099        if (next == null) {
    100            close();
    101        }
    102        return next;
    103    }
    104
    105}