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
010     * trunk/opends/resource/legal-notices/OpenDS.LICENSE
011     * or https://OpenDS.dev.java.net/OpenDS.LICENSE.
012     * See the License for the specific language governing permissions
013     * and limitations under the License.
014     *
015     * When distributing Covered Code, include this CDDL HEADER in each
016     * file and include the License file at
017     * trunk/opends/resource/legal-notices/OpenDS.LICENSE.  If applicable,
018     * add the following below this CDDL HEADER, with the fields enclosed
019     * by brackets "[]" replaced with your own identifying information:
020     *      Portions Copyright [yyyy] [name of copyright owner]
021     *
022     * CDDL HEADER END
023     *
024     *
025     *      Copyright 2008 Sun Microsystems, Inc.
026     */
027    
028    package org.opends.server.authorization.dseecompat;
029    
030    /**
031     * This class provides an enumeration of evaluation results returned by
032     * the bind rule evaluation methods.
033     */
034    public enum EnumEvalResult {
035    
036        /**
037         * This enumeration is returned when the result of the evaluation is TRUE.
038         */
039        TRUE(0),
040        /**
041         * This enumeration is returned when the result of the evaluation is FALSE.
042         */
043        FALSE(1),
044        /**
045         * This enumeration is returned when the result of the evaluation is FAIL.
046         * This should only be returned when a system failure occurred.
047         */
048        FAIL(2),
049        /**
050         * This is an internal enumeration used during evaluation of bind rule when
051         * internal processing of the evaluation is undefined. It is never returned
052         * back as a result of the evaluation.
053         */
054        ERR(3);
055    
056        /**
057         * Create a new enumeration type for the specified result value.
058         * @param v The value of the result.
059         */
060        EnumEvalResult(int v) {
061        }
062    
063        /**
064         * The method tries to determine if the result was undefined, and if so
065         * it returns an FAIL enumeration. If the result was not undefined (the
066         * common case for all of the bind rule evaluations), then the bind rule
067         * type is examined to see if the result needs to be flipped (type equals
068         * NOT_EQUAL_BINDRULE_TYPE).
069         * @param type The bind rule type enumeration of the bind rule.
070         * @param undefined  A flag that signals the the result was undefined.
071         * @return An enumeration containing the correct result after processing
072         * the undefined field and the bind rule type enumeration.
073         */
074        public EnumEvalResult getRet(EnumBindRuleType type, boolean undefined) {
075            EnumEvalResult ret=this;
076            if(this.equals(EnumEvalResult.TRUE) || !undefined) {
077                if(type.equals(EnumBindRuleType.NOT_EQUAL_BINDRULE_TYPE))
078                    if(this.equals(EnumEvalResult.TRUE))
079                        ret=EnumEvalResult.FALSE;
080                    else
081                        ret=EnumEvalResult.TRUE;
082            } else
083                ret=EnumEvalResult.FAIL;
084            return ret;
085        }
086    
087        /**
088         * This method is used to possibly negate the result of a simple bind rule
089         * evaluation. If the boolean is true than the result is negated.
090         * @param v The enumeration result of the simple bind rule evaluation.
091         * @param n If true the result should be negated (TRUE->FALSE, FALSE->TRUE).
092         * @return  A possibly negated enumeration result.
093         */
094        public  static EnumEvalResult negateIfNeeded(EnumEvalResult v, boolean n) {
095            if(n) {
096                if(v.equals(EnumEvalResult.TRUE))
097                    v=EnumEvalResult.FALSE;
098                else
099                    v=EnumEvalResult.TRUE;
100            }
101            return v;
102        }
103    
104        /**
105         * Helper method that converts this enumeration to a boolean. Usually the
106         * FAIL enumeration has been handled before this is called.
107         * @return True if the enumeration is TRUE, else false.
108         */
109        public boolean getBoolVal() {
110            return this == EnumEvalResult.TRUE;
111        }
112    }