001/*
002 * CDDL HEADER START
003 *
004 * The contents of this file are subject to the terms of the
005 * Common Development and Distribution License, Version 1.0 only
006 * (the "License").  You may not use this file except in compliance
007 * with the License.
008 *
009 * You can obtain a copy of the license at legal-notices/CDDLv1_0.txt
010 * or http://forgerock.org/license/CDDLv1.0.html.
011 * See the License for the specific language governing permissions
012 * and limitations under the License.
013 *
014 * When distributing Covered Code, include this CDDL HEADER in each
015 * file and include the License file at legal-notices/CDDLv1_0.txt.
016 * If applicable, add the following below this CDDL HEADER, with the
017 * fields enclosed by brackets "[]" replaced with your own identifying
018 * information:
019 *      Portions Copyright [yyyy] [name of copyright owner]
020 *
021 * CDDL HEADER END
022 *
023 *
024 *      Copyright 2008 Sun Microsystems, Inc.
025 *      Portions Copyright 2015 ForgeRock AS.
026 */
027package org.opends.server.authorization.dseecompat;
028
029/**
030 * This class provides an enumeration of the two access types (allow, deny).
031 */
032public enum EnumAccessType {
033
034    /** Allow access type. */
035    ALLOW   ("allow"),
036    /** Deny access type. */
037    DENY    ("deny");
038
039    /** The access type string. */
040    private final String accessType;
041
042    /**
043     * Constructor that sets the accessType string.
044     * @param accessType The access type string to set.
045     */
046    EnumAccessType (String accessType){
047        this.accessType = accessType ;
048    }
049
050    /**
051     * Checks if the access type is equal to the string
052     * representation passed in.
053     * @param type The string representation of the access type.
054     * @return True if the access types are equal.
055     */
056    public boolean isAccessType(String type){
057        return type.equalsIgnoreCase(accessType);
058    }
059
060    /*
061     * TODO Make this method and all other Enum decode methods more efficient.
062     *
063     * Using the Enum.values() method is documented to be potentially slow.
064     * If we ever expect to use the decode() method in a performance-critical
065     * manner, then we should make it more efficient.  The same thing applies
066     * to all of the other enumeration types defined in the package.
067     */
068    /**
069     * Decodes an access type enumeration from a string passed into the method.
070     * @param type The string representation of the access type.
071     * @return   Return an EnumAccessType matching the string representation,
072     * or null if the string is not valid.
073     */
074    public static EnumAccessType decode(String type){
075        if (type != null){
076            for (EnumAccessType t : EnumAccessType.values()) {
077                if (t.isAccessType(type)){
078                    return t;
079                }
080            }
081        }
082        return null;
083    }
084}