提交 c62d4cb4 编写于 作者: A Arjen Poutsma

Generified StringUtils, and replaced StringBuffer usage with StringBuilder

上级 ba425941
...@@ -48,6 +48,7 @@ import java.util.TreeSet; ...@@ -48,6 +48,7 @@ import java.util.TreeSet;
* @author Keith Donald * @author Keith Donald
* @author Rob Harrop * @author Rob Harrop
* @author Rick Evans * @author Rick Evans
* @author Arjen Poutsma
* @since 16 April 2001 * @since 16 April 2001
* @see org.apache.commons.lang.StringUtils * @see org.apache.commons.lang.StringUtils
*/ */
...@@ -598,7 +599,7 @@ public abstract class StringUtils { ...@@ -598,7 +599,7 @@ public abstract class StringUtils {
} }
String[] pathArray = delimitedListToStringArray(pathToUse, FOLDER_SEPARATOR); String[] pathArray = delimitedListToStringArray(pathToUse, FOLDER_SEPARATOR);
List pathElements = new LinkedList(); List<String> pathElements = new LinkedList<String>();
int tops = 0; int tops = 0;
for (int i = pathArray.length - 1; i >= 0; i--) { for (int i = pathArray.length - 1; i >= 0; i--) {
...@@ -736,7 +737,7 @@ public abstract class StringUtils { ...@@ -736,7 +737,7 @@ public abstract class StringUtils {
if (ObjectUtils.isEmpty(array2)) { if (ObjectUtils.isEmpty(array2)) {
return array1; return array1;
} }
List result = new ArrayList(); List<String> result = new ArrayList<String>();
result.addAll(Arrays.asList(array1)); result.addAll(Arrays.asList(array1));
for (int i = 0; i < array2.length; i++) { for (int i = 0; i < array2.length; i++) {
String str = array2[i]; String str = array2[i];
...@@ -767,11 +768,11 @@ public abstract class StringUtils { ...@@ -767,11 +768,11 @@ public abstract class StringUtils {
* @return the String array (<code>null</code> if the passed-in * @return the String array (<code>null</code> if the passed-in
* Collection was <code>null</code>) * Collection was <code>null</code>)
*/ */
public static String[] toStringArray(Collection collection) { public static String[] toStringArray(Collection<String> collection) {
if (collection == null) { if (collection == null) {
return null; return null;
} }
return (String[]) collection.toArray(new String[collection.size()]); return collection.toArray(new String[collection.size()]);
} }
/** /**
...@@ -781,12 +782,12 @@ public abstract class StringUtils { ...@@ -781,12 +782,12 @@ public abstract class StringUtils {
* @return the String array (<code>null</code> if the passed-in * @return the String array (<code>null</code> if the passed-in
* Enumeration was <code>null</code>) * Enumeration was <code>null</code>)
*/ */
public static String[] toStringArray(Enumeration enumeration) { public static String[] toStringArray(Enumeration<String> enumeration) {
if (enumeration == null) { if (enumeration == null) {
return null; return null;
} }
List list = Collections.list(enumeration); List<String> list = Collections.list(enumeration);
return (String[]) list.toArray(new String[list.size()]); return list.toArray(new String[list.size()]);
} }
/** /**
...@@ -817,7 +818,7 @@ public abstract class StringUtils { ...@@ -817,7 +818,7 @@ public abstract class StringUtils {
if (ObjectUtils.isEmpty(array)) { if (ObjectUtils.isEmpty(array)) {
return array; return array;
} }
Set set = new TreeSet(); Set<String> set = new TreeSet<String>();
for (int i = 0; i < array.length; i++) { for (int i = 0; i < array.length; i++) {
set.add(array[i]); set.add(array[i]);
} }
...@@ -941,7 +942,7 @@ public abstract class StringUtils { ...@@ -941,7 +942,7 @@ public abstract class StringUtils {
return null; return null;
} }
StringTokenizer st = new StringTokenizer(str, delimiters); StringTokenizer st = new StringTokenizer(str, delimiters);
List tokens = new ArrayList(); List<String> tokens = new ArrayList<String>();
while (st.hasMoreTokens()) { while (st.hasMoreTokens()) {
String token = st.nextToken(); String token = st.nextToken();
if (trimTokens) { if (trimTokens) {
...@@ -989,7 +990,7 @@ public abstract class StringUtils { ...@@ -989,7 +990,7 @@ public abstract class StringUtils {
if (delimiter == null) { if (delimiter == null) {
return new String[] {str}; return new String[] {str};
} }
List result = new ArrayList(); List<String> result = new ArrayList<String>();
if ("".equals(delimiter)) { if ("".equals(delimiter)) {
for (int i = 0; i < str.length(); i++) { for (int i = 0; i < str.length(); i++) {
result.add(deleteAny(str.substring(i, i + 1), charsToDelete)); result.add(deleteAny(str.substring(i, i + 1), charsToDelete));
...@@ -1025,8 +1026,8 @@ public abstract class StringUtils { ...@@ -1025,8 +1026,8 @@ public abstract class StringUtils {
* @param str the input String * @param str the input String
* @return a Set of String entries in the list * @return a Set of String entries in the list
*/ */
public static Set commaDelimitedListToSet(String str) { public static Set<String> commaDelimitedListToSet(String str) {
Set set = new TreeSet(); Set<String> set = new TreeSet<String>();
String[] tokens = commaDelimitedListToStringArray(str); String[] tokens = commaDelimitedListToStringArray(str);
for (int i = 0; i < tokens.length; i++) { for (int i = 0; i < tokens.length; i++) {
set.add(tokens[i]); set.add(tokens[i]);
...@@ -1047,7 +1048,7 @@ public abstract class StringUtils { ...@@ -1047,7 +1048,7 @@ public abstract class StringUtils {
if (CollectionUtils.isEmpty(coll)) { if (CollectionUtils.isEmpty(coll)) {
return ""; return "";
} }
StringBuffer sb = new StringBuffer(); StringBuilder sb = new StringBuilder();
Iterator it = coll.iterator(); Iterator it = coll.iterator();
while (it.hasNext()) { while (it.hasNext()) {
sb.append(prefix).append(it.next()).append(suffix); sb.append(prefix).append(it.next()).append(suffix);
...@@ -1090,7 +1091,7 @@ public abstract class StringUtils { ...@@ -1090,7 +1091,7 @@ public abstract class StringUtils {
if (ObjectUtils.isEmpty(arr)) { if (ObjectUtils.isEmpty(arr)) {
return ""; return "";
} }
StringBuffer sb = new StringBuffer(); StringBuilder sb = new StringBuilder();
for (int i = 0; i < arr.length; i++) { for (int i = 0; i < arr.length; i++) {
if (i > 0) { if (i > 0) {
sb.append(delim); sb.append(delim);
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册