Main.java Source code

Java tutorial

Introduction

Here is the source code for Main.java

Source

//package com.java2s;
/* Copyright 2005-2006 Tim Fennell
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

import java.lang.reflect.Array;
import java.util.LinkedList;
import java.util.List;

public class Main {
    /**
     * <p>
     * Converts an Object reference that is known to be an array into a List.
     * Semantically very similar to {@link java.util.Arrays#asList(Object[])}
     * except that this method can deal with arrays of primitives in the same
     * manner as arrays as objects.
     * </p>
     * 
     * <p>
     * A new List is created of the same size as the array, and elements are
     * copied from the array into the List. If elements are primitives then they
     * are converted to the appropriate wrapper types in order to return a List.
     * </p>
     * 
     * @param in
     *            an array of Objects or primitives (null values are not
     *            allowed)
     * @return a List containing an element for each element in the input array
     * @throws IllegalArgumentException
     *             thrown if the in parameter is null or not an array
     */
    public static List<Object> asList(Object in) {
        if (in == null || !in.getClass().isArray()) {
            throw new IllegalArgumentException("Parameter to asObjectArray must be a non-null array.");
        } else {
            int length = Array.getLength(in);
            LinkedList<Object> list = new LinkedList<Object>();
            for (int i = 0; i < length; ++i) {
                list.add(i, Array.get(in, i));
            }

            return list;
        }
    }

    /**
     * Converts an Iterable into a List that can be navigated in ways other than
     * simple iteration. If the underlying implementation of the Iterable is a
     * List, it is cast to List and returned. Otherwise it is iterated and the
     * items placed, in order, into a new List.
     * 
     * @param in
     *            an Iterable to serve as the source for a List
     * @return either the Iterable itself if it is a List, or a new List with
     *         the same elements
     */
    public static <T> List<T> asList(Iterable<T> in) {
        if (in instanceof List<?>)
            return (List<T>) in;
        else {
            LinkedList<T> list = new LinkedList<T>();
            for (T item : in) {
                list.add(item);
            }

            return list;
        }
    }
}