EMPIREDB-410
StringUtils arrayToString edge case
diff --git a/empire-db/src/main/java/org/apache/empire/commons/StringUtils.java b/empire-db/src/main/java/org/apache/empire/commons/StringUtils.java
index 09528dc..c1268eb 100644
--- a/empire-db/src/main/java/org/apache/empire/commons/StringUtils.java
+++ b/empire-db/src/main/java/org/apache/empire/commons/StringUtils.java
@@ -207,7 +207,8 @@
String separator = ((tbeg>0) ? (tend>tbeg ? template.substring(tbeg+1, tend) : DEFAULT_ITEM_SEPARATOR) : template);
StringBuilder buf = new StringBuilder();
if (tend>0)
- buf.append(template.substring(0, tbeg));
+ buf.append(template.substring(0, tbeg)); // add template prefix
+ boolean isEmpty = true;
boolean hasValue = false;
for (int i = 0; i < array.length; i++)
{ // append separator
@@ -216,11 +217,14 @@
// append value
String value = toString(array[i], template, defItemValue);
hasValue = !(ignoreEmpty && StringUtils.isEmpty(value));
+ isEmpty &= !hasValue;
if (hasValue && value!=null)
buf.append(value);
}
+ if (hasValue==false && !isEmpty && separator!=null)
+ buf.setLength(buf.length()-separator.length()); // remove last separator
if (tend>0)
- buf.append(template.substring(tend+1));
+ buf.append(template.substring(tend+1)); // add template suffix
return buf.toString();
}
// Only one member
@@ -276,7 +280,8 @@
String separator = ((tbeg>0) ? (tend>tbeg ? template.substring(tbeg+1, tend) : DEFAULT_ITEM_SEPARATOR) : template);
StringBuilder buf = new StringBuilder();
if (tend>0)
- buf.append(template.substring(0, tbeg));
+ buf.append(template.substring(0, tbeg)); // add template prefix
+ boolean isEmpty = true;
boolean hasValue = false;
for (Object item : list)
{ // append separator
@@ -285,11 +290,14 @@
// append value
String value = toString(item, template, defItemValue);
hasValue = !(ignoreEmpty && StringUtils.isEmpty(value));
+ isEmpty &= !hasValue;
if (hasValue && value!=null)
buf.append(value);
}
+ if (hasValue==false && !isEmpty && separator!=null)
+ buf.setLength(buf.length()-separator.length()); // remove last separator
if (tend>0)
- buf.append(template.substring(tend+1));
+ buf.append(template.substring(tend+1)); // add template suffix
return buf.toString();
}
// Only one member
diff --git a/empire-db/src/test/java/org/apache/empire/commons/StringUtilsTest.java b/empire-db/src/test/java/org/apache/empire/commons/StringUtilsTest.java
index ac4bcc5..be924ee 100644
--- a/empire-db/src/test/java/org/apache/empire/commons/StringUtilsTest.java
+++ b/empire-db/src/test/java/org/apache/empire/commons/StringUtilsTest.java
@@ -155,6 +155,7 @@
assertEquals("null/null", StringUtils.arrayToString(new String[]{null, null} , "/", StringUtils.NULL));
assertEquals("null", StringUtils.arrayToString(new String[]{null} , "/", StringUtils.NULL));
// Special case with SPACE
+ assertEquals("Hello", StringUtils.arrayToString(new Object[]{"Hello","",null," "," "}, StringUtils.SPACE));
assertEquals("Hello World", StringUtils.arrayToString(new Object[]{"Hello","",null," ","World"}, StringUtils.SPACE));
assertEquals("Hello World", StringUtils.toString(new Object[]{"Hello","",null," ","World"}, StringUtils.SPACE, null));
}