Many resources are needed to download a project. Please understand that we have to compensate our server costs. Thank you in advance. Project price only 1 $
You can buy this project and download/modify it how often you want.
/*
* JBoss, Home of Professional Open Source.
* Copyright 2017 Red Hat, Inc., and individual contributors
* as indicated by the @author tags.
*
* 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 org.wildfly.common.function;
import java.util.function.BiConsumer;
import java.util.function.BiFunction;
import java.util.function.Consumer;
import java.util.function.Function;
import java.util.function.Supplier;
import org.wildfly.common.Assert;
/**
* A set of utility methods which return common functions.
*/
public final class Functions {
private Functions() {}
/**
* Get the singleton consumer which accepts and runs runnable instances.
*
* @return the runnable consumer
*/
public static Consumer runnableConsumer() {
return RunnableConsumer.INSTANCE;
}
/**
* Get the singleton exception consumer which accepts and runs exception runnable instances.
*
* @param the exception type
* @return the runnable consumer
*/
@SuppressWarnings({"unchecked", "rawtypes"})
public static ExceptionConsumer, E> exceptionRunnableConsumer() {
return ExceptionRunnableConsumer.INSTANCE;
}
/**
* Get the singleton exception consumer which accepts and runs runnable instances.
*
* @return the runnable consumer
*/
public static ExceptionConsumer runnableExceptionConsumer() {
return RunnableExceptionConsumer.INSTANCE;
}
/**
* Get the singleton consumer which accepts a consumer and an argument to hand to it.
*
* @param the argument type
* @return the consumer
*/
@SuppressWarnings({"unchecked", "rawtypes"})
public static BiConsumer, T> consumerBiConsumer() {
return ConsumerBiConsumer.INSTANCE;
}
/**
* Get the singleton consumer which accepts a consumer and an argument to hand to it.
*
* @param the argument type
* @param the exception type
* @return the consumer
*/
@SuppressWarnings({"unchecked", "rawtypes"})
public static ExceptionBiConsumer, T, E> exceptionConsumerBiConsumer() {
return ExceptionConsumerBiConsumer.INSTANCE;
}
/**
* Get the singleton consumer which accepts a consumer and an argument to hand to it.
*
* @param the argument type
* @return the consumer
*/
@SuppressWarnings({"unchecked", "rawtypes"})
public static ExceptionBiConsumer, T, RuntimeException> consumerExceptionBiConsumer() {
return ConsumerExceptionBiConsumer.INSTANCE;
}
/**
* Get the singleton function which accepts a supplier and returns the result of the supplier.
*
* @param the result type
* @return the function
*/
@SuppressWarnings({"unchecked", "rawtypes"})
public static Function, R> supplierFunction() {
return SupplierFunction.INSTANCE;
}
/**
* Get the singleton function which accepts a supplier and returns the result of the supplier.
*
* @param the result type
* @param the exception type
* @return the function
*/
@SuppressWarnings({"unchecked", "rawtypes"})
public static ExceptionFunction, R, E> exceptionSupplierFunction() {
return ExceptionSupplierFunction.INSTANCE;
}
/**
* Get the singleton function which accepts a supplier and returns the result of the supplier.
*
* @param the result type
* @return the function
*/
@SuppressWarnings({"unchecked", "rawtypes"})
public static ExceptionFunction, R, RuntimeException> supplierExceptionFunction() {
return SupplierExceptionFunction.INSTANCE;
}
/**
* Get the singleton function which accepts a function which accepts a supplier, all of which return the result
* of the supplier.
*
* @param the result type
* @return the function
*/
@SuppressWarnings({"unchecked", "rawtypes"})
public static BiFunction, R>, Supplier, R> supplierFunctionBiFunction() {
return FunctionSupplierBiFunction.INSTANCE;
}
/**
* Get the singleton function which accepts a function which accepts a supplier, all of which return the result
* of the supplier.
*
* @param the result type
* @param the exception type
* @return the function
*/
@SuppressWarnings({"unchecked", "rawtypes"})
public static ExceptionBiFunction, R, E>, ExceptionSupplier, R, E> exceptionSupplierFunctionBiFunction() {
return ExceptionFunctionSupplierBiFunction.INSTANCE;
}
/**
* Get the singleton function which accepts a function and a parameter to pass to the function, and returns the
* result of the function.
*
* @param the argument type
* @param the result type
* @return the function
*/
@SuppressWarnings({"unchecked", "rawtypes"})
public static BiFunction, T, R> functionBiFunction() {
return FunctionBiFunction.INSTANCE;
}
/**
* Get the singleton function which accepts a function and a parameter to pass to the function, and returns the
* result of the function.
*
* @param the argument type
* @param the result type
* @param the exception type
* @return the function
*/
@SuppressWarnings({"unchecked", "rawtypes"})
public static ExceptionBiFunction, T, R, E> exceptionFunctionBiFunction() {
return ExceptionFunctionBiFunction.INSTANCE;
}
/**
* Get the singleton function which accepts a function and a parameter to pass to the function, and returns the
* result of the function.
*
* @param the argument type
* @param the result type
* @return the function
*/
@SuppressWarnings({"unchecked", "rawtypes"})
public static ExceptionBiFunction, T, R, RuntimeException> functionExceptionBiFunction() {
return FunctionExceptionBiFunction.INSTANCE;
}
/**
* Get a supplier which always returns the same value.
*
* @param value the value to return
* @param the value type
* @return the value supplier
*/
@SuppressWarnings("unchecked")
public static Supplier constantSupplier(T value) {
return value == null ? ConstantSupplier.NULL : new ConstantSupplier<>(value);
}
/**
* Get a supplier which always returns the same value.
*
* @param value the value to return
* @param the value type
* @param the exception type
* @return the value supplier
*/
@SuppressWarnings({"unchecked", "rawtypes"})
public static ExceptionSupplier constantExceptionSupplier(T value) {
return value == null ? ConstantSupplier.NULL : new ConstantSupplier(value);
}
/**
* Get a runnable which executes the given consumer with captured values.
*
* @param consumer the consumer to run (must not be {@code null})
* @param param1 the first parameter to pass
* @param param2 the second parameter to pass
* @param the first parameter type
* @param the second parameter type
* @return the capturing runnable
*/
public static Runnable capturingRunnable(BiConsumer consumer, T param1, U param2) {
Assert.checkNotNullParam("consumer", consumer);
return new BiConsumerRunnable(consumer, param1, param2);
}
/**
* Get a runnable which executes the given consumer with captured values.
*
* @param consumer the consumer to run (must not be {@code null})
* @param param the parameter to pass
* @param the parameter type
* @return the capturing runnable
*/
public static Runnable capturingRunnable(Consumer consumer, T param) {
Assert.checkNotNullParam("consumer", consumer);
return new ConsumerRunnable(consumer, param);
}
/**
* Get a runnable which executes the given consumer with captured values.
*
* @param consumer the consumer to run (must not be {@code null})
* @param param1 the first parameter to pass
* @param param2 the second parameter to pass
* @param the first parameter type
* @param the second parameter type
* @param the exception type
* @return the capturing runnable
*/
public static ExceptionRunnable exceptionCapturingRunnable(ExceptionBiConsumer consumer, T param1, U param2) {
Assert.checkNotNullParam("consumer", consumer);
return new ExceptionBiConsumerRunnable(consumer, param1, param2);
}
/**
* Get a runnable which executes the given consumer with captured values.
*
* @param consumer the consumer to run (must not be {@code null})
* @param param the parameter to pass
* @param the parameter type
* @param the exception type
* @return the capturing runnable
*/
public static ExceptionRunnable exceptionCapturingRunnable(ExceptionConsumer consumer, T param) {
Assert.checkNotNullParam("consumer", consumer);
return new ExceptionConsumerRunnable(consumer, param);
}
/**
* Get a consumer which discards the values it is given.
*
* @param the parameter type
* @return the discarding consumer
*/
@SuppressWarnings("unchecked")
public static Consumer discardingConsumer() {
return DiscardingConsumer.INSTANCE;
}
/**
* Get a consumer which discards the values it is given.
*
* @param the parameter type
* @param the exception type
* @return the discarding consumer
*/
@SuppressWarnings("unchecked")
public static ExceptionConsumer discardingExceptionConsumer() {
return DiscardingConsumer.INSTANCE;
}
/**
* Get a consumer which discards the values it is given.
*
* @param the first parameter type
* @param the second parameter type
* @return the discarding consumer
*/
@SuppressWarnings("unchecked")
public static BiConsumer discardingBiConsumer() {
return DiscardingBiConsumer.INSTANCE;
}
/**
* Get a consumer which discards the values it is given.
*
* @param the first parameter type
* @param the second parameter type
* @param the exception type
* @return the discarding consumer
*/
@SuppressWarnings("unchecked")
public static ExceptionBiConsumer discardingExceptionBiConsumer() {
return DiscardingBiConsumer.INSTANCE;
}
static class RunnableConsumer implements Consumer {
static final Consumer INSTANCE = new RunnableConsumer();
private RunnableConsumer() {}
public void accept(final Runnable runnable) {
runnable.run();
}
}
static class ExceptionRunnableConsumer implements ExceptionConsumer, E> {
static final ExceptionConsumer INSTANCE = new ExceptionRunnableConsumer<>();
private ExceptionRunnableConsumer() {}
public void accept(final ExceptionRunnable ExceptionRunnable) throws E {
ExceptionRunnable.run();
}
}
static class RunnableExceptionConsumer implements ExceptionConsumer {
static final RunnableExceptionConsumer INSTANCE = new RunnableExceptionConsumer();
private RunnableExceptionConsumer() {}
public void accept(final Runnable runnable) throws RuntimeException {
runnable.run();
}
}
static class ConsumerBiConsumer implements BiConsumer, Object> {
static final BiConsumer INSTANCE = new ConsumerBiConsumer();
private ConsumerBiConsumer() {}
public void accept(final Consumer