IOConsumer.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.Objects;
  21. import java.util.function.Consumer;
  22. import java.util.stream.Stream;

  23. import org.apache.commons.io.IOExceptionList;
  24. import org.apache.commons.io.IOIndexedException;

  25. /**
  26.  * Like {@link Consumer} but throws {@link IOException}.
  27.  *
  28.  * @param <T> the type of the input to the operations.
  29.  * @since 2.7
  30.  */
  31. @FunctionalInterface
  32. public interface IOConsumer<T> {

  33.     /**
  34.      * Consider private.
  35.      */
  36.     IOConsumer<?> NOOP_IO_CONSUMER = t -> {
  37.         // noop
  38.     };

  39.     /**
  40.      * Performs an action for each element of the collection gathering any exceptions.
  41.      *
  42.      * @param action The action to apply to each input element.
  43.      * @param iterable The input to stream.
  44.      * @param <T> The element type.
  45.      * @throws IOExceptionList if any I/O errors occur.
  46.      * @since 2.12.0
  47.      */
  48.     static <T> void forAll(final IOConsumer<T> action, final Iterable<T> iterable) throws IOExceptionList {
  49.         IOStreams.forAll(IOStreams.of(iterable), action);
  50.     }

  51.     /**
  52.      * Performs an action for each element of the collection gathering any exceptions.
  53.      *
  54.      * @param action The action to apply to each input element.
  55.      * @param stream The input to stream.
  56.      * @param <T> The element type.
  57.      * @throws IOExceptionList if any I/O errors occur.
  58.      * @since 2.12.0
  59.      */
  60.     static <T> void forAll(final IOConsumer<T> action, final Stream<T> stream) throws IOExceptionList {
  61.         IOStreams.forAll(stream, action, IOIndexedException::new);
  62.     }

  63.     /**
  64.      * Performs an action for each element of the array, gathering any exceptions.
  65.      *
  66.      * @param action The action to apply to each input element.
  67.      * @param array The input to stream.
  68.      * @param <T> The element type.
  69.      * @throws IOExceptionList if any I/O errors occur.
  70.      * @since 2.12.0
  71.      */
  72.     @SafeVarargs
  73.     static <T> void forAll(final IOConsumer<T> action, final T... array) throws IOExceptionList {
  74.         IOStreams.forAll(IOStreams.of(array), action);
  75.     }

  76.     /**
  77.      * Performs an action for each element of the collection, stopping at the first exception.
  78.      *
  79.      * @param <T> The element type.
  80.      * @param iterable The input to stream.
  81.      * @param action The action to apply to each input element.
  82.      * @throws IOException if an I/O error occurs.
  83.      * @since 2.12.0
  84.      */
  85.     static <T> void forEach(final Iterable<T> iterable, final IOConsumer<T> action) throws IOException {
  86.         IOStreams.forEach(IOStreams.of(iterable), action);
  87.     }

  88.     /**
  89.      * Performs an action for each element of the stream, stopping at the first exception.
  90.      *
  91.      * @param <T> The element type.
  92.      * @param stream The input to stream.
  93.      * @param action The action to apply to each input element.
  94.      * @throws IOException if an I/O error occurs.
  95.      * @since 2.12.0
  96.      */
  97.     static <T> void forEach(final Stream<T> stream, final IOConsumer<T> action) throws IOException {
  98.         IOStreams.forEach(stream, action);
  99.     }

  100.     /**
  101.      * Performs an action for each element of this array, stopping at the first exception.
  102.      *
  103.      * @param <T> The element type.
  104.      * @param array The input to stream.
  105.      * @param action The action to apply to each input element.
  106.      * @throws IOException if an I/O error occurs.
  107.      * @since 2.12.0
  108.      */
  109.     static <T> void forEach(final T[] array, final IOConsumer<T> action) throws IOException {
  110.         IOStreams.forEach(IOStreams.of(array), action);
  111.     }

  112.     /**
  113.      * Returns the constant no-op consumer.
  114.      *
  115.      * @param <T> Type consumer type.
  116.      * @return a constant no-op consumer.
  117.      * @since 2.9.0
  118.      */
  119.     @SuppressWarnings("unchecked")
  120.     static <T> IOConsumer<T> noop() {
  121.         return (IOConsumer<T>) NOOP_IO_CONSUMER;
  122.     }

  123.     /**
  124.      * Performs this operation on the given argument.
  125.      *
  126.      * @param t the input argument
  127.      * @throws IOException if an I/O error occurs.
  128.      */
  129.     void accept(T t) throws IOException;

  130.     /**
  131.      * Returns a composed {@link IOConsumer} that performs, in sequence, this operation followed by the {@code after}
  132.      * operation. If performing either operation throws an exception, it is relayed to the caller of the composed operation.
  133.      * If performing this operation throws an exception, the {@code after} operation will not be performed.
  134.      *
  135.      * @param after the operation to perform after this operation
  136.      * @return a composed {@link Consumer} that performs in sequence this operation followed by the {@code after} operation
  137.      * @throws NullPointerException if {@code after} is null
  138.      */
  139.     default IOConsumer<T> andThen(final IOConsumer<? super T> after) {
  140.         Objects.requireNonNull(after, "after");
  141.         return (final T t) -> {
  142.             accept(t);
  143.             after.accept(t);
  144.         };
  145.     }

  146.     /**
  147.      * Creates a {@link Consumer} for this instance that throws {@link UncheckedIOException} instead of {@link IOException}.
  148.      *
  149.      * @return an UncheckedIOException Consumer.
  150.      * @since 2.12.0
  151.      */
  152.     default Consumer<T> asConsumer() {
  153.         return t -> Uncheck.accept(this, t);
  154.     }

  155. }