Main.java Source code

Java tutorial

Introduction

Here is the source code for Main.java

Source

//package com.java2s;
/*******************************************************************************
 * Copyright (c) 2004, 2007 Boeing.
 * All rights reserved. This program and the accompanying materials
 * are made available under the terms of the Eclipse Public License v1.0
 * which accompanies this distribution, and is available at
 * http://www.eclipse.org/legal/epl-v10.html
 *
 * Contributors:
 *     Boeing - initial API and implementation
 *******************************************************************************/

import java.util.ArrayList;

import java.util.Collection;

import java.util.Iterator;

public class Main {
    /**
     * An flexible alternative for converting a Collection to a String.
     *
     * @param c The Collection to convert to a String
     * @param start The String to place at the beginning of the returned String
     * @param separator The String to place in between elements of the Collection c.
     * @param end The String to place at the end of the returned String
     * @return A String which starts with 'start', followed by the elements in the Collection c separated by 'separator',
     * ending with 'end'.
     */
    @SuppressWarnings("rawtypes")
    public static String toString(Collection c, String start, String separator, String end) {
        Iterator i = c.iterator();
        StringBuilder myString = new StringBuilder();

        if (start != null) {
            myString.append(start);
        }

        boolean first = true;
        while (i.hasNext()) {
            if (!first) {
                myString.append(separator);
            }
            myString.append(i.next().toString());
            first = false;
        }

        if (end != null) {
            myString.append(end);
        }

        return myString.toString();
    }

    public static String toString(String separator, Object... objects) {
        Collection<Object> objectsCol = new ArrayList<Object>(objects.length);
        for (Object obj : objects) {
            objectsCol.add(obj);
        }
        return toString(objectsCol, null, separator, null);
    }

    @SuppressWarnings("rawtypes")
    public static String toString(String separator, Collection c) {
        return toString(c, null, separator, null);
    }
}