Java Array to Set translateToSet(T[] array)

Here you can find the source of translateToSet(T[] array)

Description

Creates new Set from passed array instance

License

Open Source License

Parameter

Parameter Description
array a parameter

Return

translated from array

Declaration

public static <T> Set<T> translateToSet(T[] array) 

Method Source Code

//package com.java2s;
/*/*from  w  w w  .  jav a2s.  c o m*/
 * Lightmare, Lightweight embedded EJB container (works for stateless session beans) with JPA / Hibernate support
 *
 * Copyright (c) 2013, Levan Tsinadze, or third-party contributors as
 * indicated by the @author tags or express copyright attribution
 * statements applied by the authors.
 *
 * This copyrighted material is made available to anyone wishing to use, modify,
 * copy, or redistribute it subject to the terms and conditions of the GNU
 * Lesser General Public License, as published by the Free Software Foundation.
 *
 * 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 Lesser General Public License
 * for more details.
 *
 * You should have received a copy of the GNU Lesser General Public License
 * along with this distribution; if not, write to:
 * Free Software Foundation, Inc.
 * 51 Franklin Street, Fifth Floor
 * Boston, MA  02110-1301  USA
 */

import java.util.Arrays;
import java.util.Collection;
import java.util.Collections;
import java.util.HashSet;

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

public class Main {
    public static final int EMPTY_ARRAY_LENGTH = 0;

    /**
     * Creates new {@link Set} from passed {@link Collection} instance
     * 
     * @param collection
     * @return {@link Set} translated from collection
     */
    public static <T> Set<T> translateToSet(Collection<T> collection) {

        Set<T> set;

        if (valid(collection)) {
            set = new HashSet<T>(collection);
        } else {
            set = Collections.emptySet();
        }

        return set;
    }

    /**
     * Creates new {@link Set} from passed array instance
     * 
     * @param array
     * @return {@link Set} translated from array
     */
    public static <T> Set<T> translateToSet(T[] array) {

        List<T> collection;

        if (valid(array)) {
            collection = Arrays.asList(array);
        } else {
            collection = null;
        }

        return translateToSet(collection);
    }

    /**
     * Checks passed {@link Collection} instance on null and on emptiness
     * returns true if it is not null and is not empty
     * 
     * @param collection
     * @return <code></code>
     */
    public static boolean valid(Collection<?> collection) {
        return (collection != null && !collection.isEmpty());
    }

    /**
     * Checks passed {@link Map} instance on null and emptiness returns true if
     * it is not null and is not empty
     * 
     * @param map
     * @return <code>boolean</code>
     */
    public static boolean valid(Map<?, ?> map) {
        return (map != null && !map.isEmpty());
    }

    /**
     * Checks if passed array of {@link Object}'s instances is not null and is
     * not empty
     * 
     * @param array
     * @return <code>boolean</code>
     */
    public static boolean valid(Object[] array) {
        return (array != null && array.length > EMPTY_ARRAY_LENGTH);
    }
}

Related

  1. arrayToSet(final T[] array)
  2. arrayToSet(Object[] in)
  3. toSet(final TYPE[] array)
  4. toSet(T[] array)