Main.java Source code

Java tutorial

Introduction

Here is the source code for Main.java

Source

//package com.java2s;
//it under the terms of the GNU Affero General Public License as published by

import java.util.Iterator;

public class Main {
    /**
     * writes the objects in iterable to the returned String, separating them with the given separator.
     * String.valueOf() is used to write each element of the collection.
     * @param iterable
     * @param sSeparator if non-empty, is written between each two elements (that is, not before the first one, and not after the last one)
     * @return a String containing the String representations of the given Collection's elements.
     * @precondition iterable != null
     * @postcondition result != null
     */
    public static String getSeparatedList(Iterable<?> iterable, String sSeparator) {
        return getSeparatedList(iterable, sSeparator, null, null);
    }

    /**
     * writes the objects in iterable to the returned String, separating them with the given separator.
     * String.valueOf() is used to write each element of the collection.
     * @param iterable
     * @param sSeparator if non-empty, is written between each two elements (that is, not before the first one, and not after the last one)
     * @param sPrefix if non-empty, is prepended before each element.
     * @param sSuffix if non-empty, is appended after each element.
     * @return a String containing the String representations of the given Collection's elements.
     * @precondition iterable != null
     * @postcondition result != null
     */
    public static String getSeparatedList(Iterable<?> iterable, String sSeparator, String sPrefix, String sSuffix) {
        final StringBuilder sb = new StringBuilder();
        for (Iterator<?> iter = iterable.iterator(); iter.hasNext();) {
            // prefix:
            if (sPrefix != null) {
                sb.append(sPrefix);
            }
            // the element itself:
            sb.append(iter.next());
            // suffix:
            if (sSuffix != null) {
                sb.append(sSuffix);
            }
            // separator:
            if (sSeparator != null && iter.hasNext()) {
                sb.append(sSeparator);
            }
        }
        return sb.toString();
    }
}