001    /*--------------------------------------------------------------------------+
002    $Id: EnumUtils.java 26268 2010-02-18 10:44:30Z juergens $
003    |                                                                          |
004    | Copyright 2005-2010 Technische Universitaet Muenchen                     |
005    |                                                                          |
006    | Licensed under the Apache License, Version 2.0 (the "License");          |
007    | you may not use this file except in compliance with the License.         |
008    | You may obtain a copy of the License at                                  |
009    |                                                                          |
010    |    http://www.apache.org/licenses/LICENSE-2.0                            |
011    |                                                                          |
012    | Unless required by applicable law or agreed to in writing, software      |
013    | distributed under the License is distributed on an "AS IS" BASIS,        |
014    | WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
015    | See the License for the specific language governing permissions and      |
016    | limitations under the License.                                           |
017    +--------------------------------------------------------------------------*/
018    package edu.tum.cs.commons.enums;
019    
020    import edu.tum.cs.commons.assertion.CCSMAssert;
021    
022    /**
023     * Utility class for enumerations.
024     * 
025     * @author Florian Deissenboeck
026     * @author $Author: juergens $
027     * 
028     * @version $Rev: 26268 $
029     * @levd.rating GREEN Hash: C7E378E216DC3DABDB774FCA63D04260
030     */
031    public class EnumUtils {
032    
033            /**
034             * This works like {@link Enum#valueOf(java.lang.Class, java.lang.String)}
035             * but returns <code>null</code> if constant wasn't found instead of
036             * throwing an <code>IllegalArgumentException</code>.
037             * 
038             * @param enumType
039             *            Enumeration class
040             * @param constantName
041             *            name of the constant
042             * @return the matching constant or <code>null</code> if not found
043             */
044            public static <T extends Enum<T>> T valueOf(Class<T> enumType,
045                            String constantName) {
046                    try {
047                            T constant = Enum.valueOf(enumType, constantName);
048                            return constant;
049                    } catch (IllegalArgumentException ex) {
050                            return null;
051                    }
052            }
053    
054            /**
055             * Works like {@link #valueOf(Class, String)} but ignores case.
056             * 
057             * Worst case runtime is O('number of constants in enum').
058             */
059            public static <T extends Enum<T>> T valueOfIgnoreCase(Class<T> enumType,
060                            String constantName) {
061                    T[] constants = enumType.getEnumConstants();
062                    CCSMAssert.isNotNull(constants);
063                    for (T constant : constants) {
064                            if (constant.name().equalsIgnoreCase(constantName)) {
065                                    return constant;
066                            }
067                    }
068                    return null;
069            }
070    
071            /**
072             * Returns an array containing the names of the enum element. Ordering is
073             * same as element ordering in enum.
074             */
075            public static <T extends Enum<T>> String[] stringValues(Class<T> enumType) {
076                    T[] constants = enumType.getEnumConstants();
077                    CCSMAssert.isNotNull(constants);
078                    String[] result = new String[constants.length];
079                    for (int i = 0; i < constants.length; i++) {
080                            result[i] = constants[i].name();
081                    }
082                    return result;
083            }
084    }