All Downloads are FREE. Search and download functionalities are using the official Maven repository.

io.datakernel.util.Utils Maven / Gradle / Ivy

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> fns) {
		for (Function fn : fns) {
			seed = fn.apply(seed);
		}
		return seed;
	}

	public static String nullToEmpty(@Nullable String string) {
		return string != null ? string : "";
	}

	public static  void set(Consumer op, V value) {
		op.accept(value);
	}

	public static  void setIf(Consumer op, V value, Predicate predicate) {
		if (!predicate.test(value)) return;
		op.accept(value);
	}

	public static  void setIfNotNull(Consumer op, V value) {
		setIf(op, value, Objects::nonNull);
	}

	public static  void set(Function setter, V value) {
		setter.apply(value);
	}

	public static  void setIf(Function setter, V value, Predicate predicate) {
		if (!predicate.test(value)) return;
		setter.apply(value);
	}

	public static  void setIfNotNull(Function 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 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);
	}

}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy