Java CSV String Create getCSVPhrase(Collection objects)

Here you can find the source of getCSVPhrase(Collection objects)

Description

Creates CSV phrase from objects

License

Open Source License

Parameter

Parameter Description
objects objects to be returned in phrase

Return

a string, with each object separated by ,

Declaration

public static String getCSVPhrase(Collection<?> objects) 

Method Source Code

//package com.java2s;
/**/*from  w w w.  ja  v a2 s . c  o m*/
JEM, the BEE - Job Entry Manager, the Batch Execution Environment
Copyright (C) 2012-2015   Marco "Fuzzo" Cuccato
This program is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
any later version.
    
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
GNU General Public License for more details.
    
You should have received a copy of the GNU General Public License
along with this program.  If not, see <http://www.gnu.org/licenses/>.
*/

import java.util.Collection;

public class Main {
    /**
     * 
     */
    public static final char COMMA_CHAR = ',';
    /**
     * 
     */
    public static final String NULL = "null";

    /**
     * Creates CSV phrase from objects
     * @param objects objects to be returned in phrase
     * @return a string, with each object separated by <code>,</code>
     */
    public static String getCSVPhrase(Collection<?> objects) {
        return getCSVPhrase(objects, COMMA_CHAR);
    }

    /**
     * Creates CSV phrase from objects
     * @param objects objects to be returned in phrase
     * @param separator the char to be used as separator
     * @return a string, with each object separated by separator 
     */
    public static String getCSVPhrase(Collection<?> objects, char separator) {
        return getCSVPhrase(objects, String.valueOf(separator));
    }

    /**
     * Creates CSV phrase from objects
     * @param objects objects to be returned in phrase
     * @param separator the string to be used as separator
     * @return a string, with each object separated by separator 
     */
    public static String getCSVPhrase(Collection<?> objects, String separator) {
        StringBuilder s = new StringBuilder();
        if (!objects.isEmpty()) {
            for (Object o : objects) {
                if (o != null) {
                    s.append(o.toString());
                } else {
                    s.append(NULL);
                }
                s.append(separator);
            }
            s.setLength(s.length() - separator.length());
        }
        return s.toString();
    }

    /**
     * Creates CSV phrase from objects
     * @param objects objects to be returned in phrase
     * @return a string, with each object separated by <code>,</code>
     */
    public static String getCSVPhrase(Object[] objects) {
        return getCSVPhrase(objects, COMMA_CHAR);
    }

    /**
     * Creates CSV phrase from objects
     * @param objects objects to be returned in phrase
     * @param separator the char to be used as separator
     * @return a string, with each object separated by separator 
     */
    public static String getCSVPhrase(Object[] objects, char separator) {
        return getCSVPhrase(objects, String.valueOf(separator));
    }

    /**
     * Creates CSV phrase from objects
     * @param objects objects to be returned in phrase
     * @param separator the string to be used as separator
     * @return a string, with each object separated by separator 
     */
    public static String getCSVPhrase(Object[] objects, String separator) {
        StringBuilder s = new StringBuilder();
        if (objects.length > 0) {
            for (Object o : objects) {
                if (o != null) {
                    s.append(o.toString());
                } else {
                    s.append(NULL);
                }
                s.append(separator);
            }
            s.setLength(s.length() - separator.length());
        }
        return s.toString();
    }
}

Related

  1. csv(String separator, String... elements)
  2. getCSV(Collection coll)
  3. getCSV(Collection values)
  4. getCsv(Collection collection)
  5. toCSV(int[] a)
  6. toCsv(Object[] arr, String format)
  7. toCsv(String value)
  8. toCSV3_2(double[][][] array, int index1, int index3)