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.IntConsumer;
022
023/**
024 * A functional interface like {@link IntConsumer} but for {@code boolean}.
025 *
026 * @see IntConsumer
027 * @since 3.13.0
028 */
029@FunctionalInterface
030public interface BooleanConsumer {
031
032    /** NOP singleton */
033    BooleanConsumer NOP = t -> { /* NOP */ };
034
035    /**
036     * Returns The NOP singleton.
037     *
038     * @return The NOP singleton.
039     */
040    static BooleanConsumer nop() {
041        return NOP;
042    }
043
044    /**
045     * Accepts the given arguments.
046     *
047     * @param value the input argument
048     */
049    void accept(boolean value);
050
051    /**
052     * Returns a composed {@link BooleanConsumer} that performs, in sequence, this operation followed by the {@code after}
053     * operation. If performing either operation throws an exception, it is relayed to the caller of the composed operation.
054     * If performing this operation throws an exception, the {@code after} operation will not be performed.
055     *
056     * @param after the operation to perform after this operation
057     * @return a composed {@link BooleanConsumer} that performs in sequence this operation followed by the {@code after}
058     *         operation
059     * @throws NullPointerException if {@code after} is null
060     */
061    default BooleanConsumer andThen(final BooleanConsumer after) {
062        Objects.requireNonNull(after);
063        return (final boolean t) -> {
064            accept(t);
065            after.accept(t);
066        };
067    }
068}