001/*-------------------------------------------------------------------------+
002|                                                                          |
003| Copyright 2005-2011 The ConQAT Project                                   |
004|                                                                          |
005| Licensed under the Apache License, Version 2.0 (the "License");          |
006| you may not use this file except in compliance with the License.         |
007| 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+-------------------------------------------------------------------------*/
017package org.conqat.lib.commons.enums;
018
019import java.util.Arrays;
020import java.util.EnumSet;
021
022import org.conqat.lib.commons.assertion.CCSMAssert;
023
024/**
025 * Utility class for enumerations.
026 */
027public class EnumUtils {
028
029        /**
030         * This works like {@link Enum#valueOf(java.lang.Class, java.lang.String)} but
031         * returns <code>null</code> if constant wasn't found instead of throwing an
032         * <code>IllegalArgumentException</code>.
033         * 
034         * @param enumType
035         *            Enumeration class
036         * @param constantName
037         *            name of the constant
038         * @return the matching constant or <code>null</code> if not found
039         */
040        public static <T extends Enum<T>> T valueOf(Class<T> enumType, String constantName) {
041                try {
042                        T constant = Enum.valueOf(enumType, constantName);
043                        return constant;
044                } catch (IllegalArgumentException ex) {
045                        return null;
046                }
047        }
048
049        /**
050         * Works like {@link #valueOf(Class, String)} but ignores case.
051         * 
052         * Worst case runtime is O('number of constants in enum').
053         */
054        public static <T extends Enum<T>> T valueOfIgnoreCase(Class<T> enumType, String constantName) {
055                T[] constants = enumType.getEnumConstants();
056                CCSMAssert.isNotNull(constants);
057                for (T constant : constants) {
058                        if (constant.name().equalsIgnoreCase(constantName)) {
059                                return constant;
060                        }
061                }
062                return null;
063        }
064
065        /**
066         * Returns an array containing the names of the enum element. Ordering is same
067         * as element ordering in enum.
068         */
069        public static <T extends Enum<T>> String[] names(Class<T> enumType) {
070                T[] constants = enumType.getEnumConstants();
071                CCSMAssert.isNotNull(constants);
072                String[] result = new String[constants.length];
073                for (int i = 0; i < constants.length; i++) {
074                        result[i] = constants[i].name();
075                }
076                return result;
077        }
078
079        /**
080         * Merges an EnumSet and a list of enum values into one EnumSet.
081         */
082        @SafeVarargs
083        public static <T extends Enum<T>> EnumSet<T> mergeSets(EnumSet<T> set, T... values) {
084                EnumSet<T> newSet = set.clone();
085                newSet.addAll(Arrays.asList(values));
086                return newSet;
087        }
088}