Java String Value Of valueOfIgnoreCase(Class enumType, String constantName)

Here you can find the source of valueOfIgnoreCase(Class enumType, String constantName)

Description

Works like #valueOf(Class,String) but ignores case.

License

Apache License

Declaration

public static <T extends Enum<T>> T valueOfIgnoreCase(Class<T> enumType, String constantName) 

Method Source Code

//package com.java2s;
/*--------------------------------------------------------------------------+
   $Id: EnumUtils.java 16749 2008-06-20 16:04:47Z deissenb $
 |                                                                          |
 | Copyright 2005-2008 Technische Universitaet Muenchen                     |
 |                                                                          |
 | Licensed under the Apache License, Version 2.0 (the "License");          |
 | you may not use this file except in compliance with the License.         |
 | You may obtain a copy of the License at                                  |
 |                                                                          |
 |    http://www.apache.org/licenses/LICENSE-2.                        |
 |                                                                          |
 | Unless required by applicable law or agreed to in writing, software      |
 | distributed under the License is distributed on an "AS IS" BASIS,        |
 | WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
 | See the License for the specific language governing permissions and      |
 | limitations under the License.                                           |
 +--------------------------------------------------------------------------*/

public class Main {
    /**/*ww w .  j  a va 2s  . co m*/
     * Works like {@link #valueOf(Class, String)} but ignores case.
     * 
     * Worst case runtime is O('number of constants in enum').
     */
    public static <T extends Enum<T>> T valueOfIgnoreCase(Class<T> enumType, String constantName) {
        T[] constants = enumType.getEnumConstants();
        if (constants == null) {
            return null;
        }
        for (T constant : constants) {
            if (constant.name().equalsIgnoreCase(constantName)) {
                return constant;
            }
        }
        return null;
    }
}

Related

  1. valueOfBooleanStr(String str)
  2. valueOfEnum(Class enumClass, String value, E defaultValue)
  3. valueOfEnum(final Class type, final String value)
  4. valueOfIC(Class enumType, String aName)
  5. valueOfIgnoreCase(Class cls, String value)
  6. valueOfInteger(final String s, int radix)
  7. valueOfIpv4(String ip)
  8. valueOfLongToString(long[] values)
  9. valueOfNullSafe(final Class enumType, final String name)