Patterns { public static Pattern and(final Pattern... patterns) { return new Pattern() { @Override public int match(CharSequence src, int begin, int end) { int ret = 0; for (Pattern pattern : patterns) { int l = pattern.match(src, begin, end); if (l == MISMATCH) return MISMATCH; if (l > ret) ret = l; } return ret; } @Override public String toString() { StringBuilder sb = new StringBuilder(); sb.append('('); for (Pattern pattern : patterns) { sb.append(pattern).append(" & "); } if (sb.length() > 1) { sb.delete(sb.length() - 3, sb.length()); } return sb.append(')').toString(); } }; } private  Patterns(); static Pattern hasAtLeast(final int n); static Pattern hasExact(final int n); static Pattern isChar(char c); static Pattern range(char c1, char c2); static Pattern among(String chars); static Pattern isChar(final CharPredicate predicate); static Pattern lineComment(String begin); static Pattern string(final String string); static Pattern stringCaseInsensitive(final String string); static Pattern notString(final String string); static Pattern notStringCaseInsensitive(final String string); static Pattern not(Pattern pattern); static Pattern and(final Pattern... patterns); static Pattern or(Pattern... patterns); static Pattern sequence(Pattern... patterns); static Pattern repeat(int n, CharPredicate predicate); @Deprecated static Pattern many(int min, CharPredicate predicate); static Pattern atLeast(final int min, final CharPredicate predicate); static Pattern many(final CharPredicate predicate); @Deprecated static Pattern some(int min, int max, CharPredicate predicate); static Pattern times(final int min, final int max, final CharPredicate predicate); @Deprecated static Pattern some(final int max, final CharPredicate predicate); static Pattern atMost(final int max, final CharPredicate predicate); static Pattern longer(Pattern p1, Pattern p2); static Pattern longest(final Pattern... patterns); static Pattern shorter(Pattern p1, Pattern p2); static Pattern shortest(final Pattern... patterns); static Pattern many1(CharPredicate predicate); static Pattern regex(final java.util.regex.Pattern p); static Pattern regex(String s); static final Pattern NEVER; static final Pattern ALWAYS; static final Pattern ANY_CHAR; static final Pattern EOF; static final Pattern ESCAPED; static final Pattern INTEGER; static final Pattern STRICT_DECIMAL; static final Pattern FRACTION; static final Pattern DECIMAL; static final Pattern WORD; static final Pattern OCT_INTEGER; static final Pattern DEC_INTEGER; static final Pattern HEX_INTEGER; static final Pattern SCIENTIFIC_NOTATION; static final Pattern REGEXP_PATTERN; static final Pattern REGEXP_MODIFIERS; }
@Test public void testAnd() { assertEquals(2, Patterns.and(Patterns.hasAtLeast(1), Patterns.hasAtLeast(2)).match("abc", 0, 3)); assertEquals(2, Patterns.and(Patterns.hasAtLeast(2), Patterns.hasAtLeast(1)).match("abc", 0, 3)); assertEquals(MISMATCH, Patterns.and(Patterns.hasAtLeast(1), Patterns.NEVER).match("abc", 0, 3)); assertEquals(MISMATCH, Patterns.and(Patterns.NEVER, Patterns.hasAtLeast(1), Patterns.ALWAYS).match("abc", 0, 3)); }