    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 *      https://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.lang3.function;
    019
    020import java.util.Objects;
    021import java.util.function.BiConsumer;
    022
    023/**
    024 * A functional interface like {@link BiConsumer} that declares a {@link Throwable}.
    025 *
    026 * @param <T> Consumed type 1.
    027 * @param <U> Consumed type 2.
    028 * @param <E> The kind of thrown exception or error.
    029 * @since 3.11
    030 */
    031@FunctionalInterface
    032public interface FailableBiConsumer<T, U, E extends Throwable> {
    033
    034    /** NOP singleton */
    035    @SuppressWarnings("rawtypes")
    036    FailableBiConsumer NOP = (t, u) -> { /* NOP */ };
    037
    038    /**
    039     * Gets the NOP singleton.
    040     *
    041     * @param <T> Consumed type 1.
    042     * @param <U> Consumed type 2.
    043     * @param <E> The kind of thrown exception or error.
    044     * @return The NOP singleton.
    045     */
    046    @SuppressWarnings("unchecked")
    047    static <T, U, E extends Throwable> FailableBiConsumer<T, U, E> nop() {
    048        return NOP;
    049    }
    050
    051    /**
    052     * Accepts the given arguments.
    053     *
    054     * @param t the first parameter for the consumable to accept
    055     * @param u the second parameter for the consumable to accept
    056     * @throws E Thrown when the consumer fails.
    057     */
    058    void accept(T t, U u) throws E;
    059
    060    /**
    061     * Returns a composed {@link FailableBiConsumer} like {@link BiConsumer#andThen(BiConsumer)}.
    062     *
    063     * @param after the operation to perform after this one.
    064     * @return a composed {@link FailableBiConsumer} like {@link BiConsumer#andThen(BiConsumer)}.
    065     * @throws NullPointerException when {@code after} is null.
    066     */
    067    default FailableBiConsumer<T, U, E> andThen(final FailableBiConsumer<? super T, ? super U, E> after) {
    068        Objects.requireNonNull(after);
    069        return (t, u) -> {
    070            accept(t, u);
    071            after.accept(t, u);
    072        };
    073    }
    074}