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.DoubleConsumer;
022
023/**
024 * A functional interface like {@link DoubleConsumer} that declares a {@code Throwable}.
025 *
026 * @param <E> Thrown exception.
027 * @since 3.11
028 */
029@FunctionalInterface
030public interface FailableDoubleConsumer<E extends Throwable> {
031
032    /** NOP singleton */
033    @SuppressWarnings("rawtypes")
034    FailableDoubleConsumer NOP = t -> {/* NOP */};
035
036    /**
037     * Returns The NOP singleton.
038     *
039     * @param <E> Thrown exception.
040     * @return The NOP singleton.
041     */
042    static <E extends Throwable> FailableDoubleConsumer<E> nop() {
043        return NOP;
044    }
045
046    /**
047     * Accepts the consumer.
048     *
049     * @param value the parameter for the consumable to accept
050     * @throws E Thrown when the consumer fails.
051     */
052    void accept(double value) throws E;
053
054    /**
055     * Returns a composed {@code FailableDoubleConsumer} like {@link DoubleConsumer#andThen(DoubleConsumer)}.
056     *
057     * @param after the operation to perform after this one.
058     * @return a composed {@code FailableDoubleConsumer} like {@link DoubleConsumer#andThen(DoubleConsumer)}.
059     * @throws NullPointerException when {@code after} is null.
060     */
061    default FailableDoubleConsumer<E> andThen(final FailableDoubleConsumer<E> after) {
062        Objects.requireNonNull(after);
063        return (final double t) -> {
064            accept(t);
065            after.accept(t);
066        };
067    }
068}