Main.java Source code

Java tutorial

Introduction

Here is the source code for Main.java

Source

//package com.java2s;
/* This file is part of the InternalPluginManager.
 *
 * Copyright (C) 2014-2015 Fabian Damken
 *
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
 */

import java.lang.reflect.Modifier;

import java.util.Set;

public class Main {
    /**
     * Converts the given array to a set with the given type. Works like
     * {@link Arrays#asList(Object...)}.
     *
     * @param <T>
     *            The type of the set values.
     * @param setType
     *            The set type to return.
     * @param arr
     *            The array to convert into a set.
     * @return An object of the given set or, if, and only if, an error
     *         occurred, <code>null</code>.
     */
    @SuppressWarnings("unchecked")
    public static <T> Set<T> toSet(@SuppressWarnings("rawtypes") final Class<? extends Set> setType,
            final T... arr) {
        assert setType != null : "SetType cannot be null!";
        assert !Modifier.isAbstract(setType.getModifiers()) : "SetType cannot be abstract!";
        assert !setType.isInterface() : "SetType cannot be an interface!";
        assert arr != null : "Arr cannot be null!";

        Set<T> result = null;
        try {
            result = setType.newInstance();

            for (final T t : arr) {
                result.add(t);
            }
        } catch (final InstantiationException ex) {
            ex.printStackTrace();
        } catch (final IllegalAccessException ex) {
            ex.printStackTrace();
        }

        return result;
    }
}