
com.github.dakusui.valid8j_pcond.experimentals.currying.Checks Maven / Gradle / Ivy
The newest version!
package com.github.dakusui.valid8j_pcond.experimentals.currying;
import com.github.dakusui.valid8j_pcond.internals.InternalChecks;
import java.util.function.Supplier;
import static com.github.dakusui.valid8j_pcond.internals.InternalUtils.formatObject;
import static com.github.dakusui.valid8j_pcond.internals.InternalUtils.wrapperClassOf;
import static java.lang.String.format;
/**
* A utility class for checking values that the "currying" mechanism of the `pcond`
* library processes.
*/
public enum Checks {
;
static > T requireLast(T value) {
if (value.hasNext())
throw new IllegalStateException();
return value;
}
/**
* Validates if a given argument value is appropriate for a parameter type (`paramType`).
*
* @param arg An argument value is to check with `paramType`.
* @param paramType An expected type for `arg`.
* @param messageFormatter A message formatter which generates a message on a failure.
* @param The type of the argument value.
* @return The `arg` value itself.
*/
static T validateArgumentType(T arg, Class> paramType, Supplier messageFormatter) {
InternalChecks.checkArgument(isValidValueForType(arg, paramType), messageFormatter);
return arg;
}
static boolean isValidValueForType(Object arg, Class> paramType) {
if (paramType.isPrimitive()) {
if (arg == null)
return paramType.equals(void.class);
if (InternalChecks.isPrimitiveWrapperClassOrPrimitive(arg.getClass())) {
Class> wrapperClassForParamType = wrapperClassOf(paramType);
if (wrapperClassForParamType.equals(arg.getClass()))
return true;
return InternalChecks.isWiderThan(wrapperClassForParamType, arg.getClass());
}
return false;
} else {
if (arg == null)
return true;
return paramType.isAssignableFrom(arg.getClass());
}
}
@SuppressWarnings("unchecked")
static T ensureReturnedValueType(Object value, Class> returnType) {
if (isValidValueForType(value, returnType))
return (T) value;
else
throw new IllegalStateException("Returned value:"
+ formatObject(value)
+ (value != null ? "(" + value.getClass().getName() + ")" : "")
+ " is neither null nor an instance of " + returnType.getName() + ". ");
}
}
© 2015 - 2025 Weber Informatics LLC | Privacy Policy