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