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.forgerock.opendj.config.conditions;
028
029import java.util.SortedSet;
030
031import org.forgerock.opendj.config.AbstractManagedObjectDefinition;
032import org.forgerock.opendj.config.PropertyDefinition;
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 a property
042 * contains a particular value.
043 */
044public final class ContainsCondition implements Condition {
045
046    /**
047     * The strongly typed underlying implementation.
048     *
049     * @param <T>
050     *            The type of the property value being tested.
051     */
052    private static final class Impl<T> implements Condition {
053
054        /** The property. */
055        final PropertyDefinition<T> pd;
056
057        /** The required property value. */
058        final T value;
059
060        /** Private constructor. */
061        private Impl(PropertyDefinition<T> pd, T value) {
062            this.pd = pd;
063            this.value = value;
064        }
065
066        /** {@inheritDoc} */
067        public boolean evaluate(ManagementContext context, ManagedObject<?> managedObject) throws LdapException {
068            SortedSet<T> values = managedObject.getPropertyValues(pd);
069            return values.contains(value);
070        }
071
072        /** {@inheritDoc} */
073        public boolean evaluate(ServerManagedObject<?> managedObject) throws ConfigException {
074            SortedSet<T> values = managedObject.getPropertyValues(pd);
075            return values.contains(value);
076        }
077
078        /** {@inheritDoc} */
079        public void initialize(AbstractManagedObjectDefinition<?, ?> d) throws Exception {
080            // Not used.
081        }
082
083        /** Private implementation of fix() method. */
084        private void setPropertyValue(ManagedObject<?> managedObject) {
085            managedObject.setPropertyValue(pd, value);
086        }
087
088    }
089
090    /** The strongly typed private implementation. */
091    private Impl<?> impl;
092
093    /** The property name. */
094    private final String propertyName;
095
096    /** The string representation of the required property value. */
097    private final String propertyStringValue;
098
099    /**
100     * Creates a new contains value condition.
101     *
102     * @param propertyName
103     *            The property name.
104     * @param stringValue
105     *            The string representation of the required property value.
106     */
107    public ContainsCondition(String propertyName, String stringValue) {
108        Reject.ifNull(propertyName, stringValue);
109        this.propertyName = propertyName;
110        this.propertyStringValue = stringValue;
111    }
112
113    /** {@inheritDoc} */
114    public boolean evaluate(ManagementContext context, ManagedObject<?> managedObject) throws LdapException {
115        return impl.evaluate(context, managedObject);
116    }
117
118    /** {@inheritDoc} */
119    public boolean evaluate(ServerManagedObject<?> managedObject) throws ConfigException {
120        return impl.evaluate(managedObject);
121    }
122
123    /**
124     * Modifies the provided managed object so that it has the property value
125     * associated with this condition.
126     *
127     * @param managedObject
128     *            The managed object.
129     */
130    public void setPropertyValue(ManagedObject<?> managedObject) {
131        impl.setPropertyValue(managedObject);
132    }
133
134    /** {@inheritDoc} */
135    public void initialize(AbstractManagedObjectDefinition<?, ?> d) throws Exception {
136        // Decode the property.
137        buildImpl(d.getPropertyDefinition(propertyName));
138    }
139
140    /** Creates the new private implementation. */
141    private <T> void buildImpl(PropertyDefinition<T> pd) {
142        T value = pd.decodeValue(propertyStringValue);
143        this.impl = new Impl<>(pd, value);
144    }
145
146    /**
147     * Returns the property definition associated with this condition.
148     *
149     * @return the property definition associated with this condition.
150     */
151    public PropertyDefinition<?> getPropertyDefinition() {
152        return impl.pd;
153    }
154
155    /**
156     * Returns the value that must be set for this condition to be fulfilled.
157     *
158     * @return the value that must be set for this condition to be fulfilled.
159     */
160    public Object getValue() {
161        return impl.value;
162    }
163}