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