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 */
017package org.apache.commons.chain.web;
018
019import java.util.Objects;
020
021/**
022 * Represents an operation that accepts a single input argument and returns no
023 * result. Unlike most other functional interfaces, {@code Consumer} is expected
024 * to operate via side-effects. With this functional interface it is possible
025 * that an exception is thrown.
026 *
027 * <p>This is a <a href="package-summary.html">functional interface</a>
028 * whose functional method is {@link #accept(Object)}.
029 *
030 * @param <T> the type of the input to the operation
031 * @param <E> the type of the exception to the operation
032 *
033 * @since Chain 1.3
034 */
035@FunctionalInterface
036public interface CheckedConsumer<T, E extends Throwable> {
037
038    /**
039     * Performs this operation on the given argument.
040     *
041     * @param t the input argument
042     *
043     * @throws E the possible exception
044     */
045    void accept(T t) throws E;
046
047    /**
048     * Returns a composed {@code CheckedConsumer} that performs, in sequence, this
049     * operation followed by the {@code after} operation. If performing either
050     * operation throws an exception, it is relayed to the caller of the
051     * composed operation.  If performing this operation throws an exception,
052     * the {@code after} operation will not be performed.
053     *
054     * @param after the operation to perform after this operation
055     *
056     * @return a composed {@code Consumer} that performs in sequence this
057     * operation followed by the {@code after} operation
058     *
059     * @throws NullPointerException if {@code after} is null
060     * @throws E the possible exception
061     */
062    default CheckedConsumer<T, E> andThen(CheckedConsumer<? super T, E> after) throws E {
063        Objects.requireNonNull(after);
064        return (T t) -> {
065            accept(t);
066            after.accept(t);
067        };
068    }
069}