001/*
002 * Licensed to the Apache Software Foundation (ASF) under one or more
003 * contributor license agreements.  See the NOTICE file distributed with
004 * this work for additional information regarding copyright ownership.
005 * The ASF licenses this file to You under the Apache License, Version 2.0
006 * (the "License"); you may not use this file except in compliance with
007 * the License.  You may obtain a copy of the License at
008 *
009 *      http://www.apache.org/licenses/LICENSE-2.0
010 *
011 * Unless required by applicable law or agreed to in writing, software
012 * distributed under the License is distributed on an "AS IS" BASIS,
013 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
014 * See the License for the specific language governing permissions and
015 * limitations under the License.
016 */
017
018package org.apache.commons.io.function;
019
020import java.io.IOException;
021import java.io.UncheckedIOException;
022import java.util.Objects;
023import java.util.function.BiConsumer;
024
025/**
026 * Like {@link BiConsumer} but throws {@link IOException}.
027 *
028 * @param <T> the type of the first argument to the operation
029 * @param <U> the type of the second argument to the operation
030 *
031 * @see BiConsumer
032 * @since 2.12.0
033 */
034@FunctionalInterface
035public interface IOBiConsumer<T, U> {
036
037    /**
038     * Returns the no-op singleton.
039     *
040     * @param <T> the type of the first argument to the operation
041     * @param <U> the type of the second argument to the operation
042     * @return The no-op singleton.
043     */
044    @SuppressWarnings("unchecked")
045    static <T, U> IOBiConsumer<T, U> noop() {
046        return Constants.IO_BI_CONSUMER;
047    }
048
049    /**
050     * Performs this operation on the given arguments.
051     *
052     * @param t the first input argument
053     * @param u the second input argument
054     * @throws IOException if an I/O error occurs.
055     */
056    void accept(T t, U u) throws IOException;
057
058    /**
059     * Creates a composed {@link IOBiConsumer} that performs, in sequence, this operation followed by the {@code after}
060     * operation. If performing either operation throws an exception, it is relayed to the caller of the composed operation.
061     * If performing this operation throws an exception, the {@code after} operation will not be performed.
062     *
063     * @param after the operation to perform after this operation
064     * @return a composed {@link IOBiConsumer} that performs in sequence this operation followed by the {@code after}
065     *         operation
066     * @throws NullPointerException if {@code after} is null
067     */
068    default IOBiConsumer<T, U> andThen(final IOBiConsumer<? super T, ? super U> after) {
069        Objects.requireNonNull(after);
070        return (t, u) -> {
071            accept(t, u);
072            after.accept(t, u);
073        };
074    }
075
076    /**
077     * Creates a {@link BiConsumer} for this instance that throws {@link UncheckedIOException} instead of
078     * {@link IOException}.
079     *
080     * @return an UncheckedIOException BiConsumer.
081     * @throws UncheckedIOException Wraps an {@link IOException}.
082     * @since 2.12.0
083     */
084    default BiConsumer<T, U> asBiConsumer() {
085        return (t, u) -> Uncheck.accept(this, t, u);
086    }
087
088}