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.function;
019
020import java.io.IOException;
021import java.util.Objects;
022
023/**
024 * Like {@link Iterable} but throws {@link IOException}.
025 *
026 * @param <T> the type of elements returned by the iterable.
027 * @since 2.19.0
028 */
029public interface IOIterable<T> {
030
031    /**
032     * Like {@link Iterable#iterator()}.
033     *
034     * @param action The action to be performed for each element.
035     * @throws NullPointerException if the specified action is null.
036     * @throws IOException thrown by the given action.
037     * @see Iterable#iterator()
038     */
039    default void forEach(final IOConsumer<? super T> action) throws IOException {
040        iterator().forEachRemaining(Objects.requireNonNull(action));
041    }
042
043    /**
044     * Like {@link Iterable#iterator()}.
045     *
046     * @return See {@link Iterable#iterator() delegate}.
047     * @see Iterable#iterator()
048     */
049    IOIterator<T> iterator();
050
051    /**
052     * Like {@link Iterable#spliterator()}.
053     *
054     * @return See {@link Iterable#spliterator() delegate}.
055     * @see Iterable#spliterator()
056     */
057    default IOSpliterator<T> spliterator() {
058        return IOSpliteratorAdapter.adapt(new UncheckedIOIterable<>(this).spliterator());
059    }
060
061    /**
062     * Unwraps this instance and returns the underlying {@link Iterable}.
063     * <p>
064     * Implementations may not have anything to unwrap and that behavior is undefined for now.
065     * </p>
066     * @return the underlying Iterable.
067     */
068    Iterable<T> unwrap();
069
070}