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 = "";

    /**
     * Transforms a collection of strings into a comma delimited string, where
     * each component get single-quoted.
     *
     * @param elements the collection of Integers
     * @return a comma delimited String.
     */
    public static String getQuotedCommaDelimitedString(Collection<String> elements) {
        if (elements != null && elements.size() > 0) {
            final StringBuffer buffer = new StringBuffer();

            for (String element : elements) {
                buffer.append("'").append(element.toString()).append("', ");
            }

            return buffer.substring(0, buffer.length() - ", ".length());
        }

        return null;
    }

    /**
     * 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);
    }
}