Main.java Source code

Java tutorial

Introduction

Here is the source code for Main.java

Source

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

import java.util.AbstractList;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collection;

import java.util.Iterator;
import java.util.List;

public class Main {
    /**
     * Same as {@link #toString(Collection,String) toString}
     * separated by ", ".
     * 
     * @param c
     * @return
     */
    public static String toString(Collection c) {
        return toString(c, ", ");
    }

    public static String toString(Object[] objs) {
        return toString(Arrays.asList(objs));
    }

    public static String toString(Object[] objs, String separatedBy) {
        if (objs != null) {
            return toString(Arrays.asList(objs), separatedBy);
        } else {
            return "";
        }
    }

    /**
     * Returns a String representation of this collection.
     * The items are converted to strings using String.format(frmtPattern).
     * If frmtPattern is given, the items will be formatted as
     * String.format(Object, frmtPattern), else it will use String.valueOf(Object).
     * If separatedby is given, the items will be separated by that string.
     *
     * @param c
     * @param separatedBy
     * @return
     */
    public static String toString(Collection c, String separatedBy/*, String frmtPattern*/) {

        if (c == null || c.size() == 0)
            return "";

        //        boolean usePattern = frmtPattern != null;
        StringBuffer sb = new StringBuffer();
        for (Iterator itr = c.iterator(); itr.hasNext();) {
            Object o = itr.next();
            //            String s = usePattern ? String.format(frmtPattern, o) : String.valueOf(o);
            String s = String.valueOf(o);
            sb.append(s);
            if (separatedBy != null && itr.hasNext()) {
                sb.append(separatedBy);
            }
        }

        return sb.toString();
    }

    /**
     * takes an int array and returns a List of Integer backed by the array
     * @param a
     * @return
     */
    public static List<Integer> asList(final int[] a) {
        return new AbstractList<Integer>() {
            public Integer get(int i) {
                return a[i];
            }

            // Throws NullPointerException if val == null
            public Integer set(int i, Integer val) {
                Integer oldVal = a[i];
                a[i] = val;
                return oldVal;
            }

            public int size() {
                return a.length;
            }
        };
    }

    /**
     * takes a float array and returns a List of Float backed by the array
     * @param a
     * @return
     */
    public static List<Float> asList(final float[] a) {
        return new AbstractList<Float>() {
            public Float get(int i) {
                return a[i];
            }

            // Throws NullPointerException if val == null
            public Float set(int i, Integer val) {
                Float oldVal = a[i];
                a[i] = val;
                return oldVal;
            }

            public int size() {
                return a.length;
            }
        };
    }

    public static <T> List<T> asList(T... items) {

        ArrayList<T> list = new ArrayList<T>(items.length);
        if (items != null) {
            for (T item : items) {
                list.add(item);
            }
        }
        return list;
    }
}