Main.java Source code

Java tutorial

Introduction

Here is the source code for Main.java

Source

//package com.java2s;

import java.util.Collection;

public class Main {
    public static final String EMPTY = "";
    private static final String DELIMITER = ", ";

    /**
     * Transforms a collection of Integers into a comma delimited String. If the
     * given collection of elements are null or is empty, an empty String is
     * returned.
     *
     * @param elements the collection of Integers
     * @return a comma delimited String.
     */
    public static String getCommaDelimitedString(Collection<?> elements) {
        final StringBuilder builder = new StringBuilder();

        if (elements != null && !elements.isEmpty()) {
            for (Object element : elements) {
                builder.append(element.toString()).append(DELIMITER);
            }

            return builder.substring(0, builder.length() - DELIMITER.length());
        }

        return builder.toString();
    }

    /**
     * Transforms a collection of Integers into a comma delimited String. If the
     * given collection of elements are null or is empty, an empty String is
     * returned.
     *
     * @param delimitPrefix whether to prefix the string with a delimiter.
     * @param delimitSuffix whether to suffix the string with a delimiter.
     * @param elements      the collection of Integers
     * @return a comma delimited String.
     */
    public static String getCommaDelimitedString(Collection<?> elements, boolean delimitPrefix,
            boolean delimitSuffix) {
        final StringBuilder builder = new StringBuilder();

        if (elements != null && !elements.isEmpty()) {
            if (delimitPrefix) {
                builder.append(DELIMITER);
            }

            builder.append(getCommaDelimitedString(elements));

            if (delimitSuffix) {
                builder.append(DELIMITER);
            }
        }

        return builder.toString();
    }

    /**
     * Null-safe method for writing the items of a string array out as a string
     * separated by the given char separator.
     *
     * @param array     the array.
     * @param separator the separator of the array items.
     * @return a string.
     */
    public static String toString(String[] array, String separator) {
        StringBuilder builder = new StringBuilder();

        if (array != null && array.length > 0) {
            for (String string : array) {
                builder.append(string).append(separator);
            }

            builder.deleteCharAt(builder.length() - 1);
        }

        return builder.toString();
    }

    /**
     * Returns the string representation of the object, or null if the object is
     * null.
     *
     * @param object the object.
     * @return the string representation.
     */
    public static String toString(Object object) {
        return object != null ? object.toString() : null;
    }

    /**
     * Gets the sub string of the given string. If the beginIndex is larger than
     * the length of the string, the empty string is returned. If the beginIndex +
     * the length is larger than the length of the string, the part of the string
     * following the beginIndex is returned. Method is out-of-range safe.
     *
     * @param string     the string.
     * @param beginIndex the zero-based begin index.
     * @param length     the length of the sub string starting at the begin index.
     * @return the sub string of the given string.
     */
    public static String subString(String string, int beginIndex, int length) {
        final int endIndex = beginIndex + length;

        if (beginIndex >= string.length()) {
            return EMPTY;
        }

        if (endIndex > string.length()) {
            return string.substring(beginIndex, string.length());
        }

        return string.substring(beginIndex, endIndex);
    }
}