Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package org.unifiedtech.guardian;
- import java.util.HashMap;
- import java.util.regex.Pattern;
- public class StringUtils {
- private static HashMap<String, Pattern> regexCache = new HashMap<String, Pattern>();
- /**
- * Returns whether the specified String is null or empty.
- *
- * @param str
- * @return
- */
- public static boolean isNullOrEmpty(String str) {
- return (str == null || str.trim().length() == 0);
- }
- /**
- * Checks whether the specified string matches the specified regex pattern.
- * Utilizes a caching mechanism to cache patterns that are used repeatedly in the same program, avoiding useless cpu cycles.
- *
- * @param string
- * @param pattern
- * @return
- */
- public static boolean matches(String string, String pattern) {
- synchronized(regexCache) {
- Pattern p;
- if (regexCache.containsKey(pattern)) {
- p = regexCache.get(pattern);
- } else {
- p = Pattern.compile(pattern);
- regexCache.put(pattern, p);
- }
- return p.matcher(string).matches();
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement