Author: fdesbois Date: 2010-04-08 10:51:45 +0200 (Thu, 08 Apr 2010) New Revision: 1818 Log: join : Change Type Collection in Iterable more generic + String in ? for method with no ToString contract 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 15:25:19 UTC (rev 1817) +++ trunk/src/main/java/org/nuiton/util/StringUtil.java 2010-04-08 08:51:45 UTC (rev 1818) @@ -36,10 +36,8 @@ 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; /** * Classe contenant un ensemle de methode static utiles pour la manipulation des @@ -121,23 +119,24 @@ } /** - * Used to concat a {@code list} of String separated by {@code separator}. + * Used to concat an {@code iterable} of Object separated + * by {@code separator} using the toString() method of each object. * You can specify if the string must be trimmed or not. * - * @param list to treate + * @param iterable Iterable with objects 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, + static public String join(Iterable<?> iterable, String separator, boolean trim) { - String result = join(list, null, separator, trim); + String result = join(iterable, null, separator, trim); return result; } /** - * Used to concat a {@code list} of object {@code <O>} separated by + * Used to concat an {@code iterable} 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 @@ -145,21 +144,26 @@ * in the {@code list} will be ignored. * * @param <O> type of object in the list - * @param list of object to treate + * @param iterable Iterable with objects 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. + * @throws NullPointerException */ - static public <O> String join(Collection<O> list, ToString<O> ts, - String separator, boolean trim) { + static public <O> String join(Iterable<O> iterable, ToString<O> ts, + String separator, boolean trim) throws NullPointerException { + if (iterable == null) { + throw new NullPointerException("null iterable can't be used" + + " to join the elements with " + separator); + } // Do nothing for an empty list - if (list.isEmpty()) { + if (!iterable.iterator().hasNext()) { return ""; } StringBuilder builder = new StringBuilder(); - for (O o : list) { + for (O o : iterable) { // Ignore the null object in the list if (o == null) { continue;
participants (1)
-
fdesbois@users.nuiton.org