Java Comma Separated List commaSeparate(List a_list)

Here you can find the source of commaSeparate(List a_list)

Description

Comma separate all the items in the list.

License

Open Source License

Parameter

Parameter Description
a_list is a list of items to be combined contatenated into a comma separated list.

Return

the string of comma separated items

Declaration

public static String commaSeparate(List a_list) 

Method Source Code

//package com.java2s;
/*/*ww w  .ja  v  a2s  .c o m*/
 * @(#) StringUtils.java Jul 20, 2005
 * Copyright 2005 Frequency Marketing, Inc. All rights reserved.
 * Frequency Marketing, Inc. PROPRIETARY/CONFIDENTIAL. Use is subject to license terms.
 */

import java.util.List;

public class Main {
    /**
     * Comma separate all the items in the list. Exclude any of the list entries
     * that are null.
     * @param a_list is a list of items to be combined contatenated into a comma
     *        separated list.
     * @return the string of comma separated items
     */
    public static String commaSeparate(List a_list) {
        return delimitList(a_list, 0, a_list.size(), ",");
    }

    /**
     * Comma separate all the items in the list. Exclude any of the list entries
     * that are null.
     * @param a_list the list to be formatted
     * @param a_startIndex the index into the list to start
     * @param a_length the number of items in the list to format
     * @return the string of comman separated items
     */
    public static String commaSeparate(List a_list, int a_startIndex, int a_length) {
        return delimitList(a_list, a_startIndex, a_length, ",");
    }

    /**
     * Separeate all items in a list defining a delimiter.
     * @param a_list list of items to be combined
     * @param a_delimiter delimiter to separate list elements
     * @return the string of delimited items
     */
    public static String delimitList(List a_list, String a_delimiter) {
        return delimitList(a_list, 0, a_list.size(), a_delimiter);
    }

    /**
     * Comma separate all the items in the list. Exclude any of the list entries
     * that are null.
     * @param a_list the list to be formatted
     * @param a_startIndex the index into the list to start
     * @param a_length the number of items in the list to format
     * @param a_delimiter defined field delimiter
     * @return the string of comman separated items
     */
    public static String delimitList(List a_list, int a_startIndex, int a_length, String a_delimiter) {
        StringBuffer text = new StringBuffer();

        Object entry;
        boolean foundFirst = false;
        int size = a_list.size();

        if (a_length < 0) {
            throw new IndexOutOfBoundsException("Nagitive length to copy");
        } else if (a_startIndex + a_length > size) {
            throw new IndexOutOfBoundsException("Subsection too long for copy");
        }

        for (int index = 0; index < a_length; index++) {
            entry = a_list.get(a_startIndex + index);
            if (null != entry) {
                if (foundFirst) {
                    text.append(a_delimiter);
                }
                text.append(entry);
                foundFirst = true;
            }
        }
        return text.toString();
    }
}

Related

  1. commaDelimitedStringToList(String str)
  2. commaDelimitedToList(String commaDelimited)
  3. commalist(List l)
  4. commalist(List l)
  5. commandToString(List args)
  6. commaSeparatedClassNames(final List objects)
  7. commaSeparatedList(Collection c)
  8. commaSeparatedList(Object... values)
  9. commaSeparatedList(String string)

  10. HOME | Copyright © www.java2s.com 2016