package biz.evot.util;
import java.util.ArrayList;
import java.util.Collection;
import java.util.Enumeration;
import java.util.Iterator;
import java.util.LinkedList;
import java.util.List;
import java.util.Properties;
import java.util.Set;
import java.util.StringTokenizer;
import java.util.TreeSet;
import java.util.Vector;
/**
*
*
* @author Jisung, An
* @created 2001 10 4
*/
public class TextUtil {
/**
*
*/
private static final String FOLDER_SEPARATOR = "/";
private static final String WINDOWS_FOLDER_SEPARATOR = "\\";
private static final String TOP_PATH = "..";
private static final String CURRENT_PATH = ".";
private static final char EXTENSION_SEPARATOR = '.';
public final static char WHITE_SPACE = ' ';
static String m_whiteSpace = " \t\n\r";
static char m_citChar = '"';
/**
* Property . Property 'key=value' . ,
* Property '::' .<br>
* Example<br>
* <code>
* String source = "key1=value1::key2=value2::key3=value3";<br>
* String key = "key2";<br>
* String value = TextUtil.getParam(source,key,"Default Value");<br>
* </code> "value2" .
*
* @param source
*
* @param key
*
* @param defaultValue
* Key
* @return Property Value
*/
public static String getParam(String source, String key, String defaultValue) {
if (source == null || key == null) {
return defaultValue;
}
int i = source.indexOf(key + "=");
if (i < 0) {
return defaultValue;
}
int j = i + key.length() + 1;
int k = source.indexOf("::", j);
if (k < 0) {
k = source.length();
}
try {
return source.substring(j, k);
} catch (Exception _ex) {
return defaultValue;
}
}
/**
* . .<br>
* <code>
* String source = "ABCDEFG";<br>
* String result = TextUtil.alignLeft(source, 10);<br>
* </code> <code>result</code> <code>"ABCDEFG "</code> .
*
* @param source
*
* @param length
*
* @return
*/
public static String alignLeft(String source, int length) {
return alignLeft(source, length, false);
}
/**
* .<br>
* <code>
* String source = "ABCDEFG";<br>
* String result = TextUtil.alignLeft(source, 5, true);<br>
* </code> <code>result</code> <code>"AB..."</code> .
*
* @param source
*
* @param length
*
* @param isEllipsis
* ("...")
* @return
*/
public static String alignLeft(String source, int length, boolean isEllipsis) {
if (source.length() <= length) {
StringBuffer temp = new StringBuffer(source);
for (int i = 0; i < (length - source.length()); i++) {
temp.append(WHITE_SPACE);
}
return temp.toString();
} else {
if (isEllipsis) {
StringBuffer temp = new StringBuffer(length);
temp.append(source.substring(0, length - 3));
temp.append("...");
return temp.toString();
} else {
return source.substring(0, length);
}
}
}
/**
* . .<br>
* <code>
* String source = "ABCDEFG";<br>
* String result = TextUtil.alignRight(source, 10);<br>
* </code> <code>result</code> <code>" ABCDEFG"</code> .
*
* @param source
*
* @param length
*
* @return
*/
public static String alignRight(String source, int length) {
return alignRight(source, length, false);
}
/**
* .<br>
* <code>
* String source = "ABCDEFG";<br>
* String result = TextUtil.alignRight(source, 5, true);<br>
* </code> <code>result</code> <code>"AB..."</code> .
*
* @param source
*
* @param length
*
* @param isEllipsis
* ("...")
* @return
*/
public static String alignRight(String source, int length,
boolean isEllipsis) {
if (source.length() <= length) {
StringBuffer temp = new StringBuffer(length);
for (int i = 0; i < (length - source.length()); i++) {
temp.append(WHITE_SPACE);
}
temp.append(source);
return temp.toString();
} else {
if (isEllipsis) {
StringBuffer temp = new StringBuffer(length);
temp.append(source.substring(0, length - 3));
temp.append("...");
return temp.toString();
} else {
return source.substring(0, length);
}
}
}
/**
* . . .<br>
* <code>
* String source = "ABCDEFG";<br>
* String result = TextUtil.alignCenter(source, 10);<br>
* </code> <code>result</code> <code>" ABCDEFG "</code> .
*
* @param source
*
* @param length
*
* @return
*/
public static String alignCenter(String source, int length) {
return alignCenter(source, length, false);
}
/**
* . . .<br>
* <code>
* String source = "ABCDEFG";<br>
* String result = TextUtil.alignCenter(source, 5,true);<br>
* </code> <code>result</code> <code>"AB..."</code> .
*
* @param source
*
* @param length
*
* @param isEllipsis
* ("...")
* @return
*/
public static String alignCenter(String source, int length,
boolean isEllipsis) {
if (source.length() <= length) {
StringBuffer temp = new StringBuffer(length);
int leftMargin = (int) (length - source.length()) / 2;
int rightMargin;
if ((leftMargin * 2) == (length - source.length())) {
rightMargin = leftMargin;
} else {
rightMargin = leftMargin + 1;
}
for (int i = 0; i < leftMargin; i++) {
temp.append(WHITE_SPACE);
}
temp.append(source);
for (int i = 0; i < rightMargin; i++) {
temp.append(WHITE_SPACE);
}
return temp.toString();
} else {
if (isEllipsis) {
StringBuffer temp = new StringBuffer(length);
temp.append(source.substring(0, length - 3));
temp.append("...");
return temp.toString();
} else {
return source.substring(0, length);
}
}
}
/**
* .<br>
* <code>
* String source = "abcdefg";<br>
* String result = TextUtil.capitalize(source);<br>
* </code> <code>result</code> <code>"Abcdefg"</code> .
*
* @param src
*
* @return
*/
public static String capitalize(String src) {
return !src.equals("") && src != null ? src.substring(0, 1)
.toUpperCase()
+ src.substring(1).toLowerCase() : src;
}
/**
* target boolean .<br>
* <code>
* String source = "Onwer is [B] statues.";<br>
* String result = TextUtil.replace(source, "[B]",true);<br>
* </code> <code>result</code> <code>"Onwer is true statues."</code>
* .
*
* @param src
*
* @param find
*
* @param flag
* boolean
* @return
*/
public static String replace(String src, String find, boolean flag) {
return replace(src, find, String.valueOf(flag));
}
/**
* target .<br>
* <code>
* String source = "Onwer is [I] statues.";<br>
* String result = TextUtil.replace(source, "[I]",15);<br>
* </code> <code>result</code> <code>"Onwer is 15 statues."</code>
* .
*
* @param srv
*
* @param find
*
* @param i
*
* @return
*/
public static String replace(String srv, String find, int i) {
return replace(srv, find, String.valueOf(i));
}
/**
* target .<br>
* <code>
* String source = "Onwer is [I] statues.";<br>
* String result = TextUtil.replace(source, "[I]","fool");<br>
* </code> <code>result</code> <code>"Onwer is fool statues."</code>
* .
*
* @param src
*
* @param find
*
* @param toReplace
*
* @return
*/
public static String replace(String src, String find, String toReplace) {
// boolean flag = false;
StringBuffer stringbuffer = new StringBuffer(src.length());
int j = 0;
for (int i = src.indexOf(find, j); i != -1; i = src.indexOf(find, j)) {
stringbuffer.append(src.substring(j, i));
stringbuffer.append(toReplace);
j = i + find.length();
}
if (j < src.length()) {
stringbuffer.append(src.substring(j));
}
return stringbuffer.toString();
}
public static String delete(String src, String pattern) {
return replace(src, pattern, "");
}
/**
* character .
*
* @param src
* @param charsToDelete
* @return
*/
public static String deleteAny(String src, String charsToDelete) {
if (src == null || charsToDelete == null) {
return src;
}
StringBuffer buf = new StringBuffer();
for (int i = 0; i < src.length(); i++) {
char c = src.charAt(i);
if (charsToDelete.indexOf(c) == -1) {
buf.append(c);
}
}
return buf.toString();
}
/**
* . .<br>
* <code>
* String[] source = new String[] {"AAA","BBB","CCC"};<br>
* String result = TextUtil.join(source,"+");<br>
* </code> <code>result</code> <code>"AAA+BBB+CCC"</code> .
*
* @param src
*
* @param join
*
* @return
*/
public static String join(Object[] src, String join) {
StringBuffer stringbuffer = new StringBuffer();
int i = src.length;
if (i > 0) {
stringbuffer.append(src[0].toString());
}
for (int j = 1; j < i; j++) {
stringbuffer.append(join);
stringbuffer.append(src[j].toString());
}
return stringbuffer.toString();
}
/**
* . .<br>
* <code>
* String[] source = new String[] {"AAA","BBB","CCC"};<br>
* String result = TextUtil.join(source,"+");<br>
* </code> <code>result</code> <code>"AAA+BBB+CCC"</code> .
*
* @param src
*
* @param join
*
* @return
*/
public static String join(int[] src, String join) {
StringBuffer stringbuffer = new StringBuffer();
int i = src.length;
if (i > 0) {
stringbuffer.append(src[0]);
}
for (int j = 1; j < i; j++) {
stringbuffer.append(join);
stringbuffer.append(src[j]);
}
return stringbuffer.toString();
}
/**
* . .<br>
* 2 16 .
* <code>
* String[] source = new String[] {"AAA","BBB","CCC"};<br>
* String result = TextUtil.join(source,"+");<br>
* </code> <code>result</code> <code>"AAA+BBB+CCC"</code> .
*
* @param src
*
* @param join
*
* @return
*/
public static String join(byte[] src, String join) {
StringBuffer stringbuffer = new StringBuffer();
int i = src.length;
if (i > 0) {
stringbuffer.append(toHexString(src[0]));
}
for (int j = 1; j < i; j++) {
stringbuffer.append(join);
stringbuffer.append(src[j]);
}
return stringbuffer.toString();
}
/**
* Token Seperator Tokenize.<br>
* <code>
* String source = "Text token\tis A Good\nAnd bad.";<br>
* String[] result = TextUtil.split(source, " \t\n");<br>
* </code> <code>result</code>
* <code>"Text","token","is","A","Good","And","bad."</code> .
*
* @param src
*
* @param sperator
* Token Seperators
* @return
*/
public static String[] split(String src, String sperator) {
StringTokenizer stringtokenizer = new StringTokenizer(src, sperator);
int i = stringtokenizer.countTokens();
String as[] = new String[i];
for (int j = 0; j < i; j++) {
as[j] = stringtokenizer.nextToken();
}
return as;
}
/**
* (' ','\n','\t','\r') .('"')
* .<br>
* <code>
* String source = "Text token\tis A Good\nAnd\rbad.";<br>
* String[] result = TextUtil.splitwords(source);<br>
* </code> <code>result</code>
* <code>"Text","token","is","A","Good","And","bad."</code> .
*
* @param src
*
* @return
*/
public static String[] splitwords(String src) {
return splitwords(src, m_whiteSpace);
}
/**
* . ('"') .<br>
* <code>
* String source = "Text token\tis A \"Good day\"\nAnd\r\"bad day.\"";<br>
* String[] result = TextUtil.splitwords(source);<br>
* </code> <code>result</code>
* <code>"Text","token","is","A","Good day","And","bad day."</code>
* .
*
* @param src
*
* @param separators
* Token Seperators
* @return Description of the Returned Value
*/
public static String[] splitwords(String src, String separators) {
boolean flag = false;
StringBuffer stringbuffer = null;
Vector vector = new Vector();
for (int i = 0; i < src.length();) {
char c = src.charAt(i);
if (!flag && separators.indexOf(c) != -1) {
if (stringbuffer != null) {
vector.addElement(stringbuffer);
stringbuffer = null;
}
for (; i < src.length()
&& separators.indexOf(src.charAt(i)) != -1; i++) {
;
}
} else {
if (c == m_citChar) {
if (flag) {
flag = false;
} else {
flag = true;
}
} else {
if (stringbuffer == null) {
stringbuffer = new StringBuffer();
}
stringbuffer.append(c);
}
i++;
}
}
if (stringbuffer != null) {
vector.addElement(stringbuffer);
}
String as[] = new String[vector.size()];
for (int j = 0; j < vector.size(); j++) {
as[j] = new String((StringBuffer) vector.elementAt(j));
}
return as;
}
/**
* Vector .<br>
*
* @param array
*
* @return Vector
*/
public static Vector toVector(Object[] array) {
if (array == null) {
return null;
}
Vector vec = new Vector(array.length);
for (int i = 0; i < array.length; i++) {
vec.add(i, array[i]);
}
return vec;
}
/**
* .
*/
public static void sortStringArray(String[] source) {
java.util.Arrays.sort(source);
}
/**
* Enemration .
*/
public static String[] sortStringArray(Enumeration source) {
Vector buf = new Vector();
while (source.hasMoreElements()) {
buf.add(source.nextElement());
}
String[] buf2 = new String[buf.size()];
for (int i = 0; i < buf.size(); i++) {
Object obj = buf.get(i);
if (obj instanceof String) {
buf2[i] = (String) obj;
} else {
throw new IllegalArgumentException("Not String Array");
}
}
java.util.Arrays.sort(buf2);
return buf2;
}
/**
* byte .
*
* @param data
* @return
* @since 3.0
*/
public static String toHexArrayString(byte[] data) {
return toHexArrayString(data, ',');
}
/**
* byte .
*
* @param data
* @param c
* @return
*/
public static String toHexArrayString(byte[] data, char c) {
StringBuffer buf = new StringBuffer();
int len = data.length;
if (len > 0) {
toHexString(buf, data[0]);
}
for (int j = 1; j < len; j++) {
buf.append(c);
toHexString(buf, data[j]);
}
return buf.toString();
}
/**
* byte 2 .
*
* @param data
* @return
*/
public static String toHexString(byte data) {
StringBuffer buf = new StringBuffer();
toHexString(buf, data);
return buf.toString();
}
/**
* byte 2 .
*
* @param buf
* @param data
*/
public static void toHexString(StringBuffer buf, byte data) {
if (data < 16)
buf.append('0');
buf.append(Integer.toHexString(data).toUpperCase());
}
/**
* String .
*
* @param str
* @return
*/
public static boolean hasText(String str) {
int strlen;
if (str == null || (strlen = str.length()) == 0) {
return false;
}
for (int i = 0; i < strlen; i++) {
if (!Character.isWhitespace(str.charAt(i))) {
return true;
}
}
return false;
}
/**
* String .
*
* @param str
* @return
*/
public static boolean hasLength(String str) {
return (str != null && str.length() > 0);
}
/**
* Str prefix .
*
* @param str
* @param prefix
* @return
*/
public static boolean startWithIgnorecase(String str, String prefix) {
if (str == null || prefix == null) {
return false;
}
if (str.startsWith(prefix)) {
return true;
}
if (str.length() < prefix.length()) {
return false;
}
String lcStr = str.substring(0, prefix.length()).toLowerCase();
String lcPrefix = prefix.toLowerCase();
return lcStr.equals(lcPrefix);
}
/**
* Str suffix .
*
* @param str
* @param prefix
* @return
*/
public static boolean endsWithIgnorecase(String str, String suffix) {
if (str == null || suffix == null) {
return false;
}
if (str.startsWith(suffix)) {
return true;
}
if (str.length() < suffix.length()) {
return false;
}
String lcStr = str.substring(str.length() - suffix.length())
.toLowerCase();
String lcPrefix = suffix.toLowerCase();
return lcStr.equals(lcPrefix);
}
/**
* .
*
* @param str
* @param sub
* @return
*/
public static int countOccurrencesOf(String str, String sub) {
if (str == null || sub == null || str.length() == 0
|| sub.length() == 0) {
return 0;
}
int count = 0, pos = 0, idx = 0;
while ((idx = str.indexOf(sub, pos)) != -1) {
++count;
pos = idx + sub.length();
}
return count;
}
/**
* .
*
* @param str
* @return
*/
public static String quote(String str) {
return (str != null ? "'" + str + "'" : null);
}
/**
* .
*
* @param path
* @return
*/
public static String getFileName(String path) {
if (path == null) {
return null;
}
int separatorIndex = path.lastIndexOf(FOLDER_SEPARATOR);
return (separatorIndex != -1 ? path.substring(separatorIndex + 1)
: path);
}
/**
* .
*
* @param path
* @return
*/
public static String getFileNameExtension(String path) {
if (path == null) {
return null;
}
int separatorIndex = path.lastIndexOf(EXTENSION_SEPARATOR);
return (separatorIndex != -1 ? path.substring(separatorIndex + 1)
: path);
}
/**
* Apply the given relative path to the given path, assuming standard Java
* folder separation (i.e. "/" separators);
*
* @param path
* the path to start from (usually a full file path)
* @param relativePath
* the relative path to apply (relative to the full file path
* above)
* @return the full file path that results from applying the relative path
*/
public static String applyRelativePath(String path, String relativePath) {
int separatorIndex = path.lastIndexOf(FOLDER_SEPARATOR);
if (separatorIndex != -1) {
String newPath = path.substring(0, separatorIndex);
if (!relativePath.startsWith(FOLDER_SEPARATOR)) {
newPath += FOLDER_SEPARATOR;
}
return newPath + relativePath;
} else {
return relativePath;
}
}
/**
* Normalize the path by suppressing sequences like "path/.." and inner
* simple dots.
* <p>
* The result is convenient for path comparison. For other uses, notice that
* Windows separators ("\") are replaced by simple slashes.
*
* @param path
* the original path
* @return the normalized path
*/
public static String cleanPath(String path) {
String pathToUse = replace(path, WINDOWS_FOLDER_SEPARATOR,
FOLDER_SEPARATOR);
// Strip prefix from path to analyze, to not treat it as part of the
// first path element. This is necessary to correctly parse paths like
// "file:core/../core/io/Resource.class", where the ".." should just
// strip the first "core" directory while keeping the "file:" prefix.
int prefixIndex = pathToUse.indexOf(":");
String prefix = "";
if (prefixIndex != -1) {
prefix = pathToUse.substring(0, prefixIndex + 1);
pathToUse = pathToUse.substring(prefixIndex + 1);
}
String[] pathArray = delimitedListToStringArray(pathToUse,
FOLDER_SEPARATOR);
List pathElements = new LinkedList();
int tops = 0;
for (int i = pathArray.length - 1; i >= 0; i--) {
if (CURRENT_PATH.equals(pathArray[i])) {
// Points to current directory - drop it.
} else if (TOP_PATH.equals(pathArray[i])) {
// Registering top path found.
tops++;
} else {
if (tops > 0) {
// Merging path element with corresponding to top path.
tops--;
} else {
// Normal path element found.
pathElements.add(0, pathArray[i]);
}
}
}
// Remaining top paths need to be retained.
for (int i = 0; i < tops; i++) {
pathElements.add(0, TOP_PATH);
}
return prefix
+ collectionToDelimitedString(pathElements, FOLDER_SEPARATOR);
}
/**
* Compare two paths after normalization of them.
*
* @param path1
* First path for comparizon
* @param path2
* Second path for comparizon
* @return whether the two paths are equivalent after normalization
*/
public static boolean pathEquals(String path1, String path2) {
return cleanPath(path1).equals(cleanPath(path2));
}
// ---------------------------------------------------------------------
// Convenience methods for working with String arrays
// ---------------------------------------------------------------------
/**
* Copy the given Collection into a String array. The Collection must
* contain String elements only.
*
* @param collection
* the Collection to copy
* @return the String array (<code>null</code> if the Collection was
* <code>null</code> as well)
*/
public static String[] toStringArray(Collection collection) {
if (collection == null) {
return null;
}
return (String[]) collection.toArray(new String[collection.size()]);
}
/**
* Take an array Strings and split each element based on the given
* delimiter. A <code>Properties</code> instance is then generated, with
* the left of the delimiter providing the key, and the right of the
* delimiter providing the value.
* <p>
* Will trim both the key and value before adding them to the
* <code>Properties</code> instance.
*
* @param array
* the array to process
* @param delimiter
* to split each element using (typically the equals symbol)
* @return a <code>Properties</code> instance representing the array
* contents, or <code>null</code> if the array to process was null
* or empty
*/
public static Properties splitArrayElementsIntoProperties(String[] array,
String delimiter) {
return splitArrayElementsIntoProperties(array, delimiter, null);
}
/**
* Take an array Strings and split each element based on the given
* delimiter. A <code>Properties</code> instance is then generated, with
* the left of the delimiter providing the key, and the right of the
* delimiter providing the value.
* <p>
* Will trim both the key and value before adding them to the
* <code>Properties</code> instance.
*
* @param array
* the array to process
* @param delimiter
* to split each element using (typically the equals symbol)
* @param charsToDelete
* one or more characters to remove from each element prior to
* attempting the split operation (typically the quotation mark
* symbol), or <code>null</code> if no removal should occur
* @return a <code>Properties</code> instance representing the array
* contents, or <code>null</code> if the array to process was null
* or empty
*/
public static Properties splitArrayElementsIntoProperties(String[] array,
String delimiter, String charsToDelete) {
if (array == null || array.length == 0) {
return null;
}
Properties result = new Properties();
for (int i = 0; i < array.length; i++) {
String element = array[i];
if (charsToDelete != null) {
element = deleteAny(array[i], charsToDelete);
}
String[] splittedElement = split(element, delimiter);
if (splittedElement == null) {
continue;
}
result.setProperty(splittedElement[0].trim(), splittedElement[1]
.trim());
}
return result;
}
/**
* Tokenize the given String into a String array via a StringTokenizer.
* Trims tokens and omits empty tokens.
* <p>
* The given delimiters string is supposed to consist of any number of
* delimiter characters. Each of those characters can be used to separate
* tokens. A delimiter is always a single character; for multi-character
* delimiters, consider using <code>delimitedListToStringArray</code>
*
* @param str
* the String to tokenize
* @param delimiters
* the delimiter characters, assembled as String (each of those
* characters is individually considered as delimiter).
* @return an array of the tokens
* @see java.util.StringTokenizer
* @see java.lang.String#trim
* @see #delimitedListToStringArray
*/
public static String[] tokenizeToStringArray(String str, String delimiters) {
return tokenizeToStringArray(str, delimiters, true, true);
}
/**
* Tokenize the given String into a String array via a StringTokenizer.
* <p>
* The given delimiters string is supposed to consist of any number of
* delimiter characters. Each of those characters can be used to separate
* tokens. A delimiter is always a single character; for multi-character
* delimiters, consider using <code>delimitedListToStringArray</code>
*
* @param str
* the String to tokenize
* @param delimiters
* the delimiter characters, assembled as String (each of those
* characters is individually considered as delimiter)
* @param trimTokens
* trim the tokens via String's <code>trim</code>
* @param ignoreEmptyTokens
* omit empty tokens from the result array (only applies to
* tokens that are empty after trimming; StringTokenizer will not
* consider subsequent delimiters as token in the first place).
* @return an array of the tokens
* @see java.util.StringTokenizer
* @see java.lang.String#trim
* @see #delimitedListToStringArray
*/
public static String[] tokenizeToStringArray(String str, String delimiters,
boolean trimTokens, boolean ignoreEmptyTokens) {
StringTokenizer st = new StringTokenizer(str, delimiters);
List tokens = new ArrayList();
while (st.hasMoreTokens()) {
String token = st.nextToken();
if (trimTokens) {
token = token.trim();
}
if (!ignoreEmptyTokens || token.length() > 0) {
tokens.add(token);
}
}
return toStringArray(tokens);
}
/**
* Take a String which is a delimited list and convert it to a String array.
* <p>
* A single delimiter can consists of more than one character: It will still
* be considered as single delimiter string, rather than as bunch of
* potential delimiter characters - in contrast to
* <code>tokenizeToStringArray</code>.
*
* @param str
* the input String
* @param delimiter
* the delimiter between elements (this is a single delimiter,
* rather than a bunch individual delimiter characters)
* @return an array of the tokens in the list
* @see #tokenizeToStringArray
*/
public static String[] delimitedListToStringArray(String str,
String delimiter) {
if (str == null) {
return new String[0];
}
if (delimiter == null) {
return new String[] { str };
}
List result = new ArrayList();
if ("".equals(delimiter)) {
for (int i = 0; i < str.length(); i++) {
result.add(str.substring(i, i + 1));
}
} else {
int pos = 0;
int delPos = 0;
while ((delPos = str.indexOf(delimiter, pos)) != -1) {
result.add(str.substring(pos, delPos));
pos = delPos + delimiter.length();
}
if (str.length() > 0 && pos <= str.length()) {
// Add rest of String, but not in case of empty input.
result.add(str.substring(pos));
}
}
return toStringArray(result);
}
/**
* Convert a CSV list into an array of Strings.
*
* @param str
* CSV list
* @return an array of Strings, or the empty array if s is null
*/
public static String[] commaDelimitedListToStringArray(String str) {
return delimitedListToStringArray(str, ",");
}
/**
* Convenience method to convert a CSV string list to a set. Note that this
* will suppress duplicates.
*
* @param str
* CSV String
* @return a Set of String entries in the list
*/
public static Set commaDelimitedListToSet(String str) {
Set set = new TreeSet();
String[] tokens = commaDelimitedListToStringArray(str);
for (int i = 0; i < tokens.length; i++) {
set.add(tokens[i]);
}
return set;
}
/**
* Convenience method to return a String array as a delimited (e.g. CSV)
* String. E.g. useful for toString() implementations.
*
* @param arr
* array to display. Elements may be of any type (toString will
* be called on each element).
* @param delim
* delimiter to use (probably a ",")
*/
public static String arrayToDelimitedString(Object[] arr, String delim) {
if (arr == null) {
return "";
}
StringBuffer sb = new StringBuffer();
for (int i = 0; i < arr.length; i++) {
if (i > 0) {
sb.append(delim);
}
sb.append(arr[i]);
}
return sb.toString();
}
/**
* Convenience method to return a Collection as a delimited (e.g. CSV)
* String. E.g. useful for toString() implementations.
*
* @param coll
* Collection to display
* @param delim
* delimiter to use (probably a ",")
* @param prefix
* string to start each element with
* @param suffix
* string to end each element with
*/
public static String collectionToDelimitedString(Collection coll,
String delim, String prefix, String suffix) {
if (coll == null) {
return "";
}
StringBuffer sb = new StringBuffer();
Iterator it = coll.iterator();
int i = 0;
while (it.hasNext()) {
if (i > 0) {
sb.append(delim);
}
sb.append(prefix).append(it.next()).append(suffix);
i++;
}
return sb.toString();
}
/**
* Convenience method to return a Collection as a delimited (e.g. CSV)
* String. E.g. useful for toString() implementations.
*
* @param coll
* Collection to display
* @param delim
* delimiter to use (probably a ",")
*/
public static String collectionToDelimitedString(Collection coll,
String delim) {
return collectionToDelimitedString(coll, delim, "", "");
}
/**
* Convenience method to return a String array as a CSV String. E.g. useful
* for toString() implementations.
*
* @param arr
* array to display. Elements may be of any type (toString will
* be called on each element).
*/
public static String arrayToCommaDelimitedString(Object[] arr) {
return arrayToDelimitedString(arr, ",");
}
/**
* Convenience method to return a Collection as a CSV String. E.g. useful
* for toString() implementations.
*
* @param coll
* Collection to display
*/
public static String collectionToCommaDelimitedString(Collection coll) {
return collectionToDelimitedString(coll, ",");
}
}
|