Advertisement
Genesis2001

Untitled

Apr 18th, 2012
38
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 1.03 KB | None | 0 0
  1. package org.unifiedtech.guardian;
  2.  
  3. import java.util.HashMap;
  4. import java.util.regex.Pattern;
  5.  
  6. public class StringUtils {
  7.    
  8.     private static HashMap<String, Pattern> regexCache = new HashMap<String, Pattern>();
  9.    
  10.     /**
  11.      * Returns whether the specified String is null or empty.
  12.      *
  13.      * @param str
  14.      * @return
  15.      */
  16.     public static boolean isNullOrEmpty(String str) {
  17.         return (str == null || str.trim().length() == 0);
  18.     }
  19.    
  20.     /**
  21.      * Checks whether the specified string matches the specified regex pattern.
  22.      * Utilizes a caching mechanism to cache patterns that are used repeatedly in the same program, avoiding useless cpu cycles.
  23.      *
  24.      * @param string
  25.      * @param pattern
  26.      * @return
  27.      */
  28.     public static boolean matches(String string, String pattern) {
  29.         synchronized(regexCache) {
  30.             Pattern p;
  31.            
  32.             if (regexCache.containsKey(pattern)) {
  33.                 p = regexCache.get(pattern);
  34.             } else {
  35.                 p = Pattern.compile(pattern);
  36.                 regexCache.put(pattern, p);
  37.             }
  38.            
  39.             return p.matcher(string).matches();
  40.         }
  41.     }
  42. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement