001/*
002 * The contents of this file are subject to the terms of the Common Development and
003 * Distribution License (the License). You may not use this file except in compliance with the
004 * License.
005 *
006 * You can obtain a copy of the License at legal/CDDLv1.0.txt. See the License for the
007 * specific language governing permission and limitations under the License.
008 *
009 * When distributing Covered Software, include this CDDL Header Notice in each file and include
010 * the License file at legal/CDDLv1.0.txt. If applicable, add the following below the CDDL
011 * Header, with the fields enclosed by brackets [] replaced by your own identifying
012 * information: "Portions Copyright [year] [name of copyright owner]".
013 *
014 * Copyright 2010-2011 ApexIdentity Inc.
015 * Portions Copyright 2011-2015 ForgeRock AS.
016 */
017
018package org.forgerock.openig.util;
019
020import java.util.Collections;
021import java.util.HashMap;
022import java.util.LinkedHashSet;
023import java.util.Map;
024import java.util.Set;
025
026/**
027 * Utility class for accessing Java enum types.
028 */
029public final class EnumUtil {
030
031    /** Cache of name sets for repeated call efficiency. */
032    private static final Map<Class<?>, Set<String>> NAMESETS = new HashMap<>();
033
034    /** Static methods only. */
035    private EnumUtil() {
036    }
037
038    /**
039     * Returns a set of the names of the enum constants of the specified enum type. The
040     * returned set maintains iteration order of the constants in the order they're declared
041     * in the enum type.
042     *
043     * @param enumType the class of the enum type from which to return the names.
044     * @param <T> enumeration type
045     * @return a set of the names of the enum constants in the specified enum.
046     */
047    public static <T extends Enum<T>> Set<String> names(Class<T> enumType) {
048        Set<String> set = NAMESETS.get(enumType);
049        if (set == null) {
050            // cached for repeated call efficiency
051            set = new LinkedHashSet<>();
052            for (T constant : enumType.getEnumConstants()) {
053                set.add(constant.toString());
054            }
055            NAMESETS.put(enumType, Collections.unmodifiableSet(set));
056        }
057        return set;
058    }
059
060    /**
061     * Returns the enum constant of the specified enum type with the specified name, or
062     * {@code null} if the specified enum type has no constant with the specified name, or
063     * if the specified class object does not represent an enum type.
064     *
065     * @param enumType the class of the enum type from which to return a constant.
066     * @param name the name of the constant to return.
067     * @param <T> enumeration type
068     * @return the matching enum constant or {@code null} if no match found.
069     */
070    public static <T extends Enum<T>> T valueOf(Class<T> enumType, Object name) {
071        T value = null;
072        if (name instanceof CharSequence) {
073            try {
074                value = Enum.valueOf(enumType, name.toString());
075            } catch (IllegalArgumentException iae) {
076                // result in null return value
077            }
078        }
079        return value;
080    }
081}