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