Creates an unmodifiable set from a variable number arguments. - Android java.util

Android examples for java.util:Set

Description

Creates an unmodifiable set from a variable number arguments.

Demo Code

/*/*from  w w  w  .j a v a  2  s .  c o m*/
 * --------------------
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
 *
 * Copyright 2008-2009 Sun Microsystems, Inc. All rights reserved.
 *
 * The contents of this file are subject to the terms of the Common Development
 * and Distribution License("CDDL") (the "License").  You may not use this file
 * except in compliance with the License.
 *
 * You can obtain a copy of the License at
 * http://opensource.org/licenses/cddl1.php
 * See the License for the specific language governing permissions and limitations
 * under the License.
 *
 * When distributing the Covered Code, include this CDDL Header Notice in each file
 * and include the License file at http://opensource.org/licenses/cddl1.php.
 * If applicable, add the following below this CDDL Header, with the fields
 * enclosed by brackets [] replaced by your own identifying information:
 * "Portions Copyrighted [year] [name of copyright owner]"
 * --------------------
 */
import java.lang.reflect.Array;
import java.util.ArrayList;
import java.util.Collection;
import java.util.Collections;
import java.util.Comparator;
import java.util.HashMap;
import java.util.HashSet;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import java.util.Properties;
import java.util.Set;
import java.util.SortedMap;
import java.util.SortedSet;
import java.util.TreeMap;
import java.util.TreeSet;

public class Main{
    /**
     * Creates an unmodifiable set from a variable number arguments.
     */
    public static <T> Set<T> newReadOnlySet(T... arr) {
        return Collections.unmodifiableSet(newSet(arr));
    }
    /**
     * Creates an unmodifiable set from a {@link Collection}.
     */
    public static <T> Set<T> newReadOnlySet(Collection<T> c) {
        return Collections.unmodifiableSet(newSet(c));
    }
    /**
     * Creates a set that can be modified from the {@link Collection} provided.
     */
    public static <T> Set<T> newSet(Collection<T> c) {
        return new HashSet<T>(CollectionUtil.nullAsEmpty(c));
    }
    /**
     * Creates a set that can be modified from the arguments.
     */
    public static <T> Set<T> newSet(T... arr) {
        // default to empty..
        Set<T> ret = new HashSet<T>();
        if (arr != null && arr.length != 0) {
            // not empty populate the set..
            for (T t : arr) {
                ret.add(t);
            }
        }
        return ret;
    }
    /**
     * Protects from <code>null</code> and returns a new instance of
     * {@link HashSet}.
     *
     * if the parameter <strong>c</strong> is <strong>null</strong>.
     *
     * @param c
     *            collection to check
     * @param <T>
     *            the type of the collection
     * @return if null new {@link HashSet} otherwise the parameter that was
     *         passed in or
     */
    public static <T> Collection<T> nullAsEmpty(Collection<T> c) {
        return c == null ? new HashSet<T>() : c;
    }
    /**
     * Protects from <code>null</code> and returns a new instance of
     * {@link HashMap} if the parameter <code>map</code> is <code>null</code>.
     * Otherwise return the parameter that was passed in.
     */
    public static <T, K> Map<T, K> nullAsEmpty(Map<T, K> map) {
        return (map == null) ? new HashMap<T, K>() : map;
    }
    /**
     * Protects from <code>null</code> and returns a new instance of
     * {@link HashSet} if the parameter <code>set</code> is <code>null</code>.
     * Otherwise return the parameter that was passed in.
     */
    public static <T> Set<T> nullAsEmpty(Set<T> set) {
        return (set == null) ? new HashSet<T>() : set;
    }
    /**
     * Protects from <code>null</code> and returns a new instance of
     * {@link ArrayList} if the parameter <code>list</code> is <code>null</code>
     * . Otherwise return the parameter that was passed in.
     */
    public static <T> List<T> nullAsEmpty(final List<T> list) {
        return (list == null) ? new ArrayList<T>() : list;
    }
}

Related Tutorials