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