org.wildfly.common.function.Functions Maven / Gradle / Ivy
Go to download
This artifact provides a single jar that contains all classes required to use remote EJB and JMS, including
all dependencies. It is intended for use by those not using maven, maven users should just import the EJB and
JMS BOM's instead (shaded JAR's cause lots of problems with maven, as it is very easy to inadvertently end up
with different versions on classes on the class path).
The newest version!
/*
* 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.BiPredicate;
import java.util.function.Consumer;
import java.util.function.DoubleFunction;
import java.util.function.Function;
import java.util.function.IntFunction;
import java.util.function.LongFunction;
import java.util.function.ObjDoubleConsumer;
import java.util.function.ObjIntConsumer;
import java.util.function.ObjLongConsumer;
import java.util.function.Predicate;
import java.util.function.Supplier;
import java.util.function.ToDoubleBiFunction;
import java.util.function.ToDoubleFunction;
import java.util.function.ToIntBiFunction;
import java.util.function.ToIntFunction;
import java.util.function.ToLongBiFunction;
import java.util.function.ToLongFunction;
import org.jboss.logging.Logger;
import org.wildfly.common.Assert;
/**
* A set of utility methods which return common functions.
*/
public final class Functions {
private Functions() {}
private static final Logger LOGGER = Logger.getLogger(Functions.class);
private static final Consumer CLOSING_CONSUMER = quiet(AutoCloseable::close, exceptionLoggingConsumer());
/**
* Returns a consumer that quietly closes its argument, logging any exceptions.
* @return a closing consumer
*/
@SuppressWarnings("unchecked")
public static Consumer closingConsumer() {
return (Consumer) CLOSING_CONSUMER;
}
private static final Consumer EXCEPTION_LOGGER = new Consumer() {
@Override
public void accept(Exception e) {
LOGGER.warn(e.getLocalizedMessage(), e);
}
};
/**
* Returns a consumer that logs its exception parameter as a warning.
* @param the exception type
* @return an exception consumer
*/
@SuppressWarnings("unchecked")
public static Consumer exceptionLoggingConsumer() {
return (Consumer) EXCEPTION_LOGGER;
}
/**
* Returns a consumer that wraps and throws its exception parameter as a {@link RuntimeException}.
* @param the exception type
* @return an exception consumer
*/
public static Consumer runtimeExceptionThrowingConsumer(Function runtimeExceptionWrapper) {
return new Consumer() {
@Override
public void accept(E exception) {
throw runtimeExceptionWrapper.apply(exception);
}
};
}
/**
* Converts an {@link ExceptionConsumer} to a standard {@link Consumer} using the specified exception handler.
* @param the parameter type of the consumer
* @param the exception type
* @param consumer an exception consumer
* @param exceptionHandler an exception handler
* @return a standard consumer
*/
public static Consumer quiet(ExceptionConsumer consumer, Consumer exceptionHandler) {
return new Consumer() {
@SuppressWarnings("unchecked")
@Override
public void accept(T value) {
try {
consumer.accept(value);
} catch (Exception e) {
exceptionHandler.accept((E) e);
}
}
};
}
/**
* Converts an {@link ExceptionBiConsumer} to a standard {@link BiConsumer} using the specified exception handler.
* @param the first parameter type of the consumer
* @param the second parameter type of the consumer
* @param the exception type
* @param consumer a binary exception consumer
* @param exceptionHandler an exception handler
* @return a standard binary consumer
*/
public static BiConsumer quiet(ExceptionBiConsumer consumer, Consumer exceptionHandler) {
return new BiConsumer() {
@SuppressWarnings("unchecked")
@Override
public void accept(T value1, U value2) {
try {
consumer.accept(value1, value2);
} catch (Exception e) {
exceptionHandler.accept((E) e);
}
}
};
}
/**
* Converts an {@link ExceptionObjIntConsumer} to a standard {@link ObjIntConsumer} using the specified exception handler.
* @param the first parameter type of the consumer
* @param the exception type
* @param consumer an object/int exception consumer
* @param exceptionHandler an exception handler
* @return a standard object/int consumer
*/
public static ObjIntConsumer quiet(ExceptionObjIntConsumer consumer, Consumer exceptionHandler) {
return new ObjIntConsumer() {
@SuppressWarnings("unchecked")
@Override
public void accept(T object, int i) {
try {
consumer.accept(object, i);
} catch (Exception e) {
exceptionHandler.accept((E) e);
}
}
};
}
/**
* Converts an {@link ExceptionObjLongConsumer} to a standard {@link ObjLongConsumer} using the specified exception handler.
* @param the first parameter type of the consumer
* @param the exception type
* @param consumer an object/long exception consumer
* @param exceptionHandler an exception handler
* @return a standard object/long consumer
*/
public static ObjLongConsumer quiet(ExceptionObjLongConsumer consumer, Consumer exceptionHandler) {
return new ObjLongConsumer() {
@SuppressWarnings("unchecked")
@Override
public void accept(T object, long i) {
try {
consumer.accept(object, i);
} catch (Exception e) {
exceptionHandler.accept((E) e);
}
}
};
}
/**
* 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;
}
/**
* Returns a {@link Consumer} with identical behavior to the specified {@link Consumer} but with restricted parameter type.
* @param the parameter type
* @param the restricted parameter type
* @param consumer a consumer
* @return a functionally equivalent consumer
*/
@SuppressWarnings("unchecked")
public static Consumer cast(Consumer consumer) {
return (Consumer) consumer;
}
/**
* Returns a {@link Predicate} with identical behavior to the specified {@link Predicate} but with restricted parameter type.
* @param the parameter type
* @param the restricted parameter type
* @param predicate a predicate
* @return a functionally equivalent predicate
*/
@SuppressWarnings("unchecked")
public static Predicate cast(Predicate predicate) {
return (Predicate) predicate;
}
/**
* Returns a {@link Supplier} with identical behavior to the specified {@link Supplier} but with relaxed return type.
* @param the return type
* @param the relaxed return type
* @param supplier a supplier
* @return a functionally equivalent supplier
*/
@SuppressWarnings("unchecked")
public static Supplier cast(Supplier supplier) {
return (Supplier) supplier;
}
/**
* Returns a {@link Function} with identical behavior to the specified {@link Function} but with restricted parameter type and relaxed return type.
* @param the parameter type
* @param the return type
* @param the restricted parameter type
* @param the relaxed return type
* @param function a function
* @return a functionally equivalent function
*/
@SuppressWarnings("unchecked")
public static Function cast(Function function) {
return (Function) function;
}
/**
* Returns a {@link DoubleFunction} with identical behavior to the specified {@link DoubleFunction} but with relaxed return type.
* @param the return type
* @param the relaxed return type
* @param function a function
* @return a functionally equivalent function
*/
@SuppressWarnings("unchecked")
public static DoubleFunction cast(DoubleFunction function) {
return (DoubleFunction) function;
}
/**
* Returns a {@link IntFunction} with identical behavior to the specified {@link IntFunction} but with relaxed return type.
* @param the return type
* @param the relaxed return type
* @param function a function
* @return a functionally equivalent function
*/
@SuppressWarnings("unchecked")
public static IntFunction cast(IntFunction function) {
return (IntFunction) function;
}
/**
* Returns a {@link LongFunction} with identical behavior to the specified {@link LongFunction} but with relaxed return type.
* @param the return type
* @param the relaxed return type
* @param function a function
* @return a functionally equivalent function
*/
@SuppressWarnings("unchecked")
public static LongFunction cast(LongFunction function) {
return (LongFunction) function;
}
/**
* Returns a {@link ToDoubleFunction} with identical behavior to the specified {@link ToDoubleFunction} but with restricted parameter type.
* @param the parameter type
* @param the restricted parameter type
* @param function a function
* @return a functionally equivalent function
*/
@SuppressWarnings("unchecked")
public static ToDoubleFunction cast(ToDoubleFunction function) {
return (ToDoubleFunction) function;
}
/**
* Returns a {@link ToIntFunction} with identical behavior to the specified {@link ToIntFunction} but with restricted parameter type.
* @param the parameter type
* @param the restricted parameter type
* @param function a function
* @return a functionally equivalent function
*/
@SuppressWarnings("unchecked")
public static ToIntFunction cast(ToIntFunction function) {
return (ToIntFunction) function;
}
/**
* Returns a {@link ToLongFunction} with identical behavior to the specified {@link ToLongFunction} but with restricted parameter type.
* @param the parameter type
* @param the restricted parameter type
* @param function a function
* @return a functionally equivalent function
*/
@SuppressWarnings("unchecked")
public static ToLongFunction cast(ToLongFunction function) {
return (ToLongFunction) function;
}
/**
* Returns a {@link BiConsumer} with identical behavior to the specified {@link BiConsumer} but with restricted parameter types.
* @param the first parameter type
* @param the second parameter type
* @param the restricted first parameter type
* @param the restricted second parameter type
* @param consumer a consumer
* @return a functionally equivalent consumer
*/
@SuppressWarnings("unchecked")
public static BiConsumer cast(BiConsumer consumer) {
return (BiConsumer) consumer;
}
/**
* Returns a {@link ObjDoubleConsumer} with identical behavior to the specified {@link ObjDoubleConsumer} but with restricted parameter type.
* @param the parameter type
* @param the restricted parameter type
* @param consumer a consumer
* @return a functionally equivalent consumer
*/
@SuppressWarnings("unchecked")
public static ObjDoubleConsumer cast(ObjDoubleConsumer consumer) {
return (ObjDoubleConsumer) consumer;
}
/**
* Returns a {@link ObjIntConsumer} with identical behavior to the specified {@link ObjIntConsumer} but with restricted parameter type.
* @param the parameter type
* @param the restricted parameter type
* @param consumer a consumer
* @return a functionally equivalent consumer
*/
@SuppressWarnings("unchecked")
public static ObjIntConsumer cast(ObjIntConsumer consumer) {
return (ObjIntConsumer) consumer;
}
/**
* Returns a {@link ObjLongConsumer} with identical behavior to the specified {@link ObjLongConsumer} but with restricted parameter type.
* @param the parameter type
* @param the restricted parameter type
* @param consumer a consumer
* @return a functionally equivalent consumer
*/
@SuppressWarnings("unchecked")
public static ObjLongConsumer cast(ObjLongConsumer consumer) {
return (ObjLongConsumer) consumer;
}
/**
* Returns a {@link BiPredicate} with identical behavior to the specified {@link BiPredicate} but with restricted parameter types.
* @param the first parameter type
* @param the second parameter type
* @param the restricted first parameter type
* @param the restricted second parameter type
* @param predicate a predicate
* @return a functionally equivalent predicate
*/
@SuppressWarnings("unchecked")
public static BiPredicate cast(BiPredicate predicate) {
return (BiPredicate) predicate;
}
/**
* Returns a {@link BiFunction} with identical behavior to the specified {@link BiFunction} but with restricted parameter types and relaxed return type.
* @param the first parameter type
* @param the second parameter type
* @param the return type
* @param the restricted first parameter type
* @param the restricted second parameter type
* @param the relaxed return type
* @param function a function
* @return a functionally equivalent function
*/
@SuppressWarnings("unchecked")
public static BiFunction cast(BiFunction function) {
return (BiFunction) function;
}
/**
* Returns a {@link ToDoubleBiFunction} with identical behavior to the specified {@link ToDoubleBiFunction} but with restricted parameter types.
* @param the first parameter type
* @param the second parameter type
* @param the restricted first parameter type
* @param the restricted second parameter type
* @param function a function
* @return a functionally equivalent function
*/
@SuppressWarnings("unchecked")
public static ToDoubleBiFunction cast(ToDoubleBiFunction function) {
return (ToDoubleBiFunction) function;
}
/**
* Returns a {@link ToIntBiFunction} with identical behavior to the specified {@link ToIntBiFunction} but with restricted parameter types.
* @param the first parameter type
* @param