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 2009-2010 Sun Microsystems, Inc.
015 * Portions Copyright 2016 ForgeRock AS.
016 */
017package org.forgerock.opendj.ldap;
018
019import org.forgerock.util.Reject;
020
021/** A modification to be performed on an entry during a Modify operation. */
022public final class Modification {
023    private final ModificationType modificationType;
024    private final Attribute attribute;
025
026    /**
027     * Creates a new modification having the provided modification type and
028     * attribute values to be updated. Note that while the returned
029     * {@code Modification} is immutable, the underlying attribute may not be.
030     * The following code ensures that the returned {@code Modification} is
031     * fully immutable:
032     *
033     * <pre>
034     * Modification change = new Modification(modificationType, Attributes
035     *         .unmodifiableAttribute(attribute));
036     * </pre>
037     *
038     * @param modificationType
039     *            The type of modification to be performed.
040     * @param attribute
041     *            The the attribute containing the values to be modified.
042     */
043    public Modification(final ModificationType modificationType, final Attribute attribute) {
044        Reject.ifNull(modificationType, attribute);
045        this.modificationType = modificationType;
046        this.attribute = attribute;
047    }
048
049    /**
050     * Returns the attribute containing the values to be modified.
051     *
052     * @return The the attribute containing the values to be modified.
053     */
054    public Attribute getAttribute() {
055        return attribute;
056    }
057
058    /**
059     * Returns the type of modification to be performed.
060     *
061     * @return The type of modification to be performed.
062     */
063    public ModificationType getModificationType() {
064        return modificationType;
065    }
066
067    @Override
068    public String toString() {
069        final StringBuilder builder = new StringBuilder();
070        builder.append("Modification(modificationType=");
071        builder.append(modificationType);
072        builder.append(", attributeDescription=");
073        builder.append(attribute.getAttributeDescriptionAsString());
074        builder.append(", attributeValues={");
075        boolean firstValue = true;
076        for (final ByteString value : attribute) {
077            if (!firstValue) {
078                builder.append(", ");
079            }
080            builder.append(value);
081            firstValue = false;
082        }
083        builder.append("})");
084        return builder.toString();
085    }
086}