IOIterator.java

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

  17. package org.apache.commons.io.function;

  18. import java.io.IOException;
  19. import java.io.UncheckedIOException;
  20. import java.util.Iterator;
  21. import java.util.NoSuchElementException;
  22. import java.util.Objects;
  23. import java.util.function.Consumer;

  24. /**
  25.  * Like {@link Iterator} but throws {@link IOException}.
  26.  *
  27.  * @param <E> the type of elements returned by this iterator.
  28.  * @since 2.12.0
  29.  */
  30. public interface IOIterator<E> {

  31.     /**
  32.      * Adapts the given Iterable as an IOIterator.
  33.      *
  34.      * @param <E> the type of the stream elements.
  35.      * @param iterable The iterable to adapt
  36.      * @return A new IOIterator
  37.      * @since 2.17.0
  38.      */
  39.     static <E> IOIterator<E> adapt(final Iterable<E> iterable) {
  40.         return IOIteratorAdapter.adapt(iterable.iterator());
  41.     }

  42.     /**
  43.      * Adapts the given Iterator as an IOIterator.
  44.      *
  45.      * @param <E> the type of the stream elements.
  46.      * @param iterator The iterator to adapt
  47.      * @return A new IOIterator
  48.      */
  49.     static <E> IOIterator<E> adapt(final Iterator<E> iterator) {
  50.         return IOIteratorAdapter.adapt(iterator);
  51.     }

  52.     /**
  53.      * Creates an {@link Iterator} for this instance that throws {@link UncheckedIOException} instead of
  54.      * {@link IOException}.
  55.      *
  56.      * @return an {@link UncheckedIOException} {@link Iterator}.
  57.      */
  58.     default Iterator<E> asIterator() {
  59.         return new UncheckedIOIterator<>(this);
  60.     }

  61.     /**
  62.      * Like {@link Iterator#forEachRemaining(Consumer)}.
  63.      *
  64.      * @param action See delegate.
  65.      * @throws IOException if an I/O error occurs.
  66.      */
  67.     default void forEachRemaining(final IOConsumer<? super E> action) throws IOException {
  68.         Objects.requireNonNull(action);
  69.         while (hasNext()) {
  70.             action.accept(next());
  71.         }
  72.     }

  73.     /**
  74.      * Like {@link Iterator#hasNext()}.
  75.      *
  76.      * @return See delegate.
  77.      * @throws IOException if an I/O error occurs.
  78.      */
  79.     boolean hasNext() throws IOException;

  80.     /**
  81.      * Like {@link Iterator#next()}.
  82.      *
  83.      * @return See delegate.
  84.      * @throws IOException if an I/O error occurs.
  85.      * @throws NoSuchElementException if the iteration has no more elements
  86.      */
  87.     E next() throws IOException;

  88.     /**
  89.      * Like {@link Iterator#remove()}.
  90.      *
  91.      * @throws IOException if an I/O error occurs.
  92.      */
  93.     @SuppressWarnings("unused")
  94.     default void remove() throws IOException {
  95.         unwrap().remove();
  96.     }

  97.     /**
  98.      * Unwraps this instance and returns the underlying {@link Iterator}.
  99.      * <p>
  100.      * Implementations may not have anything to unwrap and that behavior is undefined for now.
  101.      * </p>
  102.      * @return the underlying Iterator.
  103.      */
  104.     Iterator<E> unwrap();

  105. }