View Javadoc
1   package lv.id.jc.piglatin.core;
2   
3   import static java.lang.annotation.ElementType.FIELD;
4   import static java.lang.annotation.ElementType.METHOD;
5   import static java.lang.annotation.ElementType.PARAMETER;
6   import static java.lang.annotation.RetentionPolicy.RUNTIME;
7   
8   import java.lang.annotation.Documented;
9   import java.lang.annotation.Retention;
10  import java.lang.annotation.Target;
11  
12  import jakarta.validation.Constraint;
13  import jakarta.validation.Payload;
14  import jakarta.validation.constraints.NotBlank;
15  import jakarta.validation.constraints.NotNull;
16  import jakarta.validation.constraints.Pattern;
17  
18  /**
19   * A single word.
20   * <p>
21   * A single word contains only Unicode letters and apostrophe (') in the middle.
22   * Only one apostrophe is allowed.
23   * <p>
24   * Examples:
25   * <ul>
26   *     <li>word</li>
27   *     <li>it's</li>
28   *     <li>don't</li>
29   *     <li>can't</li>
30   *     <li>o'clock</li>
31   * </ul>
32   */
33  @Documented
34  @Retention(RUNTIME)
35  @Constraint(validatedBy = {})
36  @Target({PARAMETER, METHOD, FIELD})
37  @NotNull
38  @NotBlank
39  @Pattern(regexp = """
40      #
41      # A single word
42      # contains only Unicode letters and apostrophe (') in the middle
43      # only one apostrophe is allowed
44      #
45      ^               # Start of the line
46      (               # Start of the group
47          \\p{L}      # Any Unicode letter
48      |               # OR
49          (?<=\\p{L}) # Positive lookbehind for a Unicode letter
50          '           # An apostrophe
51          (?=\\p{L})  # Positive lookahead for a Unicode letter
52          (?!.*')     # Negative lookahead for another apostrophe
53      )               # End of the group
54      +               # One or more of the previous group
55      $               # End of the line
56      """,
57      flags = {Pattern.Flag.COMMENTS, Pattern.Flag.CASE_INSENSITIVE}
58  )
59  public @interface Word {
60      /**
61       * The default error message if the validation fails.
62       *
63       * @return the error message
64       */
65      String message() default "must be a single word";
66  
67      /**
68       * The groups the constraint belongs to.
69       *
70       * @return the groups
71       */
72      Class<?>[] groups() default {};
73  
74      /**
75       * The payload that can be associated with a constraint violation.
76       *
77       * @return the payload
78       */
79      Class<? extends Payload>[] payload() default {};
80  }