Java Comma Separated List parseCommaList(String nameCommaList)

Here you can find the source of parseCommaList(String nameCommaList)

Description

Parses a comma-separated list.

License

Open Source License

Parameter

Parameter Description
nameCommaList List of names separated by commas

Return

List of names

Declaration

public static List<String> parseCommaList(String nameCommaList) 

Method Source Code

//package com.java2s;
/*//from  w w  w  .  j a  v a2 s .c o  m
 // This software is subject to the terms of the Eclipse Public License v1.0
 // Agreement, available at the following URL:
 // http://www.eclipse.org/legal/epl-v10.html.
 // You must accept the terms of that agreement to use this software.
 //
 // Copyright (C) 2001-2005 Julian Hyde
 // Copyright (C) 2005-2012 Pentaho and others
 // All Rights Reserved.
 */

import java.util.*;

public class Main {
    /**
     * Parses a comma-separated list.
     *
     * <p>If a value contains a comma, escape it with a second comma. For
     * example, <code>parseCommaList("x,y,,z")</code> returns
     * <code>{"x", "y,z"}</code>.
     *
     * @param nameCommaList List of names separated by commas
     * @return List of names
     */
    public static List<String> parseCommaList(String nameCommaList) {
        if (nameCommaList.equals("")) {
            return Collections.emptyList();
        }
        if (nameCommaList.endsWith(",")) {
            // Special treatment for list ending in ",", because split ignores
            // entries after separator.
            final String zzz = "zzz";
            final List<String> list = parseCommaList(nameCommaList + zzz);
            String last = list.get(list.size() - 1);
            if (last.equals(zzz)) {
                list.remove(list.size() - 1);
            } else {
                list.set(list.size() - 1,
                        last.substring(0, last.length() - zzz.length()));
            }
            return list;
        }
        List<String> names = new ArrayList<String>();
        final String[] strings = nameCommaList.split(",");
        for (String string : strings) {
            final int count = names.size();
            if (count > 0 && names.get(count - 1).equals("")) {
                if (count == 1) {
                    if (string.equals("")) {
                        names.add("");
                    } else {
                        names.set(0, "," + string);
                    }
                } else {
                    names.set(count - 2, names.get(count - 2) + ","
                            + string);
                    names.remove(count - 1);
                }
            } else {
                names.add(string);
            }
        }
        return names;
    }

    /**
     * Returns true if two objects are equal, or are both null.
     *
     * @param s First object
     * @param t Second object
     * @return Whether objects are equal or both null
     */
    public static boolean equals(Object s, Object t) {
        if (s == t) {
            return true;
        }
        if (s == null || t == null) {
            return false;
        }
        return s.equals(t);
    }

    /**
     * Returns true if two strings are equal, or are both null.
     *
     * <p>The result is not affected by
     * {@link MondrianProperties#CaseSensitive the case sensitive option}; if
     * you wish to compare names, use {@link #equalName(String, String)}.
     */
    public static boolean equals(String s, String t) {
        return equals((Object) s, (Object) t);
    }
}

Related

  1. listToCommaSeparatedList(String prefix, List stringList)
  2. listToCommaSeparatedString(List list)
  3. listToString(List list, boolean doCommas)
  4. loadListFromStrings(final String commaDelimitedStrings, final boolean sortList)
  5. moveClasspathArgToEnvironmentVariable(List commandArgs, ProcessBuilder pb)
  6. parseRsfCommands(List rsfLines, List addItemCommands, List appendEntryCommands)
  7. resolveCommandParam(List params, String name, double defaultValue)
  8. splitCommandsToList(String row, String delimiter)
  9. splitListByComma(List hosts)