001 /* 002 * Licensed to the Apache Software Foundation (ASF) under one or more 003 * contributor license agreements. See the NOTICE file distributed with 004 * this work for additional information regarding copyright ownership. 005 * The ASF licenses this file to You under the Apache License, Version 2.0 006 * (the "License"); you may not use this file except in compliance with 007 * the License. You may obtain a copy of the License at 008 * 009 * http://www.apache.org/licenses/LICENSE-2.0 010 * 011 * Unless required by applicable law or agreed to in writing, software 012 * distributed under the License is distributed on an "AS IS" BASIS, 013 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 014 * See the License for the specific language governing permissions and 015 * limitations under the License. 016 */ 017 package org.apache.commons.lang3; 018 019 import java.util.ArrayList; 020 import java.util.Arrays; 021 import java.util.LinkedHashMap; 022 import java.util.List; 023 import java.util.Map; 024 025 /** 026 * <p>Utility library to provide helper methods for Java enums.</p> 027 * 028 * <p>#ThreadSafe#</p> 029 * 030 * @since 3.0 031 * @version $Id: EnumUtils.java 1088899 2011-04-05 05:31:27Z bayard $ 032 */ 033 public class EnumUtils { 034 035 /** 036 * This constructor is public to permit tools that require a JavaBean 037 * instance to operate. 038 */ 039 public EnumUtils() { 040 } 041 042 /** 043 * <p>Gets the {@code Map} of enums by name.</p> 044 * 045 * <p>This method is useful when you need a map of enums by name.</p> 046 * 047 * @param <E> the type of the enumeration 048 * @param enumClass the class of the enum to query, not null 049 * @return the modifiable map of enum names to enums, never null 050 */ 051 public static <E extends Enum<E>> Map<String, E> getEnumMap(Class<E> enumClass) { 052 Map<String, E> map = new LinkedHashMap<String, E>(); 053 for (E e: enumClass.getEnumConstants()) { 054 map.put(e.name(), e); 055 } 056 return map; 057 } 058 059 /** 060 * <p>Gets the {@code List} of enums.</p> 061 * 062 * <p>This method is useful when you need a list of enums rather than an array.</p> 063 * 064 * @param <E> the type of the enumeration 065 * @param enumClass the class of the enum to query, not null 066 * @return the modifiable list of enums, never null 067 */ 068 public static <E extends Enum<E>> List<E> getEnumList(Class<E> enumClass) { 069 return new ArrayList<E>(Arrays.asList(enumClass.getEnumConstants())); 070 } 071 072 /** 073 * <p>Checks if the specified name is a valid enum for the class.</p> 074 * 075 * <p>This method differs from {@link Enum#valueOf} in that checks if the name is 076 * a valid enum without needing to catch the exception.</p> 077 * 078 * @param <E> the type of the enumeration 079 * @param enumClass the class of the enum to query, not null 080 * @param enumName the enum name, null returns false 081 * @return true if the enum name is valid, otherwise false 082 */ 083 public static <E extends Enum<E>> boolean isValidEnum(Class<E> enumClass, String enumName) { 084 if (enumName == null) { 085 return false; 086 } 087 try { 088 Enum.valueOf(enumClass, enumName); 089 return true; 090 } catch (IllegalArgumentException ex) { 091 return false; 092 } 093 } 094 095 /** 096 * <p>Gets the enum for the class, returning {@code null} if not found.</p> 097 * 098 * <p>This method differs from {@link Enum#valueOf} in that it does not throw an exception 099 * for an invalid enum name.</p> 100 * 101 * @param <E> the type of the enumeration 102 * @param enumClass the class of the enum to query, not null 103 * @param enumName the enum name, null returns null 104 * @return the enum, null if not found 105 */ 106 public static <E extends Enum<E>> E getEnum(Class<E> enumClass, String enumName) { 107 if (enumName == null) { 108 return null; 109 } 110 try { 111 return Enum.valueOf(enumClass, enumName); 112 } catch (IllegalArgumentException ex) { 113 return null; 114 } 115 } 116 117 }