Author: fdesbois Date: 2010-04-07 17:25:19 +0200 (Wed, 07 Apr 2010) New Revision: 1817 Log: Add method to join a list of object (concat object as string with a separator) Modified: trunk/src/main/java/org/nuiton/util/StringUtil.java Modified: trunk/src/main/java/org/nuiton/util/StringUtil.java =================================================================== --- trunk/src/main/java/org/nuiton/util/StringUtil.java 2010-04-07 12:13:11 UTC (rev 1816) +++ trunk/src/main/java/org/nuiton/util/StringUtil.java 2010-04-07 15:25:19 UTC (rev 1817) @@ -36,6 +36,7 @@ import java.text.ParseException; import java.util.ArrayList; import java.util.Arrays; +import java.util.Collection; import java.util.Date; import java.util.List; import java.util.Locale; @@ -101,9 +102,89 @@ return result; } + /** + * Contract to use in {@link #join(java.util.Collection, org.nuiton.util.StringUtil.ToString, java.lang.String, boolean) } + * method. This will provide a toString method to convert an object in a + * string. + * + * @param <O> type of object manipulated + */ + static public interface ToString<O> { + + /** + * Convert an object o in a string. + * + * @param o to convert + * @return the string for this object o + */ + String toString(O o); + } + + /** + * Used to concat a {@code list} of String separated by {@code separator}. + * You can specify if the string must be trimmed or not. + * + * @param list to treate + * @param separator to used + * @param trim if each string must be trim + * @return the String chain of all elements separated by separator, never + * return null, will return an empty String for an empty list. + */ + static public String join(Collection<String> list, String separator, + boolean trim) { + String result = join(list, null, separator, trim); + return result; + } + + /** + * Used to concat a {@code list} of object {@code <O>} separated by + * {@code separator}. This method need a {@code ts} contract to + * call on each object. The ToString can be null to use directly the + * toString() method on the object. The {@code trim} boolean is used + * to specify if each string object has to be trimmed. The null elements + * in the {@code list} will be ignored. + * + * @param <O> type of object in the list + * @param list of object to treate + * @param ts used to specify how the object is converted in String + * @param separator to used between each object string + * @param trim if trim() method need to by apply on each object string + * @return the String chain of all elements separated by separator, never + * return null, will return an empty String for an empty list. + */ + static public <O> String join(Collection<O> list, ToString<O> ts, + String separator, boolean trim) { + // Do nothing for an empty list + if (list.isEmpty()) { + return ""; + } + StringBuilder builder = new StringBuilder(); + for (O o : list) { + // Ignore the null object in the list + if (o == null) { + continue; + } + String str = null; + // Use ToString contract from argument + if (ts != null) { + str = ts.toString(o); + // Or call toString() method directly on object + } else { + str = o.toString(); + } + // Apply trim if needed + if (trim) { + str = str.trim(); + } + builder.append(separator).append(str); + } + // Suppress the first separator at beginning of the chain + String result = builder.substring(separator.length()); + return result; + } + static public String substring(String s, int begin) { - String result; - result = substring(s, begin, s.length()); + String result = substring(s, begin, s.length()); return result; }
participants (1)
-
fdesbois@users.nuiton.org