@ -20,6 +20,7 @@ import java.util.HashMap;
import java.util.LinkedHashMap ;
import java.util.Map ;
import java.util.regex.Pattern ;
import java.util.regex.PatternSyntaxException ;
/ * *
* Utility class that can be used to filter source data using a name regular expression .
@ -31,6 +32,7 @@ import java.util.regex.Pattern;
* @author Phillip Webb
* @author Sergei Egorov
* @author Andy Wilkinson
* @author Dylian Bego
* @since 1.3 .0
* /
abstract class NamePatternFilter < T > {
@ -44,13 +46,13 @@ abstract class NamePatternFilter<T> {
}
public Map < String , Object > getResults ( String name ) {
if ( ! isRegex ( name ) ) {
Pattern pattern = getRegexPattern ( name ) ;
if ( pattern = = null ) { // this is not a regex
Object value = getValue ( this . source , name ) ;
Map < String , Object > result = new HashMap < String , Object > ( ) ;
result . put ( name , value ) ;
return result ;
}
Pattern pattern = Pattern . compile ( name ) ;
ResultCollectingNameCallback resultCollector = new ResultCollectingNameCallback (
pattern ) ;
getNames ( this . source , resultCollector ) ;
@ -58,13 +60,18 @@ abstract class NamePatternFilter<T> {
}
private boolean isRegex ( String name ) {
private Pattern getRegexPattern ( String name ) {
for ( String part : REGEX_PARTS ) {
if ( name . contains ( part ) ) {
return true ;
try {
return Pattern . compile ( name ) ;
}
catch ( PatternSyntaxException e ) {
return null ;
}
return false ;
}
}
return null ;
}
protected abstract void getNames ( T source , NameCallback callback ) ;