public static String arrayToString()

in empire-db/src/main/java/org/apache/empire/commons/StringUtils.java [360:409]


    public static String arrayToString(Object[] array, String template, String defItemValue, ObjectStringifier stringifier)
    {
        if (array==null || array.length==0)
            return null;
        // check 
        int tbeg = (template!=null ? template.indexOf(TEMPLATE_SEP_CHAR) : -1);
        if (array.length>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 = estimateArrayBufferSize(array, 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 (int i = 0; i < array.length; i++)
            {   // append separator
                if (hasValue && separator!=null)
                    buf.append(separator);
                // append value
                String value = stringifier.toString(array[i], 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("estimateArrayBufferSize returned {} but string length is {}", bufferLen, buf.length());
            return buf.toString();
        }
        // Only one member
        String value = stringifier.toString(array[0], template, defItemValue);
        if (stringifier.isIgnoreEmpty(template) && StringUtils.isEmpty(value))
            return defItemValue;
        return value;
    }