in empire-db/src/main/java/org/apache/empire/commons/StringUtils.java [495:544]
public static String listToString(Collection<?> list, String template, String defItemValue, ObjectStringifier stringifier)
{
if (list==null || list.isEmpty())
return null;
// check
int tbeg = (template!=null ? template.indexOf(TEMPLATE_SEP_CHAR) : -1);
if (list.size()>1 || tbeg>0)
{ // build the list
int tend =(tbeg>=0 ? template.lastIndexOf(TEMPLATE_SEP_CHAR) : -1);
String separator = ((tbeg>0) ? (tend>tbeg ? template.substring(tbeg+1, tend) : DEFAULT_ITEM_SEPARATOR) : template);
// create StringBuilder
boolean ignoreEmpty = stringifier.isIgnoreEmpty(separator);
int bufferLen = estimateListBufferSize(list, stringifier, length(separator), length(defItemValue), ignoreEmpty);
if (template!=null && template.length()>separator.length())
{ // template extra
if (tbeg>0)
bufferLen += tbeg;
if (tend>0)
bufferLen += (template.length()-tend-1);
}
StringBuilder buf = new StringBuilder(bufferLen);
if (tend>0)
buf.append(template.substring(0, tbeg)); // add template prefix
boolean isEmpty = true;
boolean hasValue = false;
for (Object item : list)
{ // append separator
if (hasValue && separator!=null)
buf.append(separator);
// append value
String value = stringifier.toString(item, template, defItemValue);
hasValue = (value!=null && !(ignoreEmpty && StringUtils.isEmpty(value)));
isEmpty &= !hasValue;
if (hasValue)
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)); // add template suffix
if (buf.length()!=bufferLen)
log.debug("estimateListBufferSize returned {} but string length is {}", bufferLen, buf.length());
return buf.toString();
}
// Only one member
String value = stringifier.toString(list.iterator().next(), template, defItemValue);
if (stringifier.isIgnoreEmpty(template) && StringUtils.isEmpty(value))
return defItemValue;
return value;
}