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