Coverage Report - com.ctrip.framework.apollo.core.utils.StringUtils
 
Classes in this File Line Coverage Branch Coverage Complexity
StringUtils
0%
0/54
0%
0/64
3.438
StringUtils$1
0%
0/2
N/A
3.438
StringUtils$StringFormatter
N/A
N/A
3.438
 
 1  
 package com.ctrip.framework.apollo.core.utils;
 2  
 
 3  
 import java.util.Collection;
 4  
 import java.util.Iterator;
 5  
 
 6  0
 public class StringUtils {
 7  
 
 8  
   public static final String EMPTY = "";
 9  
 
 10  
   /**
 11  
    * <p>
 12  
    * Checks if a String is empty ("") or null.
 13  
    * </p>
 14  
    *
 15  
    * <pre>
 16  
    * StringUtils.isEmpty(null)      = true
 17  
    * StringUtils.isEmpty("")        = true
 18  
    * StringUtils.isEmpty(" ")       = false
 19  
    * StringUtils.isEmpty("bob")     = false
 20  
    * StringUtils.isEmpty("  bob  ") = false
 21  
    * </pre>
 22  
    *
 23  
    * <p>
 24  
    * NOTE: This method changed in Lang version 2.0. It no longer trims the String. That functionality is available in isBlank().
 25  
    * </p>
 26  
    *
 27  
    * @param str the String to check, may be null
 28  
    * @return <code>true</code> if the String is empty or null
 29  
    */
 30  
   public static boolean isEmpty(String str) {
 31  0
     return str == null || str.length() == 0;
 32  
   }
 33  
 
 34  
 
 35  
   public static boolean isContainEmpty(String... args){
 36  0
     if (args == null){
 37  0
       return false;
 38  
     }
 39  
 
 40  0
     for (String arg: args){
 41  0
       if (arg == null || "".equals(arg)){
 42  0
         return true;
 43  
       }
 44  
     }
 45  
 
 46  0
     return false;
 47  
   }
 48  
   /**
 49  
    * <p>
 50  
    * Checks if a String is whitespace, empty ("") or null.
 51  
    * </p>
 52  
    *
 53  
    * <pre>
 54  
    * StringUtils.isBlank(null)      = true
 55  
    * StringUtils.isBlank("")        = true
 56  
    * StringUtils.isBlank(" ")       = true
 57  
    * StringUtils.isBlank("bob")     = false
 58  
    * StringUtils.isBlank("  bob  ") = false
 59  
    * </pre>
 60  
    *
 61  
    * @param str the String to check, may be null
 62  
    * @return <code>true</code> if the String is null, empty or whitespace
 63  
    */
 64  
   public static boolean isBlank(String str) {
 65  
     int strLen;
 66  0
     if (str == null || (strLen = str.length()) == 0) {
 67  0
       return true;
 68  
     }
 69  0
     for (int i = 0; i < strLen; i++) {
 70  0
       if (Character.isWhitespace(str.charAt(i)) == false) {
 71  0
         return false;
 72  
       }
 73  
     }
 74  0
     return true;
 75  
   }
 76  
 
 77  
   /**
 78  
    * <p>
 79  
    * Removes control characters (char &lt;= 32) from both ends of this String returning <code>null</code> if the String is empty
 80  
    * ("") after the trim or if it is <code>null</code>.
 81  
    *
 82  
    * <p>
 83  
    * The String is trimmed using {@link String#trim()}. Trim removes start and end characters &lt;= 32. To strip whitespace use
 84  
    * {@link #stripToNull(String)}.
 85  
    * </p>
 86  
    *
 87  
    * <pre>
 88  
    * StringUtils.trimToNull(null)          = null
 89  
    * StringUtils.trimToNull("")            = null
 90  
    * StringUtils.trimToNull("     ")       = null
 91  
    * StringUtils.trimToNull("abc")         = "abc"
 92  
    * StringUtils.trimToNull("    abc    ") = "abc"
 93  
    * </pre>
 94  
    *
 95  
    * @param str the String to be trimmed, may be null
 96  
    * @return the trimmed String, <code>null</code> if only chars &lt;= 32, empty or null String input
 97  
    * @since 2.0
 98  
    */
 99  
   public static String trimToNull(String str) {
 100  0
     String ts = trim(str);
 101  0
     return isEmpty(ts) ? null : ts;
 102  
   }
 103  
 
 104  
   /**
 105  
    * <p>
 106  
    * Removes control characters (char &lt;= 32) from both ends of this String returning an empty String ("") if the String is empty
 107  
    * ("") after the trim or if it is <code>null</code>.
 108  
    *
 109  
    * <p>
 110  
    * The String is trimmed using {@link String#trim()}. Trim removes start and end characters &lt;= 32. To strip whitespace use
 111  
    * {@link #stripToEmpty(String)}.
 112  
    * </p>
 113  
    *
 114  
    * <pre>
 115  
    * StringUtils.trimToEmpty(null)          = ""
 116  
    * StringUtils.trimToEmpty("")            = ""
 117  
    * StringUtils.trimToEmpty("     ")       = ""
 118  
    * StringUtils.trimToEmpty("abc")         = "abc"
 119  
    * StringUtils.trimToEmpty("    abc    ") = "abc"
 120  
    * </pre>
 121  
    *
 122  
    * @param str the String to be trimmed, may be null
 123  
    * @return the trimmed String, or an empty String if <code>null</code> input
 124  
    * @since 2.0
 125  
    */
 126  
   public static String trimToEmpty(String str) {
 127  0
     return str == null ? EMPTY : str.trim();
 128  
   }
 129  
 
 130  
   /**
 131  
    * <p>
 132  
    * Removes control characters (char &lt;= 32) from both ends of this String, handling <code>null</code> by returning
 133  
    * <code>null</code>.
 134  
    * </p>
 135  
    *
 136  
    * <p>
 137  
    * The String is trimmed using {@link String#trim()}. Trim removes start and end characters &lt;= 32. To strip whitespace use
 138  
    * {@link #strip(String)}.
 139  
    * </p>
 140  
    *
 141  
    * <p>
 142  
    * To trim your choice of characters, use the {@link #strip(String, String)} methods.
 143  
    * </p>
 144  
    *
 145  
    * <pre>
 146  
    * StringUtils.trim(null)          = null
 147  
    * StringUtils.trim("")            = ""
 148  
    * StringUtils.trim("     ")       = ""
 149  
    * StringUtils.trim("abc")         = "abc"
 150  
    * StringUtils.trim("    abc    ") = "abc"
 151  
    * </pre>
 152  
    *
 153  
    * @param str the String to be trimmed, may be null
 154  
    * @return the trimmed string, <code>null</code> if null String input
 155  
    */
 156  
   public static String trim(String str) {
 157  0
     return str == null ? null : str.trim();
 158  
   }
 159  
 
 160  
   /**
 161  
    * <p>
 162  
    * Compares two Strings, returning <code>true</code> if they are equal.
 163  
    * </p>
 164  
    *
 165  
    * <p>
 166  
    * <code>null</code>s are handled without exceptions. Two <code>null</code> references are considered to be equal. The comparison
 167  
    * is case sensitive.
 168  
    * </p>
 169  
    *
 170  
    * <pre>
 171  
    * StringUtils.equals(null, null)   = true
 172  
    * StringUtils.equals(null, "abc")  = false
 173  
    * StringUtils.equals("abc", null)  = false
 174  
    * StringUtils.equals("abc", "abc") = true
 175  
    * StringUtils.equals("abc", "ABC") = false
 176  
    * </pre>
 177  
    *
 178  
    * @param str1 the first String, may be null
 179  
    * @param str2 the second String, may be null
 180  
    * @return <code>true</code> if the Strings are equal, case sensitive, or both <code>null</code>
 181  
    * @see java.lang.String#equals(Object)
 182  
    */
 183  
   public static boolean equals(String str1, String str2) {
 184  0
     return str1 == null ? str2 == null : str1.equals(str2);
 185  
   }
 186  
 
 187  
   /**
 188  
    * <p>
 189  
    * Compares two Strings, returning <code>true</code> if they are equal ignoring the case.
 190  
    * </p>
 191  
    *
 192  
    * <p>
 193  
    * <code>null</code>s are handled without exceptions. Two <code>null</code> references are considered equal. Comparison is case
 194  
    * insensitive.
 195  
    * </p>
 196  
    *
 197  
    * <pre>
 198  
    * StringUtils.equalsIgnoreCase(null, null)   = true
 199  
    * StringUtils.equalsIgnoreCase(null, "abc")  = false
 200  
    * StringUtils.equalsIgnoreCase("abc", null)  = false
 201  
    * StringUtils.equalsIgnoreCase("abc", "abc") = true
 202  
    * StringUtils.equalsIgnoreCase("abc", "ABC") = true
 203  
    * </pre>
 204  
    *
 205  
    * @param str1 the first String, may be null
 206  
    * @param str2 the second String, may be null
 207  
    * @return <code>true</code> if the Strings are equal, case insensitive, or both <code>null</code>
 208  
    * @see java.lang.String#equalsIgnoreCase(String)
 209  
    */
 210  
   public static boolean equalsIgnoreCase(String str1, String str2) {
 211  0
     return str1 == null ? str2 == null : str1.equalsIgnoreCase(str2);
 212  
   }
 213  
 
 214  
   /**
 215  
    * <p>
 216  
    * Check if a String starts with a specified prefix.
 217  
    * </p>
 218  
    *
 219  
    * <p>
 220  
    * <code>null</code>s are handled without exceptions. Two <code>null</code> references are considered to be equal. The comparison
 221  
    * is case sensitive.
 222  
    * </p>
 223  
    *
 224  
    * <pre>
 225  
    * StringUtils.startsWith(null, null)      = true
 226  
    * StringUtils.startsWith(null, "abc")     = false
 227  
    * StringUtils.startsWith("abcdef", null)  = false
 228  
    * StringUtils.startsWith("abcdef", "abc") = true
 229  
    * StringUtils.startsWith("ABCDEF", "abc") = false
 230  
    * </pre>
 231  
    *
 232  
    * @param str    the String to check, may be null
 233  
    * @param prefix the prefix to find, may be null
 234  
    * @return <code>true</code> if the String starts with the prefix, case sensitive, or both <code>null</code>
 235  
    * @see java.lang.String#startsWith(String)
 236  
    * @since 2.4
 237  
    */
 238  
   public static boolean startsWith(String str, String prefix) {
 239  0
     return startsWith(str, prefix, false);
 240  
   }
 241  
 
 242  
   /**
 243  
    * <p>
 244  
    * Check if a String starts with a specified prefix (optionally case insensitive).
 245  
    * </p>
 246  
    *
 247  
    * @param str        the String to check, may be null
 248  
    * @param prefix     the prefix to find, may be null
 249  
    * @param ignoreCase inidicates whether the compare should ignore case (case insensitive) or not.
 250  
    * @return <code>true</code> if the String starts with the prefix or both <code>null</code>
 251  
    * @see java.lang.String#startsWith(String)
 252  
    */
 253  
   private static boolean startsWith(String str, String prefix, boolean ignoreCase) {
 254  0
     if (str == null || prefix == null) {
 255  0
       return str == null && prefix == null;
 256  
     }
 257  0
     if (prefix.length() > str.length()) {
 258  0
       return false;
 259  
     }
 260  0
     return str.regionMatches(ignoreCase, 0, prefix, 0, prefix.length());
 261  
   }
 262  
 
 263  
   /**
 264  
    * <p>
 265  
    * Case insensitive check if a String starts with a specified prefix.
 266  
    * </p>
 267  
    *
 268  
    * <p>
 269  
    * <code>null</code>s are handled without exceptions. Two <code>null</code> references are considered to be equal. The comparison
 270  
    * is case insensitive.
 271  
    * </p>
 272  
    *
 273  
    * <pre>
 274  
    * StringUtils.startsWithIgnoreCase(null, null)      = true
 275  
    * StringUtils.startsWithIgnoreCase(null, "abc")     = false
 276  
    * StringUtils.startsWithIgnoreCase("abcdef", null)  = false
 277  
    * StringUtils.startsWithIgnoreCase("abcdef", "abc") = true
 278  
    * StringUtils.startsWithIgnoreCase("ABCDEF", "abc") = true
 279  
    * </pre>
 280  
    *
 281  
    * @param str    the String to check, may be null
 282  
    * @param prefix the prefix to find, may be null
 283  
    * @return <code>true</code> if the String starts with the prefix, case insensitive, or both <code>null</code>
 284  
    * @see java.lang.String#startsWith(String)
 285  
    * @since 2.4
 286  
    */
 287  
   public static boolean startsWithIgnoreCase(String str, String prefix) {
 288  0
     return startsWith(str, prefix, true);
 289  
   }
 290  
 
 291  
   /**
 292  
    * <p>
 293  
    * Checks if the String contains only unicode digits. A decimal point is not a unicode digit and returns false.
 294  
    * </p>
 295  
    *
 296  
    * <p>
 297  
    * <code>null</code> will return <code>false</code>. An empty String (length()=0) will return <code>true</code>.
 298  
    * </p>
 299  
    *
 300  
    * <pre>
 301  
    * StringUtils.isNumeric(null)   = false
 302  
    * StringUtils.isNumeric("")     = true
 303  
    * StringUtils.isNumeric("  ")   = false
 304  
    * StringUtils.isNumeric("123")  = true
 305  
    * StringUtils.isNumeric("12 3") = false
 306  
    * StringUtils.isNumeric("ab2c") = false
 307  
    * StringUtils.isNumeric("12-3") = false
 308  
    * StringUtils.isNumeric("12.3") = false
 309  
    * </pre>
 310  
    *
 311  
    * @param str the String to check, may be null
 312  
    * @return <code>true</code> if only contains digits, and is non-null
 313  
    */
 314  
   public static boolean isNumeric(String str) {
 315  0
     if (str == null) {
 316  0
       return false;
 317  
     }
 318  0
     int sz = str.length();
 319  0
     for (int i = 0; i < sz; i++) {
 320  0
       if (Character.isDigit(str.charAt(i)) == false) {
 321  0
         return false;
 322  
       }
 323  
     }
 324  0
     return true;
 325  
   }
 326  
 
 327  0
   public static interface StringFormatter<T> {
 328  
     String format(T obj);
 329  
   }
 330  
 
 331  
   public static <T> String join(Collection<T> collection, String separator) {
 332  0
     return join(collection, separator, new StringFormatter<T>() {
 333  
       @Override
 334  
       public String format(T obj) {
 335  0
         return obj.toString();
 336  
       }
 337  
     });
 338  
   }
 339  
 
 340  
   public static <T> String join(Collection<T> collection, String separator,
 341  
                                 StringFormatter<T> formatter) {
 342  0
     Iterator<T> iterator = collection.iterator();
 343  
     // handle null, zero and one elements before building a buffer
 344  0
     if (iterator == null) {
 345  0
       return null;
 346  
     }
 347  0
     if (!iterator.hasNext()) {
 348  0
       return EMPTY;
 349  
     }
 350  0
     T first = iterator.next();
 351  0
     if (!iterator.hasNext()) {
 352  0
       return first == null ? "" : formatter.format(first);
 353  
     }
 354  
 
 355  
     // two or more elements
 356  0
     StringBuilder buf = new StringBuilder(256); // Java default is 16, probably too small
 357  0
     if (first != null) {
 358  0
       buf.append(formatter.format(first));
 359  
     }
 360  
 
 361  0
     while (iterator.hasNext()) {
 362  0
       buf.append(separator);
 363  0
       T obj = iterator.next();
 364  0
       if (obj != null) {
 365  0
         buf.append(formatter.format(obj));
 366  
       }
 367  0
     }
 368  
 
 369  0
     return buf.toString();
 370  
   }
 371  
 }