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

org.hibernate.validator.internal.constraintvalidators.bv.notempty.NotEmptyValidatorForCollection Maven / Gradle / Ivy

There is a newer version: 8.0.1.Final
Show newest version
/*
 * Hibernate Validator, declare and validate application constraints
 *
 * License: Apache License, Version 2.0
 * See the license.txt file in the root directory or .
 */
package org.hibernate.validator.internal.constraintvalidators.bv.notempty;

import java.util.Collection;

import jakarta.validation.ConstraintValidator;
import jakarta.validation.ConstraintValidatorContext;
import jakarta.validation.constraints.NotEmpty;

/**
 * Check that the collection is not null and not empty.
 *
 * @author Guillaume Smet
 */
// as per the JLS, Collection is a subtype of Collection, so we need to explicitly reference
// Collection here to support having properties defined as Collection (see HV-1551)
@SuppressWarnings("rawtypes")
public class NotEmptyValidatorForCollection implements ConstraintValidator {

	/**
	 * Checks the collection is not {@code null} and not empty.
	 *
	 * @param collection the collection to validate
	 * @param constraintValidatorContext context in which the constraint is evaluated
	 * @return returns {@code true} if the collection is not {@code null} and the collection is not empty
	 */
	@Override
	public boolean isValid(Collection collection, ConstraintValidatorContext constraintValidatorContext) {
		if ( collection == null ) {
			return false;
		}
		return collection.size() > 0;
	}
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy