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 argument the consumer accepts. 032 * @param <E> The 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 * Applies the given {@link FailableConsumer} action to the object if the consumer is not {@code null}. Otherwise, does nothing. 044 * 045 * @param consumer The consumer to consume. 046 * @param object The object to be consumed. 047 * @param <T> the type of the argument the consumer accepts. 048 * @param <E> The thrown exception type. 049 * @throws E Thrown when the consumer fails. 050 * @since 3.21.0 051 */ 052 static <T, E extends Throwable> void accept(final FailableConsumer<T, E> consumer, final T object) throws E { 053 if (consumer != null) { 054 consumer.accept(object); 055 } 056 } 057 058 /** 059 * Gets the NOP singleton. 060 * 061 * @param <T> The type of the argument the consumer accepts. 062 * @param <E> The thrown exception type. 063 * @return The NOP singleton. 064 */ 065 @SuppressWarnings("unchecked") 066 static <T, E extends Throwable> FailableConsumer<T, E> nop() { 067 return NOP; 068 } 069 070 /** 071 * Accepts the given arguments. 072 * 073 * @param object The parameter for the consumable to accept 074 * @throws E Thrown when the consumer fails. 075 */ 076 void accept(T object) throws E; 077 078 /** 079 * Returns a composed {@link Consumer} like {@link Consumer#andThen(Consumer)}. 080 * 081 * @param after The operation to perform after this operation 082 * @return A composed {@link Consumer} like {@link Consumer#andThen(Consumer)}. 083 * @throws NullPointerException Thrown when {@code after} is null. 084 */ 085 default FailableConsumer<T, E> andThen(final FailableConsumer<? super T, E> after) { 086 Objects.requireNonNull(after, "after"); 087 return (final T t) -> { 088 accept(t); 089 after.accept(t); 090 }; 091 } 092}