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