Skip to content

Instantly share code, notes, and snippets.

@gitjs77
Created August 9, 2017 15:55
Show Gist options
  • Save gitjs77/c75780b066ea87f49a146e6e67558e6b to your computer and use it in GitHub Desktop.
Save gitjs77/c75780b066ea87f49a146e6e67558e6b to your computer and use it in GitHub Desktop.
/**
* Class for validation of any fields on null.
* It helps to avoid of duplicate code.
*
*/
public interface CommonValidator {
/**
* Validate some object for null and throw ValidationException when validation failed.
*
* @param object - object for validate
* @param objectName - validation object name
* @param clazzz - class where validator was called
*/
static void validateObjectOnNull(final Object object, final String objectName, final Class clazzz) {
if (Objects.isNull(object)) {
throw new ValidationException(clazzz, objectName, objectName + " == null", "NPE");
}
}
/**
* Validate some object for null and return boolean result.
*
* @param object - object for validate
* @return object == null ? true : false
*/
static boolean validateObjectOnNullReturnBoolean(final Object object) {
return Objects.isNull(object);
}
/**
* Validate some objects for null and return boolean result.
*
* @param objects - objects for validation
* @return boolean result
* @Test not tested.
*/
static boolean validateObjectsOnNullReturnBoolean(final Object... objects) {
/*return objects.length == 1
? validateObjectOnNullReturnBoolean(objects[0])
: Stream.of(objects).noneMatch(Objects::isNull);*/
return Stream.of(objects).anyMatch(CommonValidator::validateObjectOnNullReturnBoolean);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment