Java EnumSet Create asSet(E[] array)

Here you can find the source of asSet(E[] array)

Description

as Set

License

Open Source License

Declaration

public static <E extends Enum<E>> EnumSet<E> asSet(E[] array) 

Method Source Code


//package com.java2s;
/*/*from   www .j a  v  a  2 s.  c  o  m*/
 * Scriptographer
 *
 * This file is part of Scriptographer, a Scripting Plugin for Adobe Illustrator
 * http://scriptographer.org/
 *
 * Copyright (c) 2002-2010, Juerg Lehni
 * http://scratchdisk.com/
 *
 * All rights reserved. See LICENSE file for details.
 * 
 * File created on Aug 14, 2009.
 */

import java.util.Arrays;
import java.util.Collection;
import java.util.EnumSet;
import java.util.Iterator;

public class Main {
    /**
     * An alternative to EnumSet.copyOf, with the addition to filter out null
     * values.
     */
    public static <E extends Enum<E>> EnumSet<E> asSet(Collection<E> list) {
        if (list instanceof EnumSet) {
            return ((EnumSet<E>) list).clone();
        } else {
            Iterator<E> it = list.iterator();
            if (it.hasNext()) {
                E first = it.next();
                while (first == null && it.hasNext())
                    first = it.next();
                if (first != null) {
                    EnumSet<E> result = EnumSet.of(first);
                    while (it.hasNext()) {
                        E next = it.next();
                        if (next != null)
                            result.add(next);
                    }
                    return result;
                }
            }
            return null;
        }
    }

    public static <E extends Enum<E>> EnumSet<E> asSet(E[] array) {
        return array != null ? asSet(Arrays.asList(array)) : null;
    }
}

Related

  1. createEnumSet(Class elementType)
  2. enumFromSet(EnumSet theSet)
  3. enumSet(T... elements)
  4. enumSetOf(long bitSet, Class eClass)