Java Command Annotations – @Pattern
Java command annotations are used for the developing Java commands in PAK.
This chapter describes the functionality of the @Pattern
annotation.
It also gives an example of how you can use this annotation in your implementation.
1. Basics
The @Pattern
annotation restricts the allowed values of a variable to those that match the specified regex pattern.
This annotation is evaluated at runtime and raises an exception at the latest.
This annotation is only applicable for variables that are annotated with @Persistent and of type String.
|
2. Properties
There are four properties defined for the @Pattern
annotation that will be explained in the following.
The only mandatory property is value
, the remaining ones (example
, emptyOrBlankAllowed
, errorMessage
) are optional.
-
value
:
The regex has to be assigned to this property. It defines which variable values are allowed to be set. -
example
:
This property is used for the user documentation.
You can define examples to show the user which values will be accepted by the regex. -
emptyOrBlankAllowed
:
For Patterns, that are matched by an empty or blank string (as is the case when using .*), this flag lets you decide if empty or blank inputs should be accepted.
Setting the property to false will result in the rejection of empty inputs as if they do not match the pattern.
The default is true (the pattern is matched, even if the input string is empty). -
errorMessage
:
This property is used to set an individual error message, which will be displayed if the specified pattern is not matched.
3. Usage
Listing 1 shows how to use the @Pattern
annotation with different regex values and the example
property usage.
@Pattern
annotation/**
* @workflowDocu This command shows how to use the @Persistent annotation.
*/
@JavaCommand
@CommandGroup("Element")
public class GetElement {
/**
* @workflowDocu List of labels to be added, separated by ","
*/
@Pattern(value = "\\w*(,\\w+)*", (1)
example = "elem1,elem2",
emptyOrBlankAllowed = false,
errorMessage = "The given list elements need to be separated by commas!")
@Persistent
private String commaSep;
/**
* @workflowDocu Only allow numbers
*/
@Persistent
@Pattern("\\d+") (2)
private String numbers;
[...]
}
1 | Allow comma-separated Strings for variable commaSep , the string cannot be empty and an individual error message is displayed, if the input does not match |
2 | Only numbers are allowed for the values of variable number |
Figure 1 shows the result of the @Pattern
usage of Listing 1.
As you can see, the regex as well as the example are provided in the documentation information popup.
The frame of the variable remains red as long as no valid value is entered into the text variable, as can be seen in Figure 2.
After the entered value matches the regex the red border disappears like in Figure 3.