View Javadoc
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.Objects;
23  import java.util.function.Consumer;
24  import java.util.stream.Stream;
25  
26  import org.apache.commons.io.IOExceptionList;
27  import org.apache.commons.io.IOIndexedException;
28  
29  /**
30   * Like {@link Consumer} but throws {@link IOException}.
31   *
32   * @param <T> the type of the input to the operations.
33   * @since 2.7
34   */
35  @FunctionalInterface
36  public interface IOConsumer<T> {
37  
38      /**
39       * Consider private.
40       */
41      IOConsumer<?> NOOP_IO_CONSUMER = t -> {/* noop */};
42  
43      /**
44       * Performs an action for each element of the collection gathering any exceptions.
45       *
46       * @param action The action to apply to each input element.
47       * @param iterable The input to stream.
48       * @param <T> The element type.
49       * @throws IOExceptionList if any I/O errors occur.
50       * @since 2.12.0
51       */
52      static <T> void forAll(final IOConsumer<T> action, final Iterable<T> iterable) throws IOExceptionList {
53          IOStreams.forAll(IOStreams.of(iterable), action);
54      }
55  
56      /**
57       * Performs an action for each element of the collection gathering any exceptions.
58       *
59       * @param action The action to apply to each input element.
60       * @param stream The input to stream.
61       * @param <T> The element type.
62       * @throws IOExceptionList if any I/O errors occur.
63       * @since 2.12.0
64       */
65      static <T> void forAll(final IOConsumer<T> action, final Stream<T> stream) throws IOExceptionList {
66          IOStreams.forAll(stream, action, IOIndexedException::new);
67      }
68  
69      /**
70       * Performs an action for each element of the array, gathering any exceptions.
71       *
72       * @param action The action to apply to each input element.
73       * @param array The input to stream.
74       * @param <T> The element type.
75       * @throws IOExceptionList if any I/O errors occur.
76       * @since 2.12.0
77       */
78      @SafeVarargs
79      static <T> void forAll(final IOConsumer<T> action, final T... array) throws IOExceptionList {
80          IOStreams.forAll(IOStreams.of(array), action);
81      }
82  
83      /**
84       * Performs an action for each element of the collection, stopping at the first exception.
85       *
86       * @param <T> The element type.
87       * @param iterable The input to stream.
88       * @param action The action to apply to each input element.
89       * @throws IOException if an I/O error occurs.
90       * @since 2.12.0
91       */
92      static <T> void forEach(final Iterable<T> iterable, final IOConsumer<T> action) throws IOException {
93          IOStreams.forEach(IOStreams.of(iterable), action);
94      }
95  
96      /**
97       * Performs an action for each element of the stream, stopping at the first exception.
98       *
99       * @param <T> The element type.
100      * @param stream The input to stream.
101      * @param action The action to apply to each input element.
102      * @throws IOException if an I/O error occurs.
103      * @since 2.12.0
104      */
105     static <T> void forEach(final Stream<T> stream, final IOConsumer<T> action) throws IOException {
106         IOStreams.forEach(stream, action);
107     }
108 
109     /**
110      * Performs an action for each element of this array, stopping at the first exception.
111      *
112      * @param <T> The element type.
113      * @param array The input to stream.
114      * @param action The action to apply to each input element.
115      * @throws IOException if an I/O error occurs.
116      * @since 2.12.0
117      */
118     static <T> void forEach(final T[] array, final IOConsumer<T> action) throws IOException {
119         IOStreams.forEach(IOStreams.of(array), action);
120     }
121 
122     /**
123      * Returns the constant no-op consumer.
124      *
125      * @param <T> Type consumer type.
126      * @return a constant no-op consumer.
127      * @since 2.9.0
128      */
129     @SuppressWarnings("unchecked")
130     static <T> IOConsumer<T> noop() {
131         return (IOConsumer<T>) NOOP_IO_CONSUMER;
132     }
133 
134     /**
135      * Performs this operation on the given argument.
136      *
137      * @param t the input argument
138      * @throws IOException if an I/O error occurs.
139      */
140     void accept(T t) throws IOException;
141 
142     /**
143      * Returns a composed {@link IOConsumer} that performs, in sequence, this operation followed by the {@code after}
144      * operation. If performing either operation throws an exception, it is relayed to the caller of the composed operation.
145      * If performing this operation throws an exception, the {@code after} operation will not be performed.
146      *
147      * @param after the operation to perform after this operation
148      * @return a composed {@link Consumer} that performs in sequence this operation followed by the {@code after} operation
149      * @throws NullPointerException if {@code after} is null
150      */
151     default IOConsumer<T> andThen(final IOConsumer<? super T> after) {
152         Objects.requireNonNull(after, "after");
153         return (final T t) -> {
154             accept(t);
155             after.accept(t);
156         };
157     }
158 
159     /**
160      * Creates a {@link Consumer} for this instance that throws {@link UncheckedIOException} instead of {@link IOException}.
161      *
162      * @return an UncheckedIOException Consumer.
163      * @since 2.12.0
164      */
165     default Consumer<T> asConsumer() {
166         return t -> Uncheck.accept(this, t);
167     }
168 
169 }