Java Array Join joinWithPrefixes(String[] prefixes, String name, String sep)

Here you can find the source of joinWithPrefixes(String[] prefixes, String name, String sep)

Description

Returns the name, joined to prefixes, if any, using the default #PACKAGE_SEP if none supplied.

License

Open Source License

Parameter

Parameter Description
prefixes String array of prefix labels.
name Name to prefix with prefixes
sep Separator, or <code>null</code> to use package separator.

Return

Concatenated string of prefixes and name.

Declaration

public static String joinWithPrefixes(String[] prefixes, String name, String sep) 

Method Source Code

//package com.java2s;
//License from project: Open Source License 

import java.util.Collection;

public class Main {
    /** Default package name separator, "::". */
    public static final String PACKAGE_SEP = "::";

    /**/*w  w  w. j  ava2s.c om*/
     * Returns the name, joined to prefixes, if any, using the default
     * {@link #PACKAGE_SEP} if none supplied.
     * 
     * @param prefixes  String array of prefix labels.
     * @param name      Name to prefix with prefixes
     * @param sep       Separator, or <code>null</code> to use package separator.
     * @return  Concatenated string of prefixes and name.
     */
    public static String joinWithPrefixes(String[] prefixes, String name, String sep) {
        if (isVarArgsEmpty(prefixes)) {
            return name;
        } else { // return named join to prefix
            if (sep == null) {
                sep = PACKAGE_SEP;
            }
            return join(prefixes, sep) + sep + name;
        }
    }

    /**
     * Returns whether the supplied var args is empty, meaning <code>null</code>,
     * or zero-length, or has a single element of length zero!
     * 
     * @param varArgs  variable argument to check.
     * @return  <code>true</code> if var args is considered empty, <code>false</code> otherwise.
     */
    public static boolean isVarArgsEmpty(String... varArgs) {
        boolean rv = false;
        if (varArgs == null || varArgs.length == 0
                || (varArgs.length == 1 && (varArgs[0] == null || varArgs[0].length() == 0))) {
            rv = true;
        }
        return rv;
    }

    /**
     * Join a collection with a separator string
     */
    public static String join(Collection<?> objs, String sep) {
        StringBuilder sb = new StringBuilder();
        boolean first = true;

        for (Object obj : objs) {
            if (!first) {
                sb.append(sep);
            }
            sb.append(obj.toString());
            first = false;
        }

        return sb.toString();
    }

    /**
     * Join an array with a separator string
     */
    public static <T> String join(T[] array, String sep) {
        StringBuilder sb = new StringBuilder();
        boolean first = true;

        for (T e : array) {
            if (!first) {
                sb.append(sep);
            }
            sb.append(e.toString());
            first = false;
        }

        return sb.toString();
    }
}

Related

  1. joinString(String[] array)
  2. joinString(String[] str, String delimiter)
  3. joinStringArray(String[] array1, String[] array2)
  4. joinStrings(String[] strings, String separator)
  5. joinTokens(String tokens[], String pad)
  6. joinWithSpaces(final String[] cmds)
  7. stringJoin(Object[] array, String separator)