Skip to content

Instantly share code, notes, and snippets.

@DattatreyaReddy
Last active June 7, 2025 07:18
Show Gist options
  • Select an option

  • Save DattatreyaReddy/96ad878b1bf0a13d7ba24d9736429559 to your computer and use it in GitHub Desktop.

Select an option

Save DattatreyaReddy/96ad878b1bf0a13d7ba24d9736429559 to your computer and use it in GitHub Desktop.
abstract class Utils {
/**
* The compose method composes a function operation with the provided value.
* <br/>
* It applies the operation to the value if the value is not null and returns the result.
*/
public static <V, R> R compose(V value, Function<V, R> operation) {
if (Objects.isNull(value) || Objects.isNull(operation)) {
return null;
}
return operation.apply(value);
}
/**
* The compose method applies two operations sequentially to the provided value.
* <br/>
* It first applies operation1 to the value,
* then applies operation2 to the result of operation1,
* and returns the final result.
*/
public static <V, R1, R> R compose(V value, Function<V, R1> operation1,
Function<R1, R> operation2) {
return compose(compose(value, operation1), operation2);
}
public static <V, R1, R2, R> R compose(V value, Function<V, R1> operation1,
Function<R1, R2> operation2, Function<R2, R> operation3) {
return compose(compose(value, operation1, operation2), operation3);
}
/**
* Returns the first non-null value from values or throws error
*
* @param values array values of type T
* @return first non null value
* @throws RuntimeException if no non null value found (Use `orNull` to return null)
* @see SAUtils#orNull(Object[])
*/
@SafeVarargs public static <T> T or(T... values) {
T result = orNull(values);
if (result == null) {
throw new RuntimeException("No valid value found");
}
return result;
}
/**
* Returns the first non-null value from result of the suppliers or throws error
*
* @param suppliers supplier array of type T
* @return first non-null result of suppliers
* @throws RuntimeException if no non-null result found (Use `orNull` to return null)
* @see SAUtils#orNull(Supplier[])
*/
@SafeVarargs public static <T> T or(Supplier<T>... suppliers) {
T result = orNull(suppliers);
if (result == null) {
throw new RuntimeException("No valid value found");
}
return result;
}
/**
* Returns the first non-null value from result of the suppliers
*
* @param values array values of type T
* @return first non-null result of suppliers
* @see SAUtils#or(Object[])
*/
@SafeVarargs public static <T> T orNull(T... values) {
for (T value : values) {
if (value != null) {
return value;
}
}
return null;
}
/**
* Returns the first non-null value from result of the suppliers or throws error
*
* @param suppliers supplier array of type T
* @return first non-null result of suppliers
* @see SAUtils#or(Supplier[])
*/
@SafeVarargs public static <T> T orNull(Supplier<T>... suppliers) {
for (Supplier<T> supplier : suppliers) {
if (supplier == null) {
continue;
}
T result = supplier.get();
if (result != null) {
return result;
}
}
return null;
}
@SafeVarargs
public static <V, R> R compose(V value, Function<?, ?>... functions) {
Object result = value;
for (Function<?, ?> function : functions) {
if (function == null || result == null) return null;
result = ((Function<Object, Object>) function).apply(result);
}
return (R) result;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment