RegexValuePredicate implements ValuePredicate<String> { @Override public boolean apply(@Nullable String input) { if(input == null){ return false; } if(policy == MatchPolicy.PARTIAL){ return pattern.matcher(input).find(); } if(policy == MatchPolicy.FULL){ return pattern.matcher(input).matches(); } throw new UnsupportedOperationException(String.format("the match policy %s is not supported", policy)); } RegexValuePredicate(String regex, MatchPolicy policy); @Override boolean apply(@Nullable String input); String getPattern(); MatchPolicy getMatchPolicy(); @Override String toString(); @Override int hashCode(); @Override boolean equals(Object obj);  }
@Test public void testNormalMatch() throws Exception { Object[][] inputs = { {"a123cd", "a[0-9]+cd", MatchPolicy.FULL, true}, {"", "", MatchPolicy.FULL, true}, {"", "^$", MatchPolicy.FULL, true}, {"32abde23", "[a-z]+", MatchPolicy.FULL, false}, {"32abde23", "[a-z]+", MatchPolicy.PARTIAL, true}, {null, ".*", MatchPolicy.PARTIAL, false}, {null, ".*", MatchPolicy.FULL, false}, {null, "", MatchPolicy.PARTIAL, false} }; for(Object[] input : inputs){ String value = (String)input[0]; String regex = (String)input[1]; MatchPolicy policy = (MatchPolicy)input[2]; boolean expected = (Boolean) input[3]; RegexValuePredicate pred = new RegexValuePredicate(regex, policy); assertEquals( String.format("Given regex = %s, isPartial = %s, and input = %s", regex, policy, value), expected, pred.apply(value)); } }