Java EnumSet Create parseEnum(final Class klass, final String input)

Here you can find the source of parseEnum(final Class klass, final String input)

Description

parse Enum

License

Artistic License

Declaration

public static <E extends Enum<E>> EnumSet<E> parseEnum(final Class<E> klass, final String input) 

Method Source Code

//package com.java2s;
/**//  w w  w. j a v  a  2 s  .  c o m
 * (c) 2009-2014 Otto-Friedrich-University Bamberg
 *
 * $Id$
 *
 * Use, modification and restribution of this file are covered by the
 * terms of the Artistic License 2.0.
 *
 * You should have received a copy of the license terms in a file named
 * "LICENSE" together with this software package.
 *
 * Disclaimer of Warranty: THE PACKAGE IS PROVIDED BY THE COPYRIGHT
 * HOLDER AND CONTRIBUTORS "AS IS' AND WITHOUT ANY EXPRESS OR IMPLIED
 * WARRANTIES. THE IMPLIED WARRANTIES OF MERCHANTABILITY, FITNESS FOR
 * A PARTICULAR PURPOSE, OR NON-INFRINGEMENT ARE DISCLAIMED TO THE
 * EXTENT PERMITTED BY YOUR LOCAL LAW. UNLESS REQUIRED BY LAW, NO
 * COPYRIGHT HOLDER OR CONTRIBUTOR WILL BE LIABLE FOR ANY DIRECT,
 * INDIRECT, INCIDENTAL, OR CONSEQUENTIAL DAMAGES ARISING IN ANY WAY OUT
 * OF THE USE OF THE PACKAGE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH
 * DAMAGE.
 **/

import java.util.EnumSet;

public class Main {
    public static <E extends Enum<E>> EnumSet<E> parseEnum(final Class<E> klass, final String input) {
        return parseEnum(klass, input, "\\s*,\\s*");
    }

    public static <E extends Enum<E>> EnumSet<E> parseEnum(final Class<E> klass, final String input,
            final String sepExp) {
        final EnumSet<E> result = EnumSet.noneOf(klass);
        final String[] parts = input.split(sepExp);
        for (String part : parts) {
            final E value = Enum.valueOf(klass, part);
            result.add(value);
        }
        return result;
    }
}

Related

  1. intToEnumSet(Class enumClass, int decoded)
  2. newEnumSet(Class klass)
  3. noneOf( Class elementType)
  4. of(E e1, E... e)
  5. of(E[] c)
  6. toEnumSet( Class classValue, Collection stringValues)
  7. toEnumSet(Class enumClass, long vector)
  8. toEnumSet(Class clazz, T... ts)