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.Collection;

import java.util.List;
import java.util.Map;

public class Main {
    /**
     * Converts a list to a new copy of array based on the start index and end index.
     * 
     * @param list
     * @param startIndex
     * @param endIndex
     * @return
     */
    @SuppressWarnings({ "unchecked", "cast" })
    public static final <T> T[] toArray(List<T> list, int startIndex, int endIndex) {
        if (isEmpty(list)) {
            return (T[]) list.toArray();
        }
        List<T> subList = list.subList(startIndex, endIndex);
        return (T[]) subList
                .toArray((T[]) java.lang.reflect.Array.newInstance(list.get(0).getClass(), subList.size()));
    }

    public static final <T> T[] toArray(List<T> list) {
        return toArray(list, 0, list.size());
    }

    /**
     * This is a convenience method that returns true if the specified collection is null or empty
     * 
     * @param <T>
     *            Any type of object
     * @param collection
     * @return
     */
    public static <T> boolean isEmpty(Collection<T> collection) {
        return collection == null || collection.isEmpty();
    }

    /**
     * This is a convenience method that returns true if the specified map is null or empty
     * 
     * @param <T>
     *            any type of key
     * @param <U>
     *            any type of value
     * @param map
     * @return
     */
    public static <T, U> boolean isEmpty(Map<T, U> map) {
        return map == null || map.isEmpty();
    }

    public static int size(Collection<? extends Object> collection) {
        if (collection == null) {
            return 0;
        }
        return collection.size();
    }
}