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