1 /*
2 * Hibernate Validator, declare and validate application constraints
3 *
4 * License: Apache License, Version 2.0
5 * See the license.txt file in the root directory or <http://www.apache.org/licenses/LICENSE-2.0>.
6 */
7 package org.hibernate.validator.internal.constraintvalidators.bv.notempty;
8
9 import java.util.Collection;
10
11 import javax.validation.ConstraintValidator;
12 import javax.validation.ConstraintValidatorContext;
13 import javax.validation.constraints.NotEmpty;
14
15 /**
16 * Check that the collection is not null and not empty.
17 *
18 * @author Guillaume Smet
19 */
20 // as per the JLS, Collection<?> is a subtype of Collection, so we need to explicitly reference
21 // Collection here to support having properties defined as Collection (see HV-1551)
22 @SuppressWarnings("rawtypes")
23 public class NotEmptyValidatorForCollection implements ConstraintValidator<NotEmpty, Collection> {
24
25 /**
26 * Checks the collection is not {@code null} and not empty.
27 *
28 * @param collection the collection to validate
29 * @param constraintValidatorContext context in which the constraint is evaluated
30 * @return returns {@code true} if the collection is not {@code null} and the collection is not empty
31 */
32 @Override
33 public boolean isValid(Collection collection, ConstraintValidatorContext constraintValidatorContext) {
34 if ( collection == null ) {
35 return false;
36 }
37 return collection.size() > 0;
38 }
39 }
40