Main.java Source code

Java tutorial

Introduction

Here is the source code for Main.java

Source

//package com.java2s;
/**
 * Aptana Studio
 * Copyright (c) 2005-2011 by Appcelerator, Inc. All Rights Reserved.
 * Licensed under the terms of the GNU Public License (GPL) v3 (with exceptions).
 * Please see the license.html included with this distribution for details.
 * Any modifications to this file must keep this entire header intact.
 */

import java.util.ArrayList;

import java.util.List;

public class Main {
    /**
     * Convert a vararg list of items into a List. An empty list is returned if items is null
     * 
     * @param <T>
     *            Any type of object
     * @param items
     *            A variable length list of items of type T
     * @return Returns a new ArrayList<T> or an empty list
     */
    public static final <T> List<T> newList(T... items) {
        return addToList(new ArrayList<T>(items != null ? items.length : 0), items);
    }

    /**
     * Add a varargs list of items to the specified list. If the list or items array are null, then no action is
     * performed. Note that the destination list has no requirements other than it must be a List of the source item's
     * type. This allows the destination to be used, for example, as an accumulator.<br>
     * <br>
     * Note that this method is not thread safe. Users of this method will need to maintain type safety against the
     * list.
     * 
     * @param list
     *            A list to which items will be added
     * @param items
     *            A list of items to add
     */
    public static final <T, U extends T> List<T> addToList(List<T> list, U... items) {
        if (list != null && items != null) {
            for (int i = 0; i < items.length; i++) {
                list.add(items[i]);
            }
            if (list instanceof ArrayList) {
                ((ArrayList<T>) list).trimToSize();
            }
        }

        return list;
    }

    /**
     * Add a varargs list of items to the specified list. If the list or items array are null, then no action is
     * performed. Note that the destination list has no requirements other than it must be a List of the source item's
     * type. This allows the destination to be used, for example, as an accumulator.<br>
     * <br>
     * Note that this method is not thread safe. Users of this method will need to maintain type safety against the
     * list.
     * 
     * @param list
     *            A list to which items will be added
     * @param items
     *            A list of items to add
     */
    public static final <T, U extends T> List<T> addToList(List<T> list, List<U> items) {
        if (list != null && items != null) {
            list.addAll(items);
        }

        return list;
    }
}