集册 Java实例教程 从对象创建CSV短语

从对象创建CSV短语

欢马劈雪     最近更新时间:2020-01-02 10:19:05

486
从对象创建CSV短语

/**

    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/>.

 */

//package com.nowjava;// from 时代Java

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);

    }//from 时 代 J a v a - nowjava.com


    /**

     * 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));

    }


    
展开阅读全文