de.thksystems.validation.beanvalidation.AbstractNotEmptyValidator Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of mugwort Show documentation
Show all versions of mugwort Show documentation
Commons for persistence, hibernate, xstreams, enterprise, spring, servlets, ...
package de.thksystems.validation.beanvalidation;
import java.lang.reflect.Array;
import java.util.Collection;
import org.apache.commons.lang3.StringUtils;
public class AbstractNotEmptyValidator {
protected boolean isNotEmpty(Object fieldValue) {
if (fieldValue == null) {
return false;
}
// Check for empty String
if (fieldValue instanceof CharSequence) {
return StringUtils.isNotEmpty((String) fieldValue);
}
// Check for empty collection
if (fieldValue instanceof Collection>) {
return !((Collection>) fieldValue).isEmpty();
}
// Check for empty array
if (fieldValue.getClass().isArray()) {
return Array.getLength(fieldValue) > 0;
}
// Any other object that is not null
return true;
}
}