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
18 package org.apache.commons.io.function;
19
20 import java.io.IOException;
21 import java.io.UncheckedIOException;
22 import java.util.Iterator;
23 import java.util.NoSuchElementException;
24 import java.util.Objects;
25 import java.util.function.Consumer;
26
27 /**
28 * Like {@link Iterator} but throws {@link IOException}.
29 *
30 * @param <E> the type of elements returned by this iterator.
31 * @since 2.12.0
32 */
33 public interface IOIterator<E> {
34
35 /**
36 * Adapts the given Iterable as an IOIterator.
37 *
38 * @param <E> the type of the stream elements.
39 * @param iterable The iterable to adapt
40 * @return A new IOIterator
41 * @since 2.17.0
42 */
43 static <E> IOIterator<E> adapt(final Iterable<E> iterable) {
44 return IOIteratorAdapter.adapt(iterable.iterator());
45 }
46
47 /**
48 * Adapts the given Iterator as an IOIterator.
49 *
50 * @param <E> the type of the stream elements.
51 * @param iterator The iterator to adapt
52 * @return A new IOIterator
53 */
54 static <E> IOIterator<E> adapt(final Iterator<E> iterator) {
55 return IOIteratorAdapter.adapt(iterator);
56 }
57
58 /**
59 * Creates an {@link Iterator} for this instance that throws {@link UncheckedIOException} instead of
60 * {@link IOException}.
61 *
62 * @return an {@link UncheckedIOException} {@link Iterator}.
63 */
64 default Iterator<E> asIterator() {
65 return new UncheckedIOIterator<>(this);
66 }
67
68 /**
69 * Like {@link Iterator#forEachRemaining(Consumer)}.
70 *
71 * @param action See delegate.
72 * @throws IOException if an I/O error occurs.
73 */
74 default void forEachRemaining(final IOConsumer<? super E> action) throws IOException {
75 Objects.requireNonNull(action);
76 while (hasNext()) {
77 action.accept(next());
78 }
79 }
80
81 /**
82 * Like {@link Iterator#hasNext()}.
83 *
84 * @return See delegate.
85 * @throws IOException if an I/O error occurs.
86 */
87 boolean hasNext() throws IOException;
88
89 /**
90 * Like {@link Iterator#next()}.
91 *
92 * @return See delegate.
93 * @throws IOException if an I/O error occurs.
94 * @throws NoSuchElementException if the iteration has no more elements
95 */
96 E next() throws IOException;
97
98 /**
99 * Like {@link Iterator#remove()}.
100 *
101 * @throws IOException if an I/O error occurs.
102 */
103 @SuppressWarnings("unused")
104 default void remove() throws IOException {
105 unwrap().remove();
106 }
107
108 /**
109 * Unwraps this instance and returns the underlying {@link Iterator}.
110 * <p>
111 * Implementations may not have anything to unwrap and that behavior is undefined for now.
112 * </p>
113 * @return the underlying Iterator.
114 */
115 Iterator<E> unwrap();
116
117 }