[hibernate-dev] [Bean Validation] @Pattern

Emmanuel Bernard emmanuel at hibernate.org
Tue Feb 17 07:26:01 EST 2009


I had a lot of demands for regexp support and since there is a need  
for string matching in the built-in constraints...

http://opensource.atlassian.com/projects/hibernate/browse/HV-121

I replaced the bitwise guru flags approach with an enum
@Pattern( regexp = ".*", flags = { Pattern.Flag.CANON_EQ })
String name;

/**
* The annotated String must match the following regular expression.
* The regular expression follows the Java regular expression conventions
* see {@link java.util.regex.Pattern}.
*
* Accepts String.
*
* @author Emmanuel Bernard
*
*/
@Target({ METHOD, FIELD, ANNOTATION_TYPE })
@Retention(RUNTIME)
@Documented
public @interface Pattern {
	/**
	 * regular expression to match
	 */
	String regexp();

	/**
	 * Flags considered when resolving the regular expression
	 */
	Flag[] flags() default {};

	/**
	 * error message template
	 */
	String message() default "{validator.past}";

	/**
	 * groups the constraint belongs to
	 */
	Class<?>[] groups() default { };

	/**
	 * Possible Regexp flags
	 */
	public static enum Flag {
		/**
		 * Enables Unix lines mode
		 * @see java.util.regex.Pattern#UNIX_LINES
		 */
		UNIX_LINES,

		/**
		 * Enables case-insensitive matching
		 * @see java.util.regex.Pattern#CASE_INSENSITIVE
		 */
		CASE_INSENSITIVE,

		/**
		 * Permits whitespace and comments in pattern
		 * @see java.util.regex.Pattern#COMMENTS
		 */
		COMMENTS,

		/**
		 * Enables multiline mode
		 * @see java.util.regex.Pattern#MULTILINE
		 */
		MULTILINE,

		/**
		 * Enables dotall mode
		 * @see java.util.regex.Pattern#DOTALL
		 */
		DOTALL,

		/**
		 * Enables Unicode-aware case folding
		 * @see java.util.regex.Pattern#UNICODE_CASE
		 */
		UNICODE_CASE,

		/**
		 * Enables canonical equivalence
		 * @see java.util.regex.Pattern#CANON_EQ
		 */
		CANON_EQ,
	}
}



More information about the hibernate-dev mailing list