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
010     * trunk/opends/resource/legal-notices/OpenDS.LICENSE
011     * or https://OpenDS.dev.java.net/OpenDS.LICENSE.
012     * See the License for the specific language governing permissions
013     * and limitations under the License.
014     *
015     * When distributing Covered Code, include this CDDL HEADER in each
016     * file and include the License file at
017     * trunk/opends/resource/legal-notices/OpenDS.LICENSE.  If applicable,
018     * add the following below this CDDL HEADER, with the fields enclosed
019     * by brackets "[]" replaced with your own identifying information:
020     *      Portions Copyright [yyyy] [name of copyright owner]
021     *
022     * CDDL HEADER END
023     *
024     *
025     *      Copyright 2008 Sun Microsystems, Inc.
026     */
027    package org.opends.server.admin.condition;
028    
029    
030    
031    import org.opends.server.admin.AbstractManagedObjectDefinition;
032    import org.opends.server.admin.client.AuthorizationException;
033    import org.opends.server.admin.client.CommunicationException;
034    import org.opends.server.admin.client.ManagedObject;
035    import org.opends.server.admin.client.ManagementContext;
036    import org.opends.server.admin.server.ServerManagedObject;
037    import org.opends.server.config.ConfigException;
038    
039    
040    
041    /**
042     * This class consists exclusively of static methods that operate on
043     * or return conditions.
044     */
045    public 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        /**
053         * {@inheritDoc}
054         */
055        public boolean evaluate(ManagementContext context,
056            ManagedObject<?> managedObject) throws AuthorizationException,
057            CommunicationException {
058          return false;
059        }
060    
061    
062    
063        /**
064         * {@inheritDoc}
065         */
066        public boolean evaluate(ServerManagedObject<?> managedObject)
067            throws ConfigException {
068          return false;
069        }
070    
071    
072    
073        /**
074         * {@inheritDoc}
075         */
076        public void initialize(AbstractManagedObjectDefinition<?, ?> d)
077            throws Exception {
078          // No implementation required.
079        }
080    
081      };
082    
083      /**
084       * A condition which always evaluates to <code>true</code>.
085       */
086      public static final Condition TRUE = new Condition() {
087    
088        /**
089         * {@inheritDoc}
090         */
091        public boolean evaluate(ManagementContext context,
092            ManagedObject<?> managedObject) throws AuthorizationException,
093            CommunicationException {
094          return true;
095        }
096    
097    
098    
099        /**
100         * {@inheritDoc}
101         */
102        public boolean evaluate(ServerManagedObject<?> managedObject)
103            throws ConfigException {
104          return true;
105        }
106    
107    
108    
109        /**
110         * {@inheritDoc}
111         */
112        public void initialize(AbstractManagedObjectDefinition<?, ?> d)
113            throws Exception {
114          // No implementation required.
115        }
116    
117      };
118    
119    
120    
121      /**
122       * Creates a condition which evaluates to <code>true</code> if and
123       * only if all of its sub-conditions are <code>true</code>.
124       *
125       * @param conditions
126       *          The sub-conditions which be combined using a logical
127       *          AND.
128       * @return Returns a condition which evaluates to <code>true</code>
129       *         if and only if all of its sub-conditions are
130       *         <code>true</code>.
131       */
132      public static Condition and(Condition... conditions) {
133        return new ANDCondition(conditions);
134      }
135    
136    
137    
138      /**
139       * Creates a condition which evaluates to <code>true</code> if and
140       * only if a property contains a particular value.
141       *
142       * @param propertyName
143       *          The property name.
144       * @param propertyStringValue
145       *          The string representation of the required property
146       *          value.
147       * @return Returns a condition which evaluates to <code>true</code>
148       *         if and only if a property contains a particular value.
149       */
150      public static Condition contains(String propertyName,
151          String propertyStringValue) {
152        return new ContainsCondition(propertyName, propertyStringValue);
153      }
154    
155    
156    
157      /**
158       * Creates a condition which evaluates to <code>false</code> if
159       * and only if the first sub-condition evaluates to
160       * <code>true</code> and the second sub-condition evaluates to
161       * <code>false</code>. This can be used to represent if-then
162       * relationships.
163       *
164       * @param premise
165       *          The sub-condition which, when <code>true</code>
166       *          implies that the implication sub-condition must also be
167       *          <code>true</code>.
168       * @param implication
169       *          The sub-condition which, must be <code>true</code>
170       *          when the premise is <code>true</code>.
171       * @return Returns a condition which evaluates to <code>false</code>
172       *         if and only if the first sub-condition evaluates to
173       *         <code>true</code> and the second sub-condition
174       *         evaluates to <code>false</code>.
175       */
176      public static Condition implies(Condition premise, Condition implication) {
177        return or(not(premise), implication);
178      }
179    
180    
181    
182      /**
183       * Creates a condition which evaluates to <code>true</code> if and
184       * only if a particular property has any values specified.
185       *
186       * @param propertyName
187       *          The property name.
188       * @return Returns a condition which evaluates to <code>true</code>
189       *         if and only if a particular property has any values
190       *         specified.
191       */
192      public static Condition isPresent(String propertyName) {
193        return new IsPresentCondition(propertyName);
194      }
195    
196    
197    
198      /**
199       * Creates a condition which evaluates to <code>true</code> if the
200       * sub-condition is <code>false</code>, or <code>false</code>
201       * if the sub-condition is <code>true</code>.
202       *
203       * @param condition
204       *          The sub-condition which will be inverted.
205       * @return Returns a condition which evaluates to <code>true</code>
206       *         if the sub-condition is <code>false</code>, or
207       *         <code>false</code> if the sub-condition is
208       *         <code>true</code>.
209       */
210      public static Condition not(Condition condition) {
211        return new NOTCondition(condition);
212      }
213    
214    
215    
216      /**
217       * Creates a condition which evaluates to <code>false</code> if
218       * and only if all of its sub-conditions are <code>false</code>.
219       *
220       * @param conditions
221       *          The sub-conditions which be combined using a logical OR.
222       * @return Returns a condition which evaluates to <code>false</code>
223       *         if and only if all of its sub-conditions are
224       *         <code>false</code>.
225       */
226      public static Condition or(Condition... conditions) {
227        return new ORCondition(conditions);
228      }
229    
230    
231    
232      // Prevent instantiation.
233      private Conditions() {
234        // No implementation required.
235      }
236    
237    }