Java String Value Of valueOf(Class enumType, String name)

Here you can find the source of valueOf(Class enumType, String name)

Description

value Of

License

Mozilla Public License

Declaration

public static <T extends Enum<T>> T valueOf(Class<T> enumType, String name) 

Method Source Code

//package com.java2s;
//License from project: Mozilla Public License 

public class Main {
    public static <T extends Enum<T>> T valueOf(Class<T> enumType, String name) {
        return valueOf(enumType, name, null);
    }// w  w w .  j  a  v a  2s  .co  m

    public static <T extends Enum<T>> T valueOf(Class<T> enumType, String name, T defaultValue) {
        T foundConstant = null;

        T[] constants = enumType.getEnumConstants();
        for (T constant : constants) {
            if (constant.toString().equals(name))
                foundConstant = constant;
        }

        if (foundConstant == null && defaultValue != null)
            foundConstant = defaultValue;

        return foundConstant;
    }
}

Related

  1. valueOf(Class cls, String s)
  2. valueOf(Class en, String name)
  3. valueOf(Class enumType, String constantName)
  4. valueOf(Class enumType, String name)
  5. valueOf(Class enumType, String name)
  6. valueOf(Class enumType, String name, T defaultValue)
  7. valueOf(final Class enumType, final String value)
  8. valueOf(final Class type, final String value)