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

com.github.ldeitos.validators.NotEmptyValidatorImpl Maven / Gradle / Ivy

package com.github.ldeitos.validators;

import static org.apache.commons.lang3.ArrayUtils.getLength;

import java.util.Collection;
import java.util.Map;
import java.util.function.Predicate;

import com.github.ldeitos.constraint.NotEmpty;

public class NotEmptyValidatorImpl extends MultiTargetValidator implements NotEmptyValidator {

	private static final Class[] targetClasses = { Collection.class, Map.class, CharSequence.class };

	@Override
	public void initialize(NotEmpty constraintAnnotation) {
	}

	@Override
	protected boolean doValidation(Object value) {
		Class valueClass = value.getClass();
		int size;

		if (CharSequence.class.isAssignableFrom(valueClass)) {
			size = CharSequence.class.cast(value).length();
		} else if (Collection.class.isAssignableFrom(valueClass)) {
			size = Collection.class.cast(value).size();
		} else if (Map.class.isAssignableFrom(valueClass)) {
			size = Map.class.cast(value).size();
		} else {
			size = getLength(value);
		}

		return size > 0;
	}

	@Override
	protected Class[] getTargetClasses() {
		return targetClasses;
	}

	@Override
	protected Predicate> getMustValidatePredicate(final Class valueClass) {
		Predicate> assignableFrom = new Predicate>() {
			@Override
			public boolean test(Class arg0) {
				return valueClass.getName().startsWith("[") || arg0.isAssignableFrom(valueClass);
			}
		};
		return assignableFrom;
	}

}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy