class CsvUtil {

    public static String toCsvString(Object...elements) {
        if (ArrayUtil.isEmpty(elements)) {
            return null;
        }
        StringBuilder line = new StringBuilder();
        for (int i = 0, last = elements.length - 1; i <= last; i++) {
            if (elements[i] == null && i != last) {
                line.append(FIELD_SEPARATOR);
                continue;
            }
            // FIXME
            if (elements[i] == null) {
                continue;
            }
            String field = elements[i].toString();

            // check for special cases
            int ndx = field.indexOf(FIELD_SEPARATOR);
            ndx = (ndx == -1) ? field.indexOf(FIELD_QUOTE) : ndx;

            if (ndx == -1 && (field.startsWith(StringPool.Symbol.SPACE) || field.endsWith(StringPool.Symbol.SPACE))) {
                ndx = 1;
            }
            ndx = (ndx == -1) ? StringUtil.indexOfChars(field, SPECIAL_CHARS) : ndx;

            // add field
            if (ndx != -1) {
                line.append(FIELD_QUOTE);
            }
            line.append(StringUtil.replace(field, StringPool.Symbol.DOUBLE_QUOTE, DOUBLE_QUOTE));
            if (ndx != -1) {
                line.append(FIELD_QUOTE);
            }

            // last
            if (i != last) {
                line.append(FIELD_SEPARATOR);
            }
        }
        return line.toString();
    }

}