1
2
3
4
5
6
7
8
9
10
11
12 package ca.spaz.cron.config;
13
14 import java.util.regex.*;
15
16 public abstract class AbstractRegexKeyValidator implements PropertyValidator {
17
18 protected Matcher getMatcher(String string) {
19 if (null == string) {
20 throw new IllegalArgumentException("Null strings are not allowed");
21 }
22 return Pattern.compile(getExpression()).matcher(string);
23 }
24
25
26
27
28 public boolean isValid(String key, String value) {
29 boolean valid = false;
30 if (getMatcher(key).find()) {
31 valid = doIsValid(key, value);
32 }
33 return valid;
34 }
35
36 /***
37 * This method will be called only in the case where the pattern matches. This
38 * can be assumed to be a precondition of the method call.
39 *
40 * @param key The matching key.
41 * @param value The value being checked.
42 * @return <code>true</code> if <code>value</code> is a valid value for
43 * <code>key</code>, <code>false</code> otherwise.
44 */
45 protected abstract boolean doIsValid(String key, String value);
46
47 /***
48 * Get the regular expression to match the key on.
49 * @return a string representation of a regular expression.
50 */
51 protected abstract String getExpression();
52
53 }