Improving the RegexHelper method (simply preventing nullpointer exception).

Add some javadoc, always nice to have.

git-svn-id: https://pmd.svn.sourceforge.net/svnroot/pmd/trunk@5781 51baf565-9d33-0410-a72c-fc3788e3496d
This commit is contained in:
Romain Pelisse
2008-02-15 19:10:51 +00:00
parent 1ef60c2cd1
commit c17da43741

View File

@ -25,22 +25,36 @@ public class RegexHelper {
* @return
*/
public static List<Pattern> compilePatternFromList(List<String> list) {
List<Pattern> patterns = new ArrayList<Pattern>(list.size());
for (String stringPattern : list) {
if ( stringPattern != null && stringPattern.length() > 0 ) {
patterns.add(Pattern.compile(stringPattern));
} else {
throw new IllegalArgumentException("The following pattern is not valid:" + stringPattern);
List<Pattern> patterns;
if (list != null && list.size() > 0) {
patterns = new ArrayList<Pattern>(list.size());
for (String stringPattern : list) {
if ( stringPattern != null && ! "".equals(stringPattern) ) {
patterns.add(Pattern.compile(stringPattern));
}
}
}
else
patterns = new ArrayList<Pattern>(0);
return patterns;
}
public static boolean isMatch(Pattern pattern,String image) {
Matcher matcher = pattern.matcher(image);
if (matcher.find()) {
return true;
}
/**
* <p>Simple commidity method (also designed to increase readability of source code,
* and to decrease import in the calling class).</p>
* <p>Provide a pattern and a subject, it'll do the proper matching.</p>
*
* @param pattern, a compiled regex pattern.
* @param subject, a String to match
* @return
*/
public static boolean isMatch(Pattern pattern,String subject) {
if ( subject != null && "".equals(subject) ) {
Matcher matcher = pattern.matcher(subject);
if (matcher.find()) {
return true;
}
}
return false;
}