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 2008 Sun Microsystems, Inc.
015 * Portions Copyright 2015 ForgeRock AS.
016 */
017package org.opends.server.authorization.dseecompat;
018
019/**
020 * This class provides an enumeration of the two access types (allow, deny).
021 */
022public enum EnumAccessType {
023
024    /** Allow access type. */
025    ALLOW   ("allow"),
026    /** Deny access type. */
027    DENY    ("deny");
028
029    /** The access type string. */
030    private final String accessType;
031
032    /**
033     * Constructor that sets the accessType string.
034     * @param accessType The access type string to set.
035     */
036    EnumAccessType (String accessType){
037        this.accessType = accessType ;
038    }
039
040    /**
041     * Checks if the access type is equal to the string
042     * representation passed in.
043     * @param type The string representation of the access type.
044     * @return True if the access types are equal.
045     */
046    public boolean isAccessType(String type){
047        return type.equalsIgnoreCase(accessType);
048    }
049
050    /*
051     * TODO Make this method and all other Enum decode methods more efficient.
052     *
053     * Using the Enum.values() method is documented to be potentially slow.
054     * If we ever expect to use the decode() method in a performance-critical
055     * manner, then we should make it more efficient.  The same thing applies
056     * to all of the other enumeration types defined in the package.
057     */
058    /**
059     * Decodes an access type enumeration from a string passed into the method.
060     * @param type The string representation of the access type.
061     * @return   Return an EnumAccessType matching the string representation,
062     * or null if the string is not valid.
063     */
064    public static EnumAccessType decode(String type){
065        if (type != null){
066            for (EnumAccessType t : EnumAccessType.values()) {
067                if (t.isAccessType(type)){
068                    return t;
069                }
070            }
071        }
072        return null;
073    }
074}