IOBiConsumer.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.BiConsumer;

  22. /**
  23.  * Like {@link BiConsumer} but throws {@link IOException}.
  24.  *
  25.  * @param <T> the type of the first argument to the operation
  26.  * @param <U> the type of the second argument to the operation
  27.  * @see BiConsumer
  28.  * @since 2.12.0
  29.  */
  30. @FunctionalInterface
  31. public interface IOBiConsumer<T, U> {

  32.     /**
  33.      * Returns the no-op singleton.
  34.      *
  35.      * @param <T> the type of the first argument to the operation
  36.      * @param <U> the type of the second argument to the operation
  37.      * @return The no-op singleton.
  38.      */
  39.     @SuppressWarnings("unchecked")
  40.     static <T, U> IOBiConsumer<T, U> noop() {
  41.         return Constants.IO_BI_CONSUMER;
  42.     }

  43.     /**
  44.      * Performs this operation on the given arguments.
  45.      *
  46.      * @param t the first input argument
  47.      * @param u the second input argument
  48.      * @throws IOException if an I/O error occurs.
  49.      */
  50.     void accept(T t, U u) throws IOException;

  51.     /**
  52.      * Creates a composed {@link IOBiConsumer} that performs, in sequence, this operation followed by the {@code after}
  53.      * operation. If performing either operation throws an exception, it is relayed to the caller of the composed operation.
  54.      * If performing this operation throws an exception, the {@code after} operation will not be performed.
  55.      *
  56.      * @param after the operation to perform after this operation
  57.      * @return a composed {@link IOBiConsumer} that performs in sequence this operation followed by the {@code after}
  58.      *         operation
  59.      * @throws NullPointerException if {@code after} is null
  60.      */
  61.     default IOBiConsumer<T, U> andThen(final IOBiConsumer<? super T, ? super U> after) {
  62.         Objects.requireNonNull(after);
  63.         return (t, u) -> {
  64.             accept(t, u);
  65.             after.accept(t, u);
  66.         };
  67.     }

  68.     /**
  69.      * Creates a {@link BiConsumer} for this instance that throws {@link UncheckedIOException} instead of
  70.      * {@link IOException}.
  71.      *
  72.      * @return an UncheckedIOException BiConsumer.
  73.      * @throws UncheckedIOException Wraps an {@link IOException}.
  74.      * @since 2.12.0
  75.      */
  76.     default BiConsumer<T, U> asBiConsumer() {
  77.         return (t, u) -> Uncheck.accept(this, t, u);
  78.     }

  79. }