public static < T extends ContentMeta > ResourceSelector < T > regexMetadataResourceSelector ( final Map < String , String > required , final boolean requireAll ) { return new ResourceSelector < T > ( ) { Map < String , Pattern > patternMap = new HashMap < String , Pattern > ( ) ; private Pattern forString ( String regex ) { if ( null == patternMap . get ( regex ) ) { Pattern compile = null ; try { compile = Pattern . compile ( regex ) ; } catch ( PatternSyntaxException ignored ) { return null ; } patternMap . put ( regex , compile ) ; } return patternMap . get ( regex ) ; } @ Override public boolean matchesContent ( T content ) { for ( String key : required . keySet ( ) ) { Pattern pattern = forString ( required . get ( key ) ) ; String test = content . getMeta ( ) . get ( key ) ; if ( null != test && null != pattern && pattern . matcher ( test ) . matches ( ) ) { if ( ! requireAll ) { return true ; } } else if ( requireAll ) { return false ; } } return requireAll ; } } ; }
A resource selector which requires metadata values to match regexes