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 */
016
017package org.opends.server.authorization.dseecompat;
018
019/**
020 * This class provides an enumeration of evaluation results returned by
021 * the bind rule evaluation methods.
022 */
023public enum EnumEvalResult {
024
025    /**
026     * This enumeration is returned when the result of the evaluation is TRUE.
027     */
028    TRUE(0),
029    /**
030     * This enumeration is returned when the result of the evaluation is FALSE.
031     */
032    FALSE(1),
033    /**
034     * This enumeration is returned when the result of the evaluation is FAIL.
035     * This should only be returned when a system failure occurred.
036     */
037    FAIL(2),
038    /**
039     * This is an internal enumeration used during evaluation of bind rule when
040     * internal processing of the evaluation is undefined. It is never returned
041     * back as a result of the evaluation.
042     */
043    ERR(3);
044
045    /**
046     * Create a new enumeration type for the specified result value.
047     * @param v The value of the result.
048     */
049    EnumEvalResult(int v) {
050    }
051
052    /**
053     * The method tries to determine if the result was undefined, and if so
054     * it returns an FAIL enumeration. If the result was not undefined (the
055     * common case for all of the bind rule evaluations), then the bind rule
056     * type is examined to see if the result needs to be flipped (type equals
057     * NOT_EQUAL_BINDRULE_TYPE).
058     * @param type The bind rule type enumeration of the bind rule.
059     * @param undefined  A flag that signals the the result was undefined.
060     * @return An enumeration containing the correct result after processing
061     * the undefined field and the bind rule type enumeration.
062     */
063    public EnumEvalResult getRet(EnumBindRuleType type, boolean undefined) {
064        if (equals(EnumEvalResult.TRUE) || !undefined) {
065            if (EnumBindRuleType.NOT_EQUAL_BINDRULE_TYPE.equals(type)) {
066              return negate(this);
067            }
068        } else {
069            return EnumEvalResult.FAIL;
070        }
071        return this;
072    }
073
074    /**
075     * This method is used to possibly negate the result of a simple bind rule
076     * evaluation. If the boolean is true than the result is negated.
077     * @param v The enumeration result of the simple bind rule evaluation.
078     * @param negate If true the result should be negated (TRUE->FALSE, FALSE->TRUE).
079     * @return  A possibly negated enumeration result.
080     */
081    public  static EnumEvalResult negateIfNeeded(EnumEvalResult v, boolean negate) {
082        if (negate) {
083            return negate(v);
084        }
085        return v;
086    }
087
088    private static EnumEvalResult negate(EnumEvalResult v) {
089        if (EnumEvalResult.TRUE.equals(v)) {
090            return EnumEvalResult.FALSE;
091        } else {
092            return EnumEvalResult.TRUE;
093        }
094    }
095
096    /**
097     * Helper method that converts this enumeration to a boolean. Usually the
098     * FAIL enumeration has been handled before this is called.
099     * @return True if the enumeration is TRUE, else false.
100     */
101    public boolean getBoolVal() {
102        return this == EnumEvalResult.TRUE;
103    }
104}