(no commit message)
[utils] / support / general / src / main / java / org / wamblee / conditions / PropertyRegexCondition.java
1 /*
2  * Copyright 2005-2010 the original author or authors.
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 org.wamblee.conditions;
17
18 import java.lang.reflect.InvocationTargetException;
19 import java.lang.reflect.Method;
20 import java.util.regex.Matcher;
21 import java.util.regex.Pattern;
22
23 /**
24  * Condition to check whether a given property of an object matches a certain
25  * regular expression. The method name to use of the object passed in to
26  * {@link #matches(Object)} is obtained using the Javabean conventions.
27  * 
28  * @author Erik Brakkee
29  * 
30  */
31 public class PropertyRegexCondition<T> implements Condition<T> {
32     /**
33      * Property name.
34      */
35     private String property;
36
37     /**
38      * Regular expression.
39      */
40     private Pattern regex;
41
42     /**
43      * Whether or not to convert the value to lowercase before matching.
44      */
45     private boolean tolower;
46
47     /**
48      * Constructs the condition.
49      * 
50      * @param aProperty
51      *            Name of the property to examine.
52      * @param aRegex
53      *            Regular expression to use.
54      * @param aTolower
55      *            Whether or not to convert the value to lowercase before
56      *            matching.
57      */
58     public PropertyRegexCondition(String aProperty, String aRegex,
59         boolean aTolower) {
60         property = aProperty;
61         regex = Pattern.compile(aRegex);
62         tolower = aTolower;
63     }
64
65     /*
66      * (non-Javadoc)
67      * 
68      * @see org.wamblee.conditions.Condition#matches(T)
69      */
70     public boolean matches(T aObject) {
71         try {
72             String readerName = "get" + property.substring(0, 1).toUpperCase() +
73                 property.substring(1);
74             Method reader = aObject.getClass().getMethod(readerName);
75             String value = reader.invoke(aObject) + "";
76
77             if (tolower) {
78                 value = value.toLowerCase();
79             }
80
81             Matcher matcher = regex.matcher(value);
82
83             return matcher.matches();
84         } catch (NoSuchMethodException e) {
85             throw new RuntimeException(e.getMessage(), e);
86         } catch (IllegalAccessException e) {
87             throw new RuntimeException(e.getMessage(), e);
88         } catch (InvocationTargetException e) {
89             throw new RuntimeException(e.getMessage(), e);
90         }
91     }
92 }