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.
package io.datakernel.util;
import io.datakernel.annotation.Nullable;
import java.util.List;
import java.util.Objects;
import java.util.function.*;
@SuppressWarnings("UnnecessaryLocalVariable")
public class Utils {
private Utils() {
}
@SafeVarargs
public static T coalesce(T... values) {
for (T value : values) {
if (value != null) {
return value;
}
}
return null;
}
public static T coalesce(T a, T b) {
return a != null ? a : b;
}
public static T coalesce(T a, T b, T c) {
return a != null ? a : (b != null ? b : c);
}
public static R transform(@Nullable T seed, Function fn) {
if (seed == null) return null;
R r = fn.apply(seed);
return r;
}
public static R2 transform(@Nullable T seed, Function fn1, Function fn2) {
if (seed == null) return null;
R1 r1 = fn1.apply(seed);
if (r1 == null) return null;
R2 r2 = fn2.apply(r1);
return r2;
}
public static R3 transform(@Nullable T seed, Function fn1, Function fn2, Function fn3) {
if (seed == null) return null;
R1 r1 = fn1.apply(seed);
if (r1 == null) return null;
R2 r2 = fn2.apply(r1);
if (r2 == null) return null;
R3 r3 = fn3.apply(r2);
return r3;
}
public static R4 transform(@Nullable T seed, Function fn1, Function fn2, Function fn3, Function fn4) {
if (seed == null) return null;
R1 r1 = fn1.apply(seed);
if (r1 == null) return null;
R2 r2 = fn2.apply(r1);
if (r2 == null) return null;
R3 r3 = fn3.apply(r2);
if (r3 == null) return null;
R4 r4 = fn4.apply(r3);
return r4;
}
public static T transform(@Nullable T seed, List extends Function super T, ? extends T>> fns) {
for (Function super T, ? extends T> fn : fns) {
seed = fn.apply(seed);
}
return seed;
}
public static String nullToEmpty(@Nullable String string) {
return string != null ? string : "";
}
public static void set(Consumer super V> op, V value) {
op.accept(value);
}
public static void setIf(Consumer super V> op, V value, Predicate super V> predicate) {
if (!predicate.test(value)) return;
op.accept(value);
}
public static void setIfNotNull(Consumer super V> op, V value) {
setIf(op, value, Objects::nonNull);
}
public static void set(Function super V, T> setter, V value) {
setter.apply(value);
}
public static void setIf(Function super V, T> setter, V value, Predicate super V> predicate) {
if (!predicate.test(value)) return;
setter.apply(value);
}
public static void setIfNotNull(Function super V, T> setter, V value) {
setIf(setter, value, Objects::nonNull);
}
public static UnaryOperator apply(BiFunction modifier, V value) {
return instance -> modifier.apply(instance, value);
}
public static UnaryOperator applyIf(BiFunction modifier, V value, Predicate super V> predicate) {
return instance -> {
if (!predicate.test(value)) return instance;
return modifier.apply(instance, value);
};
}
public static UnaryOperator applyIfNotNull(BiFunction modifier, V value) {
return applyIf(modifier, value, Objects::nonNull);
}
}