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 {@link Throwable}.
025 *
026 * @param <E> The kind of thrown exception or error.
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> The kind of thrown exception or error.
040     * @return The NOP singleton.
041     */
042    @SuppressWarnings("unchecked")
043    static <E extends Throwable> FailableDoubleConsumer<E> nop() {
044        return NOP;
045    }
046
047    /**
048     * Accepts the given arguments.
049     *
050     * @param value the parameter for the consumable to accept
051     * @throws E Thrown when the consumer fails.
052     */
053    void accept(double value) throws E;
054
055    /**
056     * Returns a composed {@link FailableDoubleConsumer} like {@link DoubleConsumer#andThen(DoubleConsumer)}.
057     *
058     * @param after the operation to perform after this one.
059     * @return a composed {@link FailableDoubleConsumer} like {@link DoubleConsumer#andThen(DoubleConsumer)}.
060     * @throws NullPointerException when {@code after} is null.
061     */
062    default FailableDoubleConsumer<E> andThen(final FailableDoubleConsumer<E> after) {
063        Objects.requireNonNull(after);
064        return (final double t) -> {
065            accept(t);
066            after.accept(t);
067        };
068    }
069}