WordTokenizer implements Tokenizer { public boolean isValidMention(final @NonNull Spanned text, final int start, final int end) { CharSequence token = text.subSequence(start, end); if (TextUtils.isEmpty(token)) { return false; } final int threshold = mConfig.THRESHOLD; boolean multipleWords = containsWordBreakingChar(token); boolean containsExplicitChar = containsExplicitChar(token); if (!multipleWords && containsExplicitChar) { if (!isExplicitChar(token.charAt(0))) { return false; } if (!hasWordBreakingCharBeforeExplicitChar(text, end)) { return false; } if (token.length() == 1) { return true; } return Character.isLetterOrDigit(token.charAt(1)); } else if (token.length() >= threshold) { if (!multipleWords) { return onlyLettersOrDigits(token, threshold, 0); } else if (containsExplicitChar) { return hasWordBreakingCharBeforeExplicitChar(text, end) && isExplicitChar(token.charAt(0)) && Character.isLetterOrDigit(token.charAt(1)); } else { boolean firstCharactersValid = onlyLettersOrDigits(token, threshold, 0); boolean lastCharactersValid = onlyLettersOrDigits(token, threshold, token.length() - threshold); return firstCharactersValid || lastCharactersValid; } } return false; } WordTokenizer();  WordTokenizer(final @NonNull WordTokenizerConfig config); @Override int findTokenStart(final @NonNull Spanned text, final int cursor); @Override int findTokenEnd(final @NonNull Spanned text, final int cursor); boolean isValidMention(final @NonNull Spanned text, final int start, final int end); @Override @NonNull Spanned terminateToken(final @NonNull Spanned text); boolean isExplicitChar(final char c); boolean isWordBreakingChar(final char c); boolean isExplicit(final @NonNull CharSequence text, final int cursor); char getExplicitChar(final @NonNull CharSequence text, final int cursor); boolean containsExplicitChar(final @NonNull CharSequence input); boolean containsWordBreakingChar(final @NonNull CharSequence input); boolean onlyLettersOrDigits(final @NonNull CharSequence input, final int numCharsToCheck, final int start);  }
@Test public void testIsValidMention() { isValidMentionHelper("", false); isValidMentionHelper("ab ", false); isValidMentionHelper(" ", false); isValidMentionHelper("a b", false); isValidMentionHelper("a%b", false); isValidMentionHelper("@", true); isValidMentionHelper("@%b", false); isValidMentionHelper("mic", true); isValidMentionHelper("mic", true); isValidMentionHelper("@m", true); isValidMentionHelper("@_", false); isValidMentionHelper("@ ", false); isValidMentionHelper("@,", false); isValidMentionHelper("@%", false); isValidMentionHelper("Jon@", false); }