cdc.mf.checks.atts.text.TextUtils Maven / Gradle / Ivy
package cdc.mf.checks.atts.text;
import java.util.ArrayList;
import java.util.List;
import java.util.regex.Pattern;
public final class TextUtils {
private static final Pattern ONE_TOKEN_PATTERN =
Pattern.compile("^\\S+$");
private static final Pattern TOKEN_SEPARATOR_PATTERN =
Pattern.compile("\\s+");
private static final Pattern NO_SPACES_PATTERN =
Pattern.compile("^[\\S]+$");
private TextUtils() {
}
/**
* Returns {@code true} when a {@code text} is one token.
*
* It should contain letters and digits.
*
* @param text The text.
* @return {@code true} if {@code text} is one token.
*/
public static boolean containsOneToken(String text) {
return text != null
&& ONE_TOKEN_PATTERN.matcher(text).matches();
}
/**
* @param text The text.
* @return The (non empty) tokens contained in {@code text}.
*/
public static String[] getTokens(String text) {
final List tokens = new ArrayList<>();
for (final String token : TOKEN_SEPARATOR_PATTERN.split(text)) {
if (!token.isEmpty()) {
tokens.add(token);
}
}
return tokens.toArray(new String[tokens.size()]);
}
/**
* @param text The text.
* @return The number of (non empty) tokens contained in {@code text}.
*/
public static int getTokensCount(String text) {
return getTokens(text).length;
}
/**
* @param text The text.
* @return {@code true} if {@code text} contains spaces.
*/
public static boolean containsSpaces(String text) {
return !NO_SPACES_PATTERN.matcher(text).matches();
}
}