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 * Utility library to provide helper methods for Java enums.
027 *
028 * <p>#ThreadSafe#</p>
029 * @author Apache Software Foundation
030 * @version $Id: EnumUtils.java 967237 2010-07-23 20:08:57Z mbenson $
031 */
032 public class EnumUtils {
033
034 /**
035 * This constructor is public to permit tools that require a JavaBean
036 * instance to operate.
037 */
038 public EnumUtils() {
039 }
040
041 /**
042 * Gets the <code>Map</code> of <code>enums</code> by name.
043 * <p>
044 * This method is useful when you need a map of enums by name.
045 *
046 * @param enumClass the class of the <code>enum</code> to get, not null
047 * @return the modifiable map of enum names to enums, never null
048 */
049 public static <E extends Enum<E>> Map<String, E> getEnumMap(Class<E> enumClass) {
050 Map<String, E> map = new LinkedHashMap<String, E>();
051 for (E e: enumClass.getEnumConstants()) {
052 map.put(e.name(), e);
053 }
054 return map;
055 }
056
057 /**
058 * Gets the <code>List</code> of <code>enums</code>.
059 * <p>
060 * This method is useful when you need a list of enums rather than an array.
061 *
062 * @param enumClass the class of the <code>enum</code> to get, not null
063 * @return the modifiable list of enums, never null
064 */
065 public static <E extends Enum<E>> List<E> getEnumList(Class<E> enumClass) {
066 return new ArrayList<E>(Arrays.asList(enumClass.getEnumConstants()));
067 }
068
069 /**
070 * Checks if the specified name is a valid <code>enum</code> for the class.
071 * <p>
072 * This method differs from {@link Enum#valueOf} in that checks if the name is
073 * a valid enum without needing to catch the exception.
074 *
075 * @param enumClass the class of the <code>enum</code> to get, not null
076 * @param enumName the enum name
077 * @return true if the enum name is valid, otherwise false
078 */
079 public static <E extends Enum<E>> boolean isValidEnum(Class<E> enumClass, String enumName) {
080 try {
081 Enum.valueOf(enumClass, enumName);
082 return true;
083 } catch (IllegalArgumentException ex) {
084 return false;
085 }
086 }
087
088 /**
089 * Gets the <code>enum</code> for the class, returning <code>null</code> if not found.
090 * <p>
091 * This method differs from {@link Enum#valueOf} in that it does not throw an exception
092 * for an invalid enum name.
093 *
094 * @param enumClass the class of the <code>enum</code> to get, not null
095 * @param enumName the enum name
096 * @return the enum or null if not found
097 */
098 public static <E extends Enum<E>> E getEnum(Class<E> enumClass, String enumName) {
099 try {
100 return Enum.valueOf(enumClass, enumName);
101 } catch (IllegalArgumentException ex) {
102 return null;
103 }
104 }
105
106 }