
com.github.dakusui.floorplan.utils.Checks Maven / Gradle / Ivy
package com.github.dakusui.floorplan.utils;
import com.github.dakusui.floorplan.exception.Exceptions;
import java.util.function.Function;
import java.util.function.Predicate;
import java.util.function.Supplier;
import static java.lang.String.format;
public enum Checks {
;
public static T requireArgument(T value, Predicate condition) {
return requireArgument(value, condition, v -> format("'%s' did not not satisfy <%s>", v, condition));
}
public static T requireArgument(T value, Predicate condition, Function messageComposer) {
return requireArgument(value, condition, () -> messageComposer.apply(value));
}
public static T requireArgument(T value, Predicate condition, Supplier messageComposer) {
if (condition.test(value))
return value;
throw Exceptions.throwExceptionForIllegalValue(messageComposer.get());
}
public static T requireState(T value, Predicate condition) {
return requireState(value, condition, v -> format("'%s' did not not satisfy <%s>", v, condition));
}
public static T requireState(T value, Predicate condition, Function messageComposer) {
if (condition.test(value))
return value;
throw Exceptions.throwExceptionForIllegalState(messageComposer.apply(value));
}
public static T require(T value, Predicate condition, Supplier exceptionThrower) {
return require(value, condition, d -> exceptionThrower);
}
public static T require(T value, Predicate condition, Function> exceptionThrower) {
if (condition.test(value))
return value;
throw exceptionThrower.apply(value).get();
}
public static T requireNonNull(T value) {
return requireNonNull(value, () -> null);
}
public static T requireNonNull(T value, String message) {
return requireNonNull(value, () -> message);
}
public static T requireNonNull(T value, Supplier messageSupplier) {
if (value != null)
return value;
throw Exceptions.throwExceptionForNullValue(messageSupplier.get());
}
}
© 2015 - 2025 Weber Informatics LLC | Privacy Policy