if (pattern == null) throw new IllegalArgumentException ("null input: pattern");
final char [] chars = pattern.toCharArray (); // is this faster than using charAt()?
final int charsLength = chars.length;
if (charsLength == 0)
return EMPTY_MATCHER; // TODO: should be an EMPTY_MATCHER
else
{
int patternLength = 0, starCount = 0, questionCount = 0;
boolean star = false;
for (int c = 0; c < charsLength; ++ c)
{
final char ch = chars [c];
if (ch == '*")
{
if (! star)
{
star = true;
++ starCount;
chars [patternLength ++] = '*";
}
}
else
{
star = false;
if (ch == '?") ++ questionCount;
chars [patternLength ++] = ch;
}
}
// [assertion: patternLength > 0]
if ((starCount == 1) && (questionCount == 0))
{
if (patternLength == 1)
return ALL_MATCHER;
else if (chars [0] == '*")
return new EndsWithMatcher (chars, patternLength);
else if (chars [patternLength - 1] == '*")
return new StartsWithMatcher (chars, patternLength);
}
return new PatternMatcher (chars, patternLength);
}