Android Collection Join suffixAndJoin(String suffix, String delimiter, Collection values)

Here you can find the source of suffixAndJoin(String suffix, String delimiter, Collection values)

Description

suffix And Join

Declaration

public static <T> String suffixAndJoin(String suffix, String delimiter,
            Collection<T> values) 

Method Source Code

//package com.java2s;
import java.util.*;

public class Main {
    public static <T> String suffixAndJoin(String suffix, String delimiter,
            Collection<T> values) {
        String result = join(suffix + delimiter, values);
        return isEmpty(result) ? result : result + suffix;
    }//ww w.  j a  va2 s.c  o  m

    public static <T> String join(String separator,
            Collection<T> valuesOriginal) {
        if (valuesOriginal == null)
            return "";
        ArrayList<T> values = new ArrayList<T>(valuesOriginal);
        removeAllNullEntries(values);
        if (separator == null) {
            separator = "";
        }
        if (values.isEmpty()) {
            return "";
        }
        String joinedString = "";
        for (T value : values) {
            String valueString = value.toString();
            if (valueString != null && !isEmpty(valueString)) {
                joinedString += valueString + separator;
            }
        }
        return isEmpty(joinedString) ? "" : joinedString.substring(0,
                joinedString.length() - separator.length());
    }

    public static <T> String join(String separator, T... values) {
        return join(separator, asList(values));
    }

    private static boolean isEmpty(String valueString) {
        return "".equals(valueString.trim());
    }

    public static void removeAllNullEntries(Collection values) {
        while (values.remove(null))
            ;
    }

    public static <T> ArrayList<T> asList(T... values) {
        ArrayList<T> list = new ArrayList<T>();
        for (T value : values) {
            if (value != null)
                list.add(value);
        }
        return list;
    }
}

Related

  1. join(Collection... collections)
  2. join(String conjunction, Collection... collections)
  3. toString(Collection collection, String separator)
  4. join(Collection collection, String s)
  5. join(Collection strings, String sep)
  6. join(Collection strings, String sep)