Removed DOCUMENT ME comments that were generated and applied source code
[utils] / support / general / src / main / java / org / wamblee / conditions / PropertyRegexCondition.java
1 /*
2  * Copyright 2005 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 value matches a certain regular
27  * expression.
28  * 
29  * @author Erik Brakkee
30  * 
31  */
32 public class PropertyRegexCondition<T> implements Condition<T> {
33     /**
34      * Property name.
35      */
36     private String property;
37
38     /**
39      * Regular expression.
40      */
41     private Pattern regex;
42
43     /**
44      * Whether or not to convert the value to lowercase before matching.
45      */
46     private boolean tolower;
47
48     /**
49      * Constructs the condition.
50      * 
51      * @param aProperty
52      *            Name of the property to examine.
53      * @param aRegex
54      *            Regular expression to use.
55      * @param aTolower
56      *            Whether or not to convert the value to lowercase before
57      *            matching.
58      */
59     public PropertyRegexCondition(String aProperty, String aRegex,
60         boolean aTolower) {
61         property = aProperty;
62         regex = Pattern.compile(aRegex);
63         tolower = aTolower;
64     }
65
66     /*
67      * (non-Javadoc)
68      * 
69      * @see org.wamblee.conditions.Condition#matches(T)
70      */
71     public boolean matches(T aObject) {
72         try {
73             String value = PropertyUtils.getProperty(aObject, property) + "";
74
75             if (tolower) {
76                 value = value.toLowerCase();
77             }
78
79             Matcher matcher = regex.matcher(value);
80
81             return matcher.matches();
82         } catch (IllegalAccessException e) {
83             throw new RuntimeException(e.getMessage(), e);
84         } catch (InvocationTargetException e) {
85             throw new RuntimeException(e.getMessage(), e);
86         } catch (NoSuchMethodException e) {
87             throw new RuntimeException(e.getMessage(), e);
88         }
89     }
90 }