com.hazelcast.function.ConsumerEx Maven / Gradle / Ivy
/*
* Copyright (c) 2008-2021, Hazelcast, Inc. All Rights Reserved.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package com.hazelcast.function;
import com.hazelcast.internal.util.ExceptionUtil;
import java.io.Serializable;
import java.util.function.Consumer;
import static com.hazelcast.internal.util.Preconditions.checkNotNull;
/**
* {@code Serializable} variant of {@link Consumer java.util.function.Consumer}
* which declares checked exception.
*
* @param the type of the input to the operation
*
* @since 4.0
*/
@FunctionalInterface
public interface ConsumerEx extends Consumer, Serializable {
/**
* Exception-declaring version of {@link Consumer#accept}
* @throws Exception in case of any exceptional case
*/
void acceptEx(T t) throws Exception;
@Override
default void accept(T t) {
try {
acceptEx(t);
} catch (Exception e) {
throw ExceptionUtil.sneakyThrow(e);
}
}
/**
* {@code Serializable} variant of {@link Consumer#andThen(Consumer)
* java.util.function.Consumer#andThen(Consumer)}.
*/
default ConsumerEx andThen(ConsumerEx after) {
checkNotNull(after, "after");
return t -> {
accept(t);
after.accept(t);
};
}
/**
* Returns a consumer that does nothing.
* @param the consumer input type
*/
static ConsumerEx noop() {
return x -> {
};
}
}