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