Skip to content

Instantly share code, notes, and snippets.

@kannangce
Last active August 21, 2023 07:21
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save kannangce/be7e78f7c3e2876bb10ff757d802780f to your computer and use it in GitHub Desktop.
Save kannangce/be7e78f7c3e2876bb10ff757d802780f to your computer and use it in GitHub Desktop.
import java.util.function.Function;
import java.util.function.Supplier;
import static java.util.Objects.requireNonNull;
import static java.util.Collections.emptyList;
import static java.util.Collections.emptyMap;
import static java.util.Collections.emptySet;
/**
* Class containing the functional utils
*/
public class FunctionsUtils {
/**
* Does a null-check on the given object and invokes the given function on the object only if it is non-null
* <p>
* Can typically be used for accessing attributes after a null check.
* For ex, obj == null? null: obj.getAttribute() can be replaced with nullSafe(obj, obj::getAttribute)
*
* @param obj The object on which the function to be invoked.
* @param functionToInvoke The function to be invoked in case of non-null value.
* @param <S> The type of the object.
* @param <T> The expected return type
* @return The return value when the given function when the obj is non-null, null otherwise.
*/
public static <S, T> T nullSafe(S obj, Function<S, T> functionToInvoke) {
return nullSafe(obj, functionToInvoke, (Supplier<T>) () -> null);
}
/**
* Does a null-check on the given object and invokes the given function on the object only if it is non-null
* <p>
* Can typically be used for accessing attributes after a null check.
* For ex, obj == null? someMethod(): obj.getAttribute() can be replaced with nullSafe(obj, obj::getAttribute, ()-> someMethod())
*
* @param obj The object on which the function to be invoked.
* @param functionToInvoke The function to be invoked in case of non-null value.
* @param <S> The type of the object.
* @param <T> The expected return type
* @return The return value when the given function when the obj is non-null, The value from the supplier otherwise.
*/
public static <S, T> T nullSafe(S obj, Function<S, T> functionToInvoke, Supplier<T> defaultValueSupplier) {
if (obj == null) {
return requireNonNull(defaultValueSupplier).get();
}
return requireNonNull(functionToInvoke).apply(obj);
}
/**
* Does a null-check on the given object and invokes the given function on the object only if it is non-null
* <p>
* Can typically be used for accessing attributes after a null check.
* For ex, obj == ""? someMethod(): obj.getAttribute() can be replaced with nullSafe(obj, obj::getAttribute, "")
*
* @param obj The object on which the function to be invoked.
* @param functionToInvoke The function to be invoked in case of non-null value.
* @param <S> The type of the object.
* @param <T> The expected return type
* @return The return value when the given function when the obj is non-null, The default value provided otherwise.
*/
public static <S, T> T nullSafe(S obj, Function<S, T> functionToInvoke, T defaultValue) {
return nullSafe(obj, functionToInvoke, (Supplier<T>) () -> defaultValue);
}
/**
* Returns empty list when the input is null.
*
* @param ipList Input list.
* @param <S>
* @return empty list when input is null.
*/
public static <S> List<S> nullSafe(List<S> ipList) {
if (ipList == null) {
return emptyList();
}
return ipList;
}
/**
* Returns empty set when the input is null.
*
* @param ipSet Input set.
* @param <S>
* @return empty set when input is null.
*/
public static <S> Set<S> nullSafe(Set<S> ipSet) {
if (ipSet == null) {
return emptySet();
}
return ipSet;
}
/**
*
* @param ipMap Input map.
* @param <S>
* @param <T>
* @return empty map when input is null, the same map otherwise.
*/
public static <S, T> Map<S, T> nullSafe(Map<S, T> ipMap) {
if (ipMap == null) {
return emptyMap();
}
return ipMap;
}
/**
* Composes 2 given function and returns a new function that applies f2 on the result of f1 when called with a given argument.
* @param f1 Function 1
* @param f2 Function 2
* @return Function that is equivalnet of f2(f1(R))
*/
public static <R, S, T> Function<R, S> compose(Function<R, T> f1, Function<T, S> f2) {
return (R p) -> f2.apply(f1.apply(p));
}
}
Function utils that would typically be required for most of the Java projects.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment