一、前言
关于hotswap的hotswap-agent-1.1.0-SNAPSHOT.jar源码包中org.hotswap.agent.util.spring.util.StringUtils字符串工具类,对字符串内容长度hasLength、空判断isEmpty、特殊字符串判断containsWhitespace、去空格trimAllWhitespace、尾部忽略包含endsWithIgnoreCase、指定内容替换删除及根据特殊字符拆分数组split等处理。
二、源码说明
package org.hotswap.agent.util.spring.util;@b@@b@import java.util.ArrayList;@b@import java.util.Arrays;@b@import java.util.Collection;@b@import java.util.Collections;@b@import java.util.Enumeration;@b@import java.util.Iterator;@b@import java.util.LinkedHashSet;@b@import java.util.LinkedList;@b@import java.util.List;@b@import java.util.Locale;@b@import java.util.Properties;@b@import java.util.Set;@b@import java.util.StringTokenizer;@b@import java.util.TimeZone;@b@@b@public abstract class StringUtils@b@{@b@ private static final String FOLDER_SEPARATOR = "/";@b@ private static final String WINDOWS_FOLDER_SEPARATOR = "\\";@b@ private static final String TOP_PATH = "..";@b@ private static final String CURRENT_PATH = ".";@b@ private static final char EXTENSION_SEPARATOR = 46;@b@@b@ public static boolean isEmpty(Object str)@b@ {@b@ return ((str == null) || ("".equals(str)));@b@ }@b@@b@ public static boolean hasLength(CharSequence str)@b@ {@b@ return ((str != null) && (str.length() > 0));@b@ }@b@@b@ public static boolean hasLength(String str)@b@ {@b@ return hasLength(str);@b@ }@b@@b@ public static boolean hasText(CharSequence str)@b@ {@b@ if (!(hasLength(str)))@b@ return false;@b@@b@ int strLen = str.length();@b@ for (int i = 0; i < strLen; ++i)@b@ if (!(Character.isWhitespace(str.charAt(i))))@b@ return true;@b@@b@@b@ return false;@b@ }@b@@b@ public static boolean hasText(String str)@b@ {@b@ return hasText(str);@b@ }@b@@b@ public static boolean containsWhitespace(CharSequence str)@b@ {@b@ if (!(hasLength(str)))@b@ return false;@b@@b@ int strLen = str.length();@b@ for (int i = 0; i < strLen; ++i)@b@ if (Character.isWhitespace(str.charAt(i)))@b@ return true;@b@@b@@b@ return false;@b@ }@b@@b@ public static boolean containsWhitespace(String str)@b@ {@b@ return containsWhitespace(str);@b@ }@b@@b@ public static String trimWhitespace(String str)@b@ {@b@ if (!(hasLength(str)))@b@ return str;@b@@b@ StringBuilder sb = new StringBuilder(str);@b@ while ((sb.length() > 0) && (Character.isWhitespace(sb.charAt(0))))@b@ sb.deleteCharAt(0);@b@@b@ while ((sb.length() > 0) && (Character.isWhitespace(sb.charAt(sb.length() - 1))))@b@ sb.deleteCharAt(sb.length() - 1);@b@@b@ return sb.toString();@b@ }@b@@b@ public static String trimAllWhitespace(String str)@b@ {@b@ if (!(hasLength(str)))@b@ return str;@b@@b@ int len = str.length();@b@ StringBuilder sb = new StringBuilder(str.length());@b@ for (int i = 0; i < len; ++i) {@b@ char c = str.charAt(i);@b@ if (!(Character.isWhitespace(c)))@b@ sb.append(c);@b@ }@b@@b@ return sb.toString();@b@ }@b@@b@ public static String trimLeadingWhitespace(String str)@b@ {@b@ if (!(hasLength(str)))@b@ return str;@b@@b@ StringBuilder sb = new StringBuilder(str);@b@ while ((sb.length() > 0) && (Character.isWhitespace(sb.charAt(0))))@b@ sb.deleteCharAt(0);@b@@b@ return sb.toString();@b@ }@b@@b@ public static String trimTrailingWhitespace(String str)@b@ {@b@ if (!(hasLength(str)))@b@ return str;@b@@b@ StringBuilder sb = new StringBuilder(str);@b@ while ((sb.length() > 0) && (Character.isWhitespace(sb.charAt(sb.length() - 1))))@b@ sb.deleteCharAt(sb.length() - 1);@b@@b@ return sb.toString();@b@ }@b@@b@ public static String trimLeadingCharacter(String str, char leadingCharacter)@b@ {@b@ if (!(hasLength(str)))@b@ return str;@b@@b@ StringBuilder sb = new StringBuilder(str);@b@ while ((sb.length() > 0) && (sb.charAt(0) == leadingCharacter))@b@ sb.deleteCharAt(0);@b@@b@ return sb.toString();@b@ }@b@@b@ public static String trimTrailingCharacter(String str, char trailingCharacter)@b@ {@b@ if (!(hasLength(str)))@b@ return str;@b@@b@ StringBuilder sb = new StringBuilder(str);@b@ while ((sb.length() > 0) && (sb.charAt(sb.length() - 1) == trailingCharacter))@b@ sb.deleteCharAt(sb.length() - 1);@b@@b@ return sb.toString();@b@ }@b@@b@ public static boolean startsWithIgnoreCase(String str, String prefix)@b@ {@b@ if ((str == null) || (prefix == null))@b@ return false;@b@@b@ if (str.startsWith(prefix))@b@ return true;@b@@b@ if (str.length() < prefix.length())@b@ return false;@b@@b@ String lcStr = str.substring(0, prefix.length()).toLowerCase();@b@ String lcPrefix = prefix.toLowerCase();@b@ return lcStr.equals(lcPrefix);@b@ }@b@@b@ public static boolean endsWithIgnoreCase(String str, String suffix)@b@ {@b@ if ((str == null) || (suffix == null))@b@ return false;@b@@b@ if (str.endsWith(suffix))@b@ return true;@b@@b@ if (str.length() < suffix.length()) {@b@ return false;@b@ }@b@@b@ String lcStr = str.substring(str.length() - suffix.length()).toLowerCase();@b@ String lcSuffix = suffix.toLowerCase();@b@ return lcStr.equals(lcSuffix);@b@ }@b@@b@ public static boolean substringMatch(CharSequence str, int index, CharSequence substring)@b@ {@b@ for (int j = 0; j < substring.length(); ++j) {@b@ int i = index + j;@b@ if ((i >= str.length()) || (str.charAt(i) != substring.charAt(j)))@b@ return false;@b@ }@b@@b@ return true;@b@ }@b@@b@ public static int countOccurrencesOf(String str, String sub)@b@ {@b@ if ((str == null) || (sub == null) || (str.length() == 0) || (sub.length() == 0))@b@ return 0;@b@@b@ int count = 0;@b@ int pos = 0;@b@@b@ while ((idx = str.indexOf(sub, pos)) != -1) {@b@ int idx;@b@ ++count;@b@ pos = idx + sub.length();@b@ }@b@ return count;@b@ }@b@@b@ public static String replace(String inString, String oldPattern, String newPattern)@b@ {@b@ if ((!(hasLength(inString))) || (!(hasLength(oldPattern))) || (newPattern == null))@b@ return inString;@b@@b@ StringBuilder sb = new StringBuilder();@b@ int pos = 0;@b@ int index = inString.indexOf(oldPattern);@b@@b@ int patLen = oldPattern.length();@b@ while (index >= 0) {@b@ sb.append(inString.substring(pos, index));@b@ sb.append(newPattern);@b@ pos = index + patLen;@b@ index = inString.indexOf(oldPattern, pos);@b@ }@b@ sb.append(inString.substring(pos));@b@@b@ return sb.toString();@b@ }@b@@b@ public static String delete(String inString, String pattern)@b@ {@b@ return replace(inString, pattern, "");@b@ }@b@@b@ public static String deleteAny(String inString, String charsToDelete)@b@ {@b@ if ((!(hasLength(inString))) || (!(hasLength(charsToDelete))))@b@ return inString;@b@@b@ StringBuilder sb = new StringBuilder();@b@ for (int i = 0; i < inString.length(); ++i) {@b@ char c = inString.charAt(i);@b@ if (charsToDelete.indexOf(c) == -1)@b@ sb.append(c);@b@ }@b@@b@ return sb.toString();@b@ }@b@@b@ public static String quote(String str)@b@ {@b@ return ((str != null) ? new StringBuilder().append("'").append(str).append("'").toString() : null);@b@ }@b@@b@ public static Object quoteIfString(Object obj)@b@ {@b@ return ((obj instanceof String) ? quote((String)obj) : obj);@b@ }@b@@b@ public static String unqualify(String qualifiedName)@b@ {@b@ return unqualify(qualifiedName, '.');@b@ }@b@@b@ public static String unqualify(String qualifiedName, char separator)@b@ {@b@ return qualifiedName.substring(qualifiedName.lastIndexOf(separator) + 1);@b@ }@b@@b@ public static String capitalize(String str)@b@ {@b@ return changeFirstCharacterCase(str, true);@b@ }@b@@b@ public static String uncapitalize(String str)@b@ {@b@ return changeFirstCharacterCase(str, false);@b@ }@b@@b@ private static String changeFirstCharacterCase(String str, boolean capitalize) {@b@ if ((str == null) || (str.length() == 0))@b@ return str;@b@@b@ StringBuilder sb = new StringBuilder(str.length());@b@ if (capitalize)@b@ sb.append(Character.toUpperCase(str.charAt(0)));@b@ else@b@ sb.append(Character.toLowerCase(str.charAt(0)));@b@@b@ sb.append(str.substring(1));@b@ return sb.toString();@b@ }@b@@b@ public static String getFilename(String path)@b@ {@b@ if (path == null)@b@ return null;@b@@b@ int separatorIndex = path.lastIndexOf("/");@b@ return ((separatorIndex != -1) ? path.substring(separatorIndex + 1) : path);@b@ }@b@@b@ public static String getFilenameExtension(String path)@b@ {@b@ if (path == null)@b@ return null;@b@@b@ int extIndex = path.lastIndexOf(46);@b@ if (extIndex == -1)@b@ return null;@b@@b@ int folderIndex = path.lastIndexOf("/");@b@ if (folderIndex > extIndex)@b@ return null;@b@@b@ return path.substring(extIndex + 1);@b@ }@b@@b@ public static String stripFilenameExtension(String path)@b@ {@b@ if (path == null)@b@ return null;@b@@b@ int extIndex = path.lastIndexOf(46);@b@ if (extIndex == -1)@b@ return path;@b@@b@ int folderIndex = path.lastIndexOf("/");@b@ if (folderIndex > extIndex)@b@ return path;@b@@b@ return path.substring(0, extIndex);@b@ }@b@@b@ public static String applyRelativePath(String path, String relativePath)@b@ {@b@ int separatorIndex = path.lastIndexOf("/");@b@ if (separatorIndex != -1) {@b@ String newPath = path.substring(0, separatorIndex);@b@ if (!(relativePath.startsWith("/")))@b@ newPath = new StringBuilder().append(newPath).append("/").toString();@b@@b@ return new StringBuilder().append(newPath).append(relativePath).toString();@b@ }@b@ return relativePath;@b@ }@b@@b@ public static String cleanPath(String path)@b@ {@b@ if (path == null)@b@ return null;@b@@b@ String pathToUse = replace(path, "\\", "/");@b@@b@ int prefixIndex = pathToUse.indexOf(":");@b@ String prefix = "";@b@ if (prefixIndex != -1) {@b@ prefix = pathToUse.substring(0, prefixIndex + 1);@b@ if (prefix.contains("/"))@b@ prefix = "";@b@ else@b@ pathToUse = pathToUse.substring(prefixIndex + 1);@b@ }@b@@b@ if (pathToUse.startsWith("/")) {@b@ prefix = new StringBuilder().append(prefix).append("/").toString();@b@ pathToUse = pathToUse.substring(1);@b@ }@b@@b@ String[] pathArray = delimitedListToStringArray(pathToUse, "/");@b@ List pathElements = new LinkedList();@b@ int tops = 0;@b@@b@ for (int i = pathArray.length - 1; i >= 0; --i) {@b@ String element = pathArray[i];@b@ if (".".equals(element)) break label186:@b@@b@ if ("..".equals(element))@b@ {@b@ ++tops;@b@ }@b@ else if (tops > 0)@b@ {@b@ --tops;@b@ }@b@ else {@b@ pathElements.add(0, element);@b@ }@b@@b@ }@b@@b@ for (i = 0; i < tops; ++i) {@b@ label186: pathElements.add(0, "..");@b@ }@b@@b@ return new StringBuilder().append(prefix).append(collectionToDelimitedString(pathElements, "/")).toString();@b@ }@b@@b@ public static boolean pathEquals(String path1, String path2)@b@ {@b@ return cleanPath(path1).equals(cleanPath(path2));@b@ }@b@@b@ public static Locale parseLocaleString(String localeString)@b@ {@b@ String[] parts = tokenizeToStringArray(localeString, "_ ", false, false);@b@ String language = (parts.length > 0) ? parts[0] : "";@b@ String country = (parts.length > 1) ? parts[1] : "";@b@ validateLocalePart(language);@b@ validateLocalePart(country);@b@ String variant = "";@b@ if (parts.length > 2)@b@ {@b@ int endIndexOfCountryCode = localeString.indexOf(country, language.length()) + country.length();@b@@b@ variant = trimLeadingWhitespace(localeString.substring(endIndexOfCountryCode));@b@ if (variant.startsWith("_"))@b@ variant = trimLeadingCharacter(variant, '_');@b@ }@b@@b@ return ((language.length() > 0) ? new Locale(language, country, variant) : null);@b@ }@b@@b@ private static void validateLocalePart(String localePart) {@b@ for (int i = 0; i < localePart.length(); ++i) {@b@ char ch = localePart.charAt(i);@b@ if ((ch != '_') && (ch != ' ') && (!(Character.isLetterOrDigit(ch))))@b@ throw new IllegalArgumentException(new StringBuilder().append("Locale part \"").append(localePart).append("\" contains invalid characters").toString());@b@ }@b@ }@b@@b@ public static String toLanguageTag(Locale locale)@b@ {@b@ return new StringBuilder().append(locale.getLanguage()).append((hasText(locale.getCountry())) ? new StringBuilder().append("-").append(locale.getCountry()).toString() : "").toString();@b@ }@b@@b@ public static TimeZone parseTimeZoneString(String timeZoneString)@b@ {@b@ TimeZone timeZone = TimeZone.getTimeZone(timeZoneString);@b@ if (("GMT".equals(timeZone.getID())) && (!(timeZoneString.startsWith("GMT"))))@b@ {@b@ throw new IllegalArgumentException(new StringBuilder().append("Invalid time zone specification '").append(timeZoneString).append("'").toString());@b@ }@b@ return timeZone;@b@ }@b@@b@ public static String[] addStringToArray(String[] array, String str)@b@ {@b@ if (ObjectUtils.isEmpty(array))@b@ return new String[] { str };@b@@b@ String[] newArr = new String[array.length + 1];@b@ System.arraycopy(array, 0, newArr, 0, array.length);@b@ newArr[array.length] = str;@b@ return newArr;@b@ }@b@@b@ public static String[] concatenateStringArrays(String[] array1, String[] array2)@b@ {@b@ if (ObjectUtils.isEmpty(array1))@b@ return array2;@b@@b@ if (ObjectUtils.isEmpty(array2))@b@ return array1;@b@@b@ String[] newArr = new String[array1.length + array2.length];@b@ System.arraycopy(array1, 0, newArr, 0, array1.length);@b@ System.arraycopy(array2, 0, newArr, array1.length, array2.length);@b@ return newArr;@b@ }@b@@b@ public static String[] mergeStringArrays(String[] array1, String[] array2)@b@ {@b@ if (ObjectUtils.isEmpty(array1))@b@ return array2;@b@@b@ if (ObjectUtils.isEmpty(array2))@b@ return array1;@b@@b@ List result = new ArrayList();@b@ result.addAll(Arrays.asList(array1));@b@ String[] arr$ = array2; int len$ = arr$.length; for (int i$ = 0; i$ < len$; ++i$) { String str = arr$[i$];@b@ if (!(result.contains(str)))@b@ result.add(str);@b@ }@b@@b@ return toStringArray(result);@b@ }@b@@b@ public static String[] sortStringArray(String[] array)@b@ {@b@ if (ObjectUtils.isEmpty(array))@b@ return new String[0];@b@@b@ Arrays.sort(array);@b@ return array;@b@ }@b@@b@ public static String[] toStringArray(Collection<String> collection)@b@ {@b@ if (collection == null)@b@ return null;@b@@b@ return ((String[])collection.toArray(new String[collection.size()]));@b@ }@b@@b@ public static String[] toStringArray(Enumeration<String> enumeration)@b@ {@b@ if (enumeration == null)@b@ return null;@b@@b@ List list = Collections.list(enumeration);@b@ return ((String[])list.toArray(new String[list.size()]));@b@ }@b@@b@ public static String[] trimArrayElements(String[] array)@b@ {@b@ if (ObjectUtils.isEmpty(array))@b@ return new String[0];@b@@b@ String[] result = new String[array.length];@b@ for (int i = 0; i < array.length; ++i) {@b@ String element = array[i];@b@ result[i] = ((element != null) ? element.trim() : null);@b@ }@b@ return result;@b@ }@b@@b@ public static String[] removeDuplicateStrings(String[] array)@b@ {@b@ if (ObjectUtils.isEmpty(array))@b@ return array;@b@@b@ Set set = new LinkedHashSet();@b@ String[] arr$ = array; int len$ = arr$.length; for (int i$ = 0; i$ < len$; ++i$) { String element = arr$[i$];@b@ set.add(element);@b@ }@b@ return toStringArray(set);@b@ }@b@@b@ public static String[] split(String toSplit, String delimiter)@b@ {@b@ if ((!(hasLength(toSplit))) || (!(hasLength(delimiter))))@b@ return null;@b@@b@ int offset = toSplit.indexOf(delimiter);@b@ if (offset < 0)@b@ return null;@b@@b@ String beforeDelimiter = toSplit.substring(0, offset);@b@ String afterDelimiter = toSplit.substring(offset + delimiter.length());@b@ return new String[] { beforeDelimiter, afterDelimiter };@b@ }@b@@b@ public static Properties splitArrayElementsIntoProperties(String[] array, String delimiter)@b@ {@b@ return splitArrayElementsIntoProperties(array, delimiter, null);@b@ }@b@@b@ public static Properties splitArrayElementsIntoProperties(String[] array, String delimiter, String charsToDelete)@b@ {@b@ if (ObjectUtils.isEmpty(array))@b@ return null;@b@@b@ Properties result = new Properties();@b@ String[] arr$ = array; int len$ = arr$.length; for (int i$ = 0; i$ < len$; ++i$) { String element = arr$[i$];@b@ if (charsToDelete != null)@b@ element = deleteAny(element, charsToDelete);@b@@b@ String[] splittedElement = split(element, delimiter);@b@ if (splittedElement == null)@b@ break label89:@b@@b@ result.setProperty(splittedElement[0].trim(), splittedElement[1].trim());@b@ }@b@ label89: return result;@b@ }@b@@b@ public static String[] tokenizeToStringArray(String str, String delimiters)@b@ {@b@ return tokenizeToStringArray(str, delimiters, true, true);@b@ }@b@@b@ public static String[] tokenizeToStringArray(String str, String delimiters, boolean trimTokens, boolean ignoreEmptyTokens)@b@ {@b@ if (str == null)@b@ return null;@b@@b@ StringTokenizer st = new StringTokenizer(str, delimiters);@b@ List tokens = new ArrayList();@b@ while (st.hasMoreTokens()) {@b@ String token = st.nextToken();@b@ if (trimTokens)@b@ token = token.trim();@b@@b@ if ((!(ignoreEmptyTokens)) || (token.length() > 0))@b@ tokens.add(token);@b@ }@b@@b@ return toStringArray(tokens);@b@ }@b@@b@ public static String[] delimitedListToStringArray(String str, String delimiter)@b@ {@b@ return delimitedListToStringArray(str, delimiter, null);@b@ }@b@@b@ public static String[] delimitedListToStringArray(String str, String delimiter, String charsToDelete)@b@ {@b@ int i;@b@ if (str == null)@b@ return new String[0];@b@@b@ if (delimiter == null)@b@ return new String[] { str };@b@@b@ List result = new ArrayList();@b@ if ("".equals(delimiter)) {@b@ for (i = 0; i < str.length(); ++i)@b@ result.add(deleteAny(str.substring(i, i + 1), charsToDelete));@b@ }@b@ else {@b@ int pos = 0;@b@@b@ while ((delPos = str.indexOf(delimiter, pos)) != -1) {@b@ int delPos;@b@ result.add(deleteAny(str.substring(pos, delPos), charsToDelete));@b@ pos = delPos + delimiter.length();@b@ }@b@ if ((str.length() > 0) && (pos <= str.length()))@b@ {@b@ result.add(deleteAny(str.substring(pos), charsToDelete));@b@ }@b@ }@b@ return toStringArray(result);@b@ }@b@@b@ public static String[] commaDelimitedListToStringArray(String str)@b@ {@b@ return delimitedListToStringArray(str, ",");@b@ }@b@@b@ public static Set<String> commaDelimitedListToSet(String str)@b@ {@b@ Set set = new LinkedHashSet();@b@ String[] tokens = commaDelimitedListToStringArray(str);@b@ String[] arr$ = tokens; int len$ = arr$.length; for (int i$ = 0; i$ < len$; ++i$) { String token = arr$[i$];@b@ set.add(token);@b@ }@b@ return set;@b@ }@b@@b@ public static String collectionToDelimitedString(Collection<?> coll, String delim, String prefix, String suffix)@b@ {@b@ if (CollectionUtils.isEmpty(coll))@b@ return "";@b@@b@ StringBuilder sb = new StringBuilder();@b@ Iterator it = coll.iterator();@b@ while (true) { do { if (!(it.hasNext())) break label78;@b@ sb.append(prefix).append(it.next()).append(suffix); }@b@ while (!(it.hasNext()));@b@ sb.append(delim);@b@ }@b@@b@ label78: return sb.toString();@b@ }@b@@b@ public static String collectionToDelimitedString(Collection<?> coll, String delim)@b@ {@b@ return collectionToDelimitedString(coll, delim, "", "");@b@ }@b@@b@ public static String collectionToCommaDelimitedString(Collection<?> coll)@b@ {@b@ return collectionToDelimitedString(coll, ",");@b@ }@b@@b@ public static String arrayToDelimitedString(Object[] arr, String delim)@b@ {@b@ if (ObjectUtils.isEmpty(arr))@b@ return "";@b@@b@ if (arr.length == 1)@b@ return ObjectUtils.nullSafeToString(arr[0]);@b@@b@ StringBuilder sb = new StringBuilder();@b@ for (int i = 0; i < arr.length; ++i) {@b@ if (i > 0)@b@ sb.append(delim);@b@@b@ sb.append(arr[i]);@b@ }@b@ return sb.toString();@b@ }@b@@b@ public static String arrayToCommaDelimitedString(Object[] arr)@b@ {@b@ return arrayToDelimitedString(arr, ",");@b@ }@b@}