Patterns { public static Pattern repeat(int n, CharPredicate predicate) { Checks.checkNonNegative(n, "n < 0"); return new RepeatCharPredicatePattern(n, predicate); } 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 testRepeat() { assertEquals(3, Patterns.repeat(3, CharPredicates.ALWAYS).match("abc", 0, 3)); assertEquals(2, Patterns.repeat(2, CharPredicates.ALWAYS).match("abc", 0, 3)); assertEquals(MISMATCH, Patterns.repeat(3, CharPredicates.NEVER).match("abc", 0, 3)); assertEquals(MISMATCH, Patterns.repeat(3, CharPredicates.ALWAYS).match("abc", 0, 2)); }
@Test public void testRepeat_negativeNumberThrows() { try { Patterns.repeat(-1, CharPredicates.ALWAYS); fail(); } catch (IllegalArgumentException e) { assertEquals("n < 0", e.getMessage()); } }