1 /* 2 * aTunes 3 * Copyright (C) Alex Aranda, Sylvain Gaudard and contributors 4 * 5 * See http://www.atunes.org/wiki/index.php?title=Contributing for information about contributors 6 * 7 * http://www.atunes.org 8 * http://sourceforge.net/projects/atunes 9 * 10 * This program is free software; you can redistribute it and/or 11 * modify it under the terms of the GNU General Public License 12 * as published by the Free Software Foundation; either version 2 13 * of the License, or (at your option) any later version. 14 * 15 * This program is distributed in the hope that it will be useful, 16 * but WITHOUT ANY WARRANTY; without even the implied warranty of 17 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 18 * GNU General Public License for more details. 19 */ 20 21 package net.sourceforge.atunes.utils; 22 23 import java.text.DecimalFormat; 24 import java.util.ArrayList; 25 import java.util.List; 26 27 import org.apache.commons.lang.StringEscapeUtils; 28 29 /** 30 * Utility methods for Strings. 31 */ 32 33 public final class StringUtils { 34 StringUtils()35 private StringUtils() { 36 } 37 38 /** 39 * Given an amount of bytes, return a string representation in Bytes, 40 * Kilobytes, Megabytes or Gigabytes Examples: Given 1024 bytes -> "1KB" 41 * Given 1536 bytes -> 1.5KB" 42 * 43 * @param size 44 * amount of bytes 45 * 46 * @return String representation in Bytes, Kilobytes, Megabytes or Gigabytes 47 */ fromByteToMegaOrGiga(final long size)48 public static String fromByteToMegaOrGiga(final long size) { 49 if (size < FileUtils.KILOBYTE) { 50 return StringUtils.getString(String.valueOf(size), " Bytes"); 51 } else if (size < FileUtils.MEGABYTE) { 52 return StringUtils.getString( 53 toString((double) size / FileUtils.KILOBYTE, 2), " KB"); 54 } else if (size < FileUtils.GIGABYTE) { 55 return StringUtils.getString( 56 toString((double) size / FileUtils.MEGABYTE, 2), " MB"); 57 } else { 58 return StringUtils.getString( 59 toString((double) size / FileUtils.GIGABYTE, 2), " GB"); 60 } 61 } 62 63 /** 64 * <p> 65 * Returns a List containing strings of the array. Text between " chars, are 66 * returned on a string 67 * </p> 68 * <p> 69 * Example: {"This", "is\"", "a ", "test\""} will return: "This" "is" "a 70 * test" 71 * </p> 72 * 73 * @param str 74 * String array 75 * 76 * @return List containing strings of the array 77 */ fromStringArrayToList(final String... str)78 public static List<String> fromStringArrayToList(final String... str) { 79 List<String> result = new ArrayList<String>(); 80 boolean openedQuotes = false; 81 String auxStr = ""; 82 for (String s : str) { 83 if (s.startsWith("\"") && s.endsWith("\"")) { 84 result.add(s.replaceAll("\"", "")); 85 } else if (s.endsWith("\"")) { 86 openedQuotes = false; 87 auxStr = StringUtils.getString(auxStr, " ", 88 s.replaceAll("\"", "")); 89 result.add(auxStr); 90 } else if (s.startsWith("\"")) { 91 openedQuotes = true; 92 auxStr = s.replaceFirst("\"", ""); 93 } else if (openedQuotes) { 94 auxStr = StringUtils.getString(auxStr, " ", s); 95 } else { 96 result.add(s); 97 } 98 } 99 return result; 100 } 101 102 /** 103 * Returns a string with concatenation of argument array. 104 * 105 * @param strings 106 * strings 107 * 108 * @return concatenation of argument array 109 */ getString(final Object... strings)110 public static String getString(final Object... strings) { 111 StringBuilder objStringBuilder = new StringBuilder(); 112 113 for (Object element : strings) { 114 objStringBuilder.append(element); 115 } 116 117 return objStringBuilder.toString(); 118 } 119 120 /** 121 * Returns a double value as a string with a given number of decimal digits. 122 * 123 * @param value 124 * double value 125 * @param numberOfDecimals 126 * number of decimal digits 127 * 128 * @return string with a given number of decimal digits 129 */ toString(final double value, final int numberOfDecimals)130 public static String toString(final double value, final int numberOfDecimals) { 131 DecimalFormat df = new DecimalFormat("#.#"); 132 df.setMinimumFractionDigits(numberOfDecimals); 133 return df.format(value); 134 } 135 136 /** 137 * Converts the first character of a String to uppercase. 138 * 139 * @param s 140 * A String that should be converted 141 * 142 * @return The String with the first character converted to uppercase 143 */ convertFirstCharacterToUppercase(final String s)144 public static String convertFirstCharacterToUppercase(final String s) { 145 if (s != null && !s.isEmpty()) { 146 String result; 147 result = String.valueOf(Character.toUpperCase(s.charAt(0))); 148 if (s.length() > 1) { 149 result += s.substring(1); 150 } 151 return result; 152 } 153 return s; 154 } 155 156 /** 157 * Checks if a String is empty. 158 * 159 * @param s 160 * a String 161 * 162 * @return If the specified String is empty 163 */ isEmpty(final String s)164 public static boolean isEmpty(final String s) { 165 return s == null || s.trim().isEmpty(); 166 } 167 168 /** 169 * Unescapes a HTML string 170 * 171 * @param source 172 * the HTML string 173 * @param start 174 * the start position 175 * @return the unescaped HTML string 176 */ unescapeHTML(final String source, final int start)177 public static String unescapeHTML(final String source, final int start) { 178 return StringEscapeUtils.unescapeHtml(source.substring(start)); 179 } 180 181 /** 182 * Returns int represented by string argument or 0 183 * 184 * @param number 185 * @return 186 */ getNumberOrZero(final String number)187 public static int getNumberOrZero(final String number) { 188 if (isEmpty(number)) { 189 return 0; 190 } 191 try { 192 return Integer.parseInt(number); 193 } catch (NumberFormatException e) { 194 return 0; 195 } 196 } 197 198 /** 199 * Returns list of text between specified chars. Both chars are included in 200 * result elements. Returns empty list if chars are not found in string in 201 * given order For example given string "ab cd (ef) gh (ij)" and chars '(' 202 * and ')' will return a list with two strings: "(ef)" and "(ij)" 203 * 204 * @param string 205 * @param beginChar 206 * @param endChar 207 * @return 208 */ getTextBetweenChars(final String string, final char beginChar, final char endChar)209 public static List<String> getTextBetweenChars(final String string, 210 final char beginChar, final char endChar) { 211 List<String> result = new ArrayList<String>(); 212 213 if (string == null || string.indexOf(beginChar) == -1 214 || string.indexOf(endChar) == -1) { 215 return result; 216 } 217 218 String auxStr = string; 219 int beginIndex = auxStr.indexOf(beginChar); 220 int endIndex = auxStr.indexOf(endChar); 221 while (beginIndex != -1 && endIndex != -1) { 222 if (beginIndex < endIndex) { 223 result.add(auxStr.substring(beginIndex, endIndex + 1)); 224 } 225 auxStr = auxStr.substring(endIndex + 1); 226 beginIndex = auxStr.indexOf(beginChar); 227 endIndex = auxStr.indexOf(endChar); 228 } 229 230 return result; 231 } 232 233 /** 234 * Returns true if first string is equals to any of others, false if null 235 * 236 * @param string 237 * @param strings 238 * @return 239 */ equalsToStrings(final String string, final String... strings)240 public static boolean equalsToStrings(final String string, 241 final String... strings) { 242 if (string == null) { 243 return false; 244 } 245 for (String s : strings) { 246 if (string.equals(s)) { 247 return true; 248 } 249 } 250 return false; 251 } 252 253 /** 254 * Returns common suffix for all strings (ignoring case) 255 * 256 * @param strings 257 * @return 258 */ getCommonSuffix(final String... strings)259 public static String getCommonSuffix(final String... strings) { 260 if (strings.length == 0) { 261 return null; 262 } 263 if (strings.length == 1) { 264 return strings[0]; 265 } 266 267 String[] reverse = new String[strings.length]; 268 int i = 0; 269 for (String str : strings) { 270 reverse[i++] = org.apache.commons.lang.StringUtils.reverse(str); 271 } 272 273 int indexOfDifference = org.apache.commons.lang.StringUtils 274 .indexOfDifference(reverse); 275 276 return strings[0].substring(strings[0].length() - indexOfDifference); 277 } 278 279 /** 280 * Returns common prefix for all strings (ignoring case) 281 * 282 * @param strings 283 * @return 284 */ getCommonPrefix(final String... strings)285 public static String getCommonPrefix(final String... strings) { 286 if (strings.length == 0) { 287 return null; 288 } 289 if (strings.length == 1) { 290 return strings[0]; 291 } 292 293 return org.apache.commons.lang.StringUtils.getCommonPrefix(strings); 294 } 295 296 } 297