Java EnumSet Usage getEnumFromString(Class enumClass, String enumValue, boolean compareByValue)

Here you can find the source of getEnumFromString(Class enumClass, String enumValue, boolean compareByValue)

Description

This method return Enum which name values is equals to String, which is given as parameter.

License

Open Source License

Parameter

Parameter Description
enumClass Enum class in which will be found enumValue.
enumValue value of field which will be compare in field in enumClass.
compareByValue flag which determine if comparison is done against string value or enum value. <br /> <ul> <li><b>True</b> comparison is done against string value of enum.</li> <li><b>False</b> comparison is done against enum real value </b></li> </ul>

Exception

Parameter Description
IllegalArgumentException if no match is found or arguments are null or empty.

Return

Enum, which has name equals enumValue in enumClass.

Declaration

public static <E extends Enum<E>> Enum<E> getEnumFromString(Class<E> enumClass, String enumValue,
        boolean compareByValue) throws IllegalArgumentException 

Method Source Code

//package com.java2s;
//License from project: Open Source License 

import java.util.EnumSet;

public class Main {
    /**/*w  ww  .  j av  a2  s.c  o  m*/
     * This method return {@link Enum} which name values is equals to String, which is given as
     * parameter. If arguments are null or string value is empty or no match is found, then
     * {@link IllegalArgumentException} is thrown.
     * 
     * @param enumClass {@link Enum} class in which will be found enumValue.
     * @param enumValue value of field which will be compare in field in enumClass.
     * @param compareByValue flag which determine if comparison is done against string value or enum
     *        value. <br />
     *        <ul>
     *        <li><b>True</b> comparison is done against string value of enum.</li>
     *        <li><b>False</b> comparison is done against enum real value </b></li>
     *        </ul>
     * @return Enum, which has name equals enumValue in enumClass.
     * @throws IllegalArgumentException if no match is found or arguments are null or empty.
     */
    public static <E extends Enum<E>> Enum<E> getEnumFromString(Class<E> enumClass, String enumValue,
            boolean compareByValue) throws IllegalArgumentException {
        if (enumClass == null || enumValue == null || enumValue.isEmpty()) {
            throw new IllegalArgumentException(
                    "Class shouldn't be null and value to compare shouldn't be null or empty.");
        }
        for (E enumField : EnumSet.allOf(enumClass)) {
            if (compareByValue) {
                if (enumField.toString().equalsIgnoreCase(enumValue)) {
                    return enumField;
                }
            } else {
                if (enumField.name().equalsIgnoreCase(enumValue)) {
                    return enumField;
                }
            }
        }
        throw new IllegalArgumentException("No one of string value from enum class " + enumClass.getName()
                + " doestn match with " + enumValue);
    }
}

Related

  1. encode(EnumSet set)
  2. extractTypes(final Class type)
  3. findEnumIgnoreCase(Class enumClass, String string, T defValue)
  4. getDataFromEnum(Class enumClass)
  5. getDataInEnumClass(String enumClassName)
  6. nativeLoadEnumDefaultValues(Class enumType)
  7. nextEnum(T ce)
  8. possibilities(Class enumClass)
  9. rotateEnum(T ce, boolean backwards, EnumSet ValidOptions)