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