Coverage Report - org.jtheque.utils.StringUtils
 
Classes in this File Line Coverage Branch Coverage Complexity
StringUtils
61%
36/59
94%
34/36
2.25
 
 1  
 package org.jtheque.utils;
 2  
 
 3  
 /*
 4  
  * This file is part of JTheque.
 5  
  *
 6  
  * JTheque is free software: you can redistribute it and/or modify
 7  
  * it under the terms of the GNU General Public License as published by
 8  
  * the Free Software Foundation, either version 3 of the License.
 9  
  *
 10  
  * JTheque is distributed in the hope that it will be useful,
 11  
  * but WITHOUT ANY WARRANTY; without even the implied warranty of
 12  
  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 13  
  * GNU General Public License for more details.
 14  
  *
 15  
  * You should have received a copy of the GNU General Public License
 16  
  * along with JTheque.  If not, see <http://www.gnu.org/licenses/>.
 17  
  */
 18  
 
 19  
 import java.util.Locale;
 20  
 import java.util.regex.Pattern;
 21  
 
 22  
 /**
 23  
  * This class provides utilities function to manipulate String's object.
 24  
  *
 25  
  * @author Baptiste Wicht
 26  
  */
 27  
 public final class StringUtils {
 28  
     public static final String EMPTY_STRING = "";
 29  
 
 30  2
     private static final Pattern NUMBER_PATTERN = Pattern.compile("[0-9]");
 31  
 
 32  
     /**
 33  
      * Construct a new StringUtils. This constructor is private, all the methods are static.
 34  
      */
 35  
     private StringUtils() {
 36  0
         super();
 37  0
     }
 38  
 
 39  
     /**
 40  
      * Set the first letter of the String to upper. All the others chars will be lower case.
 41  
      *
 42  
      * @param word The string to modify.
 43  
      * @return The modified string.
 44  
      */
 45  
     public static String setFirstLetterOnlyUpper(String word) {
 46  12
         if (isEmpty(word)) {
 47  4
             return word;
 48  
         }
 49  
 
 50  8
         char[] chars = word.toLowerCase(Locale.getDefault()).toCharArray();
 51  
 
 52  8
         chars[0] = Character.toUpperCase(chars[0]);
 53  
 
 54  8
         return String.valueOf(chars);
 55  
     }
 56  
 
 57  
     /**
 58  
      * Set the first letter of the String to upper.
 59  
      *
 60  
      * @param word The string to modify.
 61  
      * @return The modified string.
 62  
      */
 63  
     public static String setFirstLetterUpper(String word) {
 64  24
         if (isEmpty(word)) {
 65  4
             return word;
 66  
         }
 67  
 
 68  20
         char[] chars = word.toCharArray();
 69  
 
 70  20
         chars[0] = Character.toUpperCase(chars[0]);
 71  
 
 72  20
         return String.valueOf(chars);
 73  
     }
 74  
 
 75  
     /**
 76  
      * Test if a string is empty or not.
 77  
      *
 78  
      * @param string The string to test.
 79  
      * @return true if the string is empty else false.
 80  
      */
 81  
     public static boolean isEmpty(CharSequence string) {
 82  300
         return string == null || string.length() == 0;
 83  
     }
 84  
 
 85  
     /**
 86  
      * Return true if the string is not empty.
 87  
      *
 88  
      * @param string The string to test.
 89  
      * @return true if the string is not empty else false.
 90  
      */
 91  
     public static boolean isNotEmpty(CharSequence string) {
 92  42
         return !isEmpty(string);
 93  
     }
 94  
 
 95  
     /**
 96  
      * Remove the last space in the StringBuilder.
 97  
      *
 98  
      * @param builder The builder we must modify
 99  
      */
 100  
     public static void removeLastSpace(StringBuilder builder) {
 101  6
         if (builder.length() > 0) {
 102  4
             builder.replace(builder.length() - 1, builder.length(), EMPTY_STRING);
 103  
         }
 104  6
     }
 105  
 
 106  
     /**
 107  
      * Remove the HTML entities of a string.
 108  
      *
 109  
      * @param htmlString The string to clear.
 110  
      * @return The cleared string.
 111  
      */
 112  
     public static String removeHTMLEntities(String htmlString) {
 113  12
         String cleaned = htmlString.replaceAll("&#39;", "'");
 114  
 
 115  12
         cleaned = cleaned.replaceAll("<br[ ]?[/]?>", "\n");
 116  
 
 117  12
         return cleaned;
 118  
     }
 119  
 
 120  
     /**
 121  
      * Indicate if the string is surrounded by the character c.
 122  
      *
 123  
      * @param str The string to test.
 124  
      * @param c   The char to test.
 125  
      * @return true if the string is surrounded by c else false.
 126  
      */
 127  
     public static boolean isStringSurroundedWith(CharSequence str, char c) {
 128  10
         if (isEmpty(str)) {
 129  2
             return false;
 130  
         }
 131  
 
 132  8
         return str.charAt(0) == c && str.charAt(str.length() - 1) == c;
 133  
     }
 134  
 
 135  
     /**
 136  
      * Remove the first and the test chars.
 137  
      *
 138  
      * @param str The string to edit.
 139  
      * @return The string without his surrounded chars.
 140  
      */
 141  
     public static String removeSurroundedChars(String str) {
 142  12
         if (isEmpty(str)) {
 143  2
             return str;
 144  
         }
 145  
 
 146  10
         return str.substring(1, str.length() - 1);
 147  
     }
 148  
 
 149  
     /**
 150  
      * Remove the unicode characters from a string.
 151  
      *
 152  
      * @param value The string to clean.
 153  
      * @return The cleaned string.
 154  
      */
 155  
     @SuppressWarnings({"DynamicRegexReplaceableByCompiledPattern"})
 156  
     public static String removeUnicode(String value) {
 157  0
         String refreshed = value.replace("\\\\u00e0", "\u00e0");
 158  0
         refreshed = refreshed.replace("\\\\u00e2", "\u00e2");
 159  0
         refreshed = refreshed.replace("\\\\u00e4", "\u00e4");
 160  0
         refreshed = refreshed.replace("\\\\u00e7", "\u00e7");
 161  0
         refreshed = refreshed.replace("\\\\u00e8", "\u00e8");
 162  0
         refreshed = refreshed.replace("\\\\u00e9", "\u00e9");
 163  0
         refreshed = refreshed.replace("\\\\u00ea", "\u00ea");
 164  0
         refreshed = refreshed.replace("\\\\u00eb", "\u00eb");
 165  0
         refreshed = refreshed.replace("\\\\u00ee", "\u00ee");
 166  0
         refreshed = refreshed.replace("\\\\u00ef", "\u00ef");
 167  0
         refreshed = refreshed.replace("\\\\u00f4", "\u00f4");
 168  0
         refreshed = refreshed.replace("\\\\u00f6", "\u00f6");
 169  0
         refreshed = refreshed.replace("\\\\u00f9", "\u00f9");
 170  0
         refreshed = refreshed.replace("\\\\u00fb", "\u00fb");
 171  0
         refreshed = refreshed.replace("\\\\u00fc", "\u00fc");
 172  
 
 173  0
         return refreshed;
 174  
     }
 175  
 
 176  
     /**
 177  
      * Test if the String value ends with one of the specified ends.
 178  
      *
 179  
      * @param value The String value to test.
 180  
      * @param ends  The possible ends.
 181  
      * @return true if the String value ends with one of the specified ends.
 182  
      */
 183  
     public static boolean endsWithOneOf(String value, String... ends) {
 184  10
         for (String end : ends) {
 185  8
             if (value.endsWith(end)) {
 186  2
                 return true;
 187  
             }
 188  
         }
 189  
 
 190  2
         return false;
 191  
     }
 192  
 
 193  
     /**
 194  
      * Test if an array of String is empty. An array of strings is empty if one of these rule matches :
 195  
      * <ul>
 196  
      * <li>The array is empty</li>
 197  
      * <li>The array contains only null and empty String<li>
 198  
      * </ul>
 199  
      *
 200  
      * @param strings The array to test.
 201  
      * @return true if the array is empty else false.
 202  
      */
 203  
     public static boolean isEmpty(String[] strings) {
 204  24
         if (strings != null && strings.length > 0) {
 205  44
             for (String s : strings) {
 206  36
                 if (isNotEmpty(s)) {
 207  8
                     return false;
 208  
                 }
 209  
             }
 210  
         }
 211  
 
 212  16
         return true;
 213  
     }
 214  
 
 215  
     /**
 216  
      * Test if an array of string is not empty.
 217  
      *
 218  
      * @param strings The array of Strings to test.
 219  
      * @return true if the array is not empty else false.
 220  
      * @see StringUtils#isEmpty(String[] strings);
 221  
      */
 222  
     public static boolean isNotEmpty(String[] strings) {
 223  12
         return !isEmpty(strings);
 224  
     }
 225  
 
 226  
     /**
 227  
      * Remove all numbers of the given string.
 228  
      *
 229  
      * @param value The string.
 230  
      * @return The given string without any numbers.
 231  
      */
 232  
     public static String removeNumbers(CharSequence value) {
 233  4
         return NUMBER_PATTERN.matcher(value).replaceAll("");
 234  
     }
 235  
 
 236  
     public static String delete(String string, Object... toDeletes){
 237  0
         String cleared = string;
 238  
 
 239  0
         for(Object toDelete : toDeletes){
 240  0
             cleared = delete(cleared, toDelete.toString());
 241  
         }
 242  
 
 243  0
         return cleared;
 244  
     }
 245  
 
 246  
     public static String delete(String string, CharSequence toDelete){
 247  0
         return string.replace(toDelete, "");
 248  
     }
 249  
 }