get As Enum Type Collection - Java Collection Framework

Java examples for Collection Framework:Collections Utility Methods

Description

get As Enum Type Collection

Demo Code


import java.util.ArrayList;
import java.util.Collection;
import java.util.List;

public class Main{

    public static final String VALUE_DELIMITER = ",";
    @SuppressWarnings({ "rawtypes", "unchecked" })
    public static final <E extends Enum<E>> List getAsEnumTypeCollection(
            String valueAsString, Class<E> type) throws Exception {
        List result = new ArrayList();
        String[] valuesArray = null;

        if ((valueAsString != null) && (!valueAsString.isEmpty())
                && (type != null)) {
            valuesArray = valueAsString.split(VALUE_DELIMITER);
            for (int i = 0; i < valuesArray.length; i++) {
                result.add(StringToTypedValueHelper.getAsEnumType(
                        valuesArray[i], type));
            }/*from w ww  .  j ava2  s .  c om*/
        }

        return result;
    }
    public static final <E extends Enum<E>> Object getAsEnumType(
            String valueAsString, Class<E> type) throws Exception {
        Object result = null;

        if ((valueAsString != null) && (!valueAsString.isEmpty())
                && (type != null)) {
            result = Enum.valueOf(type, valueAsString);
        }

        return result;
    }
}

Related Tutorials