View Javadoc

1   /*
2    * Copyright 2009-2013 smartics, Kronseder & Reiner GmbH
3    *
4    * Licensed under the Apache License, Version 2.0 (the "License");
5    * you may not use this file except in compliance with the License.
6    * You may obtain a copy of the License at
7    *
8    *     http://www.apache.org/licenses/LICENSE-2.0
9    *
10   * Unless required by applicable law or agreed to in writing, software
11   * distributed under the License is distributed on an "AS IS" BASIS,
12   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13   * See the License for the specific language governing permissions and
14   * limitations under the License.
15   */
16  package de.smartics.validation.constraints;
17  
18  import static java.lang.annotation.ElementType.ANNOTATION_TYPE;
19  import static java.lang.annotation.ElementType.FIELD;
20  import static java.lang.annotation.ElementType.METHOD;
21  import static java.lang.annotation.RetentionPolicy.RUNTIME;
22  
23  import java.lang.annotation.Documented;
24  import java.lang.annotation.Retention;
25  import java.lang.annotation.Target;
26  
27  import javax.validation.Constraint;
28  import javax.validation.Payload;
29  
30  import de.smartics.validation.constraints.impl.NullButNotBlankValidator;
31  
32  /**
33   * The annotated element of type {@link String} may be <code>null</code>, but if
34   * not <code>null</code> it must not be empty or contain only whitespaces.
35   * <p>
36   * Only {@link String} values are allowed.
37   * </p>
38   */
39  @Target(
40  { METHOD, FIELD, ANNOTATION_TYPE })
41  @Retention(RUNTIME)
42  @Documented
43  @Constraint(validatedBy = NullButNotBlankValidator.class)
44  public @interface NullButNotBlank
45  {
46    // ********************************* Fields *********************************
47  
48    /**
49     * The key of the message rendered, if the constraint has been violated.
50     * <p>
51     * The value of this constant is {@value}.
52     * </p>
53     */
54    String MESSAGE_KEY =
55        "{de.smartics.validation.constraints.NullButNotBlank.message}";
56  
57    // ********************************* Methods ********************************
58  
59    /**
60     * Returns the message key to be rendered, if the constraint has been
61     * violated.
62     * <p>
63     * Defaults to returning {@value #MESSAGE_KEY}.
64     * </p>
65     */
66    String message() default MESSAGE_KEY;
67  
68    /**
69     * Support of validation groups.
70     */
71    Class<?>[] groups() default {};
72  
73    /**
74     * Support of payloads.
75     */
76    Class<? extends Payload>[] payload() default {};
77  
78  }