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.ArrayList;
import java.util.Arrays;
import java.util.Collection;

import java.util.HashSet;

import java.util.List;

import java.util.Set;

public class Main {
    /**
     * @param ae
     * @return a set containing the given elements
     * @precondition ae != null
     * @postcondition result != null
     * @postcondition result.containsAll(Arrays.asList(ae))
     */
    public static <E> Set<E> asSet(E... ae) {
        final Set<E> result = new HashSet<E>(Arrays.asList(ae));

        assert result != null;
        assert result.containsAll(Arrays.asList(ae));
        return result;
    }

    /**
     * @todo check if this method is still necessary with Java 5.
     * @param ai
     * @return a List<Integer> containing the elements of the given int array.
     * @precondition ai != null
     * @postcondition result != null
     * @postcondition result.size() == ai.length
     */
    public static List<Integer> asList(int[] ai) {
        final List<Integer> result = new ArrayList<Integer>(ai.length);
        for (int i : ai) {
            result.add(i);
        }
        assert result.size() == ai.length;
        return result;
    }

    /**
     * @param at
     * @return a List containing the elements of the given array.
     * @precondition at != null
     * @postcondition result != null
     * @postcondition result.size() == at.length
     */
    public static <T> List<T> asList(T[] at) {
        final List<T> result = new ArrayList<T>(at.length);
        for (T t : at)
            result.add(t);
        assert result.size() == at.length;
        return result;
    }

    /**
     * Returns a new list with {@code t1} as first element followed by
     * all elements of {@code ts}.
     */
    public static <T> List<T> asList(T t1, T... ts) {
        List<T> list = new ArrayList<T>(1 + ts.length);
        list.add(t1);
        for (T t : ts)
            list.add(t);
        return list;
    }

    /**
     * @param coll
     * @postcondition (coll == null) --> (result == 0)
     * @postcondition (coll != null) --> (result == coll.size())
     */
    public static int size(Collection<?> coll) {
        return (coll == null) ? 0 : coll.size();
    }
}