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 2014-2016 ForgeRock AS.
016 */
017package org.forgerock.opendj.config.conditions;
018
019import org.forgerock.opendj.config.AbstractManagedObjectDefinition;
020import org.forgerock.opendj.config.client.ManagedObject;
021import org.forgerock.opendj.config.client.ManagementContext;
022import org.forgerock.opendj.config.server.ConfigException;
023import org.forgerock.opendj.config.server.ServerManagedObject;
024import org.forgerock.opendj.ldap.LdapException;
025
026/** This class consists exclusively of static methods that operate on or return conditions. */
027public final class Conditions {
028
029    /** A condition which always evaluates to <code>false</code>. */
030    public static final Condition FALSE = new Condition() {
031
032        @Override
033        public boolean evaluate(ManagementContext context, ManagedObject<?> managedObject) throws LdapException {
034            return false;
035        }
036
037        @Override
038        public boolean evaluate(ServerManagedObject<?> managedObject) throws ConfigException {
039            return false;
040        }
041
042        @Override
043        public void initialize(AbstractManagedObjectDefinition<?, ?> d) throws Exception {
044            // No implementation required.
045        }
046
047    };
048
049    /** A condition which always evaluates to <code>true</code>. */
050    public static final Condition TRUE = new Condition() {
051
052        @Override
053        public boolean evaluate(ManagementContext context, ManagedObject<?> managedObject) throws LdapException {
054            return true;
055        }
056
057        @Override
058        public boolean evaluate(ServerManagedObject<?> managedObject) throws ConfigException {
059            return true;
060        }
061
062        @Override
063        public void initialize(AbstractManagedObjectDefinition<?, ?> d) throws Exception {
064            // No implementation required.
065        }
066
067    };
068
069    /**
070     * Creates a condition which evaluates to <code>true</code> if and only if
071     * all of its sub-conditions are <code>true</code>.
072     *
073     * @param conditions
074     *            The sub-conditions which be combined using a logical AND.
075     * @return Returns a condition which evaluates to <code>true</code> if and
076     *         only if all of its sub-conditions are <code>true</code>.
077     */
078    public static Condition and(Condition... conditions) {
079        return new ANDCondition(conditions);
080    }
081
082    /**
083     * Creates a condition which evaluates to <code>true</code> if and only if a
084     * property contains a particular value.
085     *
086     * @param propertyName
087     *            The property name.
088     * @param propertyStringValue
089     *            The string representation of the required property value.
090     * @return Returns a condition which evaluates to <code>true</code> if and
091     *         only if a property contains a particular value.
092     */
093    public static Condition contains(String propertyName, String propertyStringValue) {
094        return new ContainsCondition(propertyName, propertyStringValue);
095    }
096
097    /**
098     * Creates a condition which evaluates to <code>false</code> if and only if
099     * the first sub-condition evaluates to <code>true</code> and the second
100     * sub-condition evaluates to <code>false</code>. This can be used to
101     * represent if-then relationships.
102     *
103     * @param premise
104     *            The sub-condition which, when <code>true</code> implies that
105     *            the implication sub-condition must also be <code>true</code>.
106     * @param implication
107     *            The sub-condition which, must be <code>true</code> when the
108     *            premise is <code>true</code>.
109     * @return Returns a condition which evaluates to <code>false</code> if and
110     *         only if the first sub-condition evaluates to <code>true</code>
111     *         and the second sub-condition evaluates to <code>false</code>.
112     */
113    public static Condition implies(Condition premise, Condition implication) {
114        return or(not(premise), implication);
115    }
116
117    /**
118     * Creates a condition which evaluates to <code>true</code> if and only if a
119     * particular property has any values specified.
120     *
121     * @param propertyName
122     *            The property name.
123     * @return Returns a condition which evaluates to <code>true</code> if and
124     *         only if a particular property has any values specified.
125     */
126    public static Condition isPresent(String propertyName) {
127        return new IsPresentCondition(propertyName);
128    }
129
130    /**
131     * Creates a condition which evaluates to <code>true</code> if the
132     * sub-condition is <code>false</code>, or <code>false</code> if the
133     * sub-condition is <code>true</code>.
134     *
135     * @param condition
136     *            The sub-condition which will be inverted.
137     * @return Returns a condition which evaluates to <code>true</code> if the
138     *         sub-condition is <code>false</code>, or <code>false</code> if the
139     *         sub-condition is <code>true</code>.
140     */
141    public static Condition not(Condition condition) {
142        return new NOTCondition(condition);
143    }
144
145    /**
146     * Creates a condition which evaluates to <code>false</code> if and only if
147     * all of its sub-conditions are <code>false</code>.
148     *
149     * @param conditions
150     *            The sub-conditions which be combined using a logical OR.
151     * @return Returns a condition which evaluates to <code>false</code> if and
152     *         only if all of its sub-conditions are <code>false</code>.
153     */
154    public static Condition or(Condition... conditions) {
155        return new ORCondition(conditions);
156    }
157
158    /** Prevent instantiation. */
159    private Conditions() {
160        // No implementation required.
161    }
162
163}