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