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 java.util.Arrays;
022import java.util.List;
023
024import org.opends.server.admin.AbstractManagedObjectDefinition;
025import org.opends.server.admin.client.AuthorizationException;
026import org.opends.server.admin.client.CommunicationException;
027import org.opends.server.admin.client.ManagedObject;
028import org.opends.server.admin.client.ManagementContext;
029import org.opends.server.admin.server.ServerManagedObject;
030import org.forgerock.opendj.config.server.ConfigException;
031import org.forgerock.util.Reject;
032
033
034
035/**
036 * A condition which evaluates to <code>false</code> if and only if
037 * all of its sub-conditions are <code>false</code>.
038 */
039public final class ORCondition implements Condition {
040
041  /** The list of sub-conditions. */
042  private final List<Condition> conditions;
043
044
045
046  /**
047   * Creates a new logical OR condition with the provided
048   * sub-conditions.
049   *
050   * @param conditions
051   *          The sub-conditions which will be combined using a
052   *          logical OR.
053   */
054  public ORCondition(Condition... conditions) {
055    Reject.ifNull(conditions);
056    this.conditions = Arrays.asList(conditions);
057  }
058
059
060
061  /** {@inheritDoc} */
062  public boolean evaluate(ManagementContext context,
063      ManagedObject<?> managedObject) throws AuthorizationException,
064      CommunicationException {
065    for (Condition condition : conditions) {
066      if (condition.evaluate(context, managedObject)) {
067        return true;
068      }
069    }
070    return false;
071  }
072
073
074
075  /** {@inheritDoc} */
076  public boolean evaluate(ServerManagedObject<?> managedObject)
077      throws ConfigException {
078    for (Condition condition : conditions) {
079      if (condition.evaluate(managedObject)) {
080        return true;
081      }
082    }
083    return false;
084  }
085
086
087
088  /** {@inheritDoc} */
089  public void initialize(AbstractManagedObjectDefinition<?, ?> d)
090      throws Exception {
091    for (Condition condition : conditions) {
092      condition.initialize(d);
093    }
094  }
095
096}