From a85c94dc2fabfa282dab1930f4d50eacee012c7f Mon Sep 17 00:00:00 2001 From: Jacques Le Roux Date: Mon, 14 Aug 2017 09:52:49 +0000 Subject: [PATCH] Implemented: New UtilMisc method collectionToString(...) building safely a String with the given collection and delimiter (OFBIZ-9397) The added method UtilMisc.collectionToString(Collection values, String delimiter) creates a String from the values of a collection, separated with the given delimiter. This is done in a safe way without a "null" literal. Thanks: Martin Becker git-svn-id: https://svn.apache.org/repos/asf/ofbiz/ofbiz-framework/trunk@1804970 13f79535-47bb-0310-9956-ffa450edef68 --- .../org/apache/ofbiz/base/util/UtilMisc.java | 23 +++++++++++++++++++ 1 file changed, 23 insertions(+) diff --git a/framework/base/src/main/java/org/apache/ofbiz/base/util/UtilMisc.java b/framework/base/src/main/java/org/apache/ofbiz/base/util/UtilMisc.java index 6fa8d15883..3903cf8665 100644 --- a/framework/base/src/main/java/org/apache/ofbiz/base/util/UtilMisc.java +++ b/framework/base/src/main/java/org/apache/ofbiz/base/util/UtilMisc.java @@ -264,6 +264,29 @@ public final class UtilMisc { } return theSet; } + + /** + * Generates a String from given values delimited by delimiter. + * + * @param values + * @param delimiter + * @return String + * @throws IOException + */ + public static String collectionToString(Collection values, String delimiter) { + if (UtilValidate.isEmpty(values)) { + return null; + } + if (delimiter == null) { + delimiter = ""; + } + StringBuilder out = new StringBuilder(); + + for (Object val : values) { + out.append(UtilFormatOut.safeToString(val)).append(delimiter); + } + return out.toString(); + } /** * Create a set from the passed objects.