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 2006-2008 Sun Microsystems, Inc.
015 * Portions Copyright 2014-2016 ForgeRock AS.
016 */
017package org.opends.server.types;
018
019import org.forgerock.opendj.ldap.ModificationType;
020
021/**
022 * This class defines a data structure for storing and interacting
023 * with a modification that may be requested of an entry in the Directory Server.
024 */
025@org.opends.server.types.PublicAPI(
026     stability=org.opends.server.types.StabilityLevel.UNCOMMITTED,
027     mayInstantiate=true,
028     mayExtend=false,
029     mayInvoke=true)
030public final class Modification
031{
032  /** The attribute for this modification. */
033  private Attribute attribute;
034  /** The modification type for this modification. */
035  private ModificationType modificationType;
036  /**
037   * Indicates whether this modification was generated by internal processing
038   * and therefore should not be subject to no-user-modification and related checks.
039   */
040  private boolean isInternal;
041
042  /**
043   * Creates a new modification with the provided information.
044   *
045   * @param  modificationType  The modification type for this modification.
046   * @param  attribute         The attribute for this modification.
047   */
048  public Modification(ModificationType modificationType, Attribute attribute)
049  {
050    this(modificationType, attribute, false);
051  }
052
053  /**
054   * Creates a new modification with the provided information.
055   *
056   * @param  modificationType  The modification type for this modification.
057   * @param  attribute         The attribute for this modification.
058   * @param  isInternal        Indicates whether this is an internal modification
059   *                           and therefore should not be subject to
060   *                           no-user-modification and related checks.
061   */
062  public Modification(ModificationType modificationType, Attribute attribute, boolean isInternal)
063  {
064    this.modificationType = modificationType;
065    this.attribute        = attribute;
066    this.isInternal       = isInternal;
067  }
068
069  /**
070   * Retrieves the modification type for this modification.
071   *
072   * @return  The modification type for this modification.
073   */
074  public ModificationType getModificationType()
075  {
076    return modificationType;
077  }
078
079  /**
080   * Specifies the modification type for this modification.
081   *
082   * @param  modificationType  The modification type for this modification.
083   */
084  @org.opends.server.types.PublicAPI(
085       stability=org.opends.server.types.StabilityLevel.PRIVATE,
086       mayInstantiate=false,
087       mayExtend=false,
088       mayInvoke=false)
089  public void setModificationType(ModificationType modificationType)
090  {
091    this.modificationType = modificationType;
092  }
093
094  /**
095   * Retrieves the attribute for this modification.
096   *
097   * @return  The attribute for this modification.
098   */
099  public Attribute getAttribute()
100  {
101    return attribute;
102  }
103
104  /**
105   * Specifies the attribute for this modification.
106   *
107   * @param  attribute  The attribute for this modification.
108   */
109  @org.opends.server.types.PublicAPI(
110       stability=org.opends.server.types.StabilityLevel.PRIVATE,
111       mayInstantiate=false,
112       mayExtend=false,
113       mayInvoke=false)
114  public void setAttribute(Attribute attribute)
115  {
116    this.attribute = attribute;
117  }
118
119  /**
120   * Indicates whether this is modification was created by internal processing
121   * and should not be subject to no-user-modification and related checks.
122   *
123   * @return  {@code true} if this is an internal modification, or {@code false} if not.
124   */
125  public boolean isInternal()
126  {
127    return isInternal;
128  }
129
130  /**
131   * Specifies whether this modification was created by internal processing
132   * and should not be subject to no-user-modification and related checks.
133   *
134   * @param  isInternal  Specifies whether this modification was created
135   *                     by internal processing and should not be subject to
136   *                     no-user-modification and related checks.
137   */
138  public void setInternal(boolean isInternal)
139  {
140    this.isInternal = isInternal;
141  }
142
143  /**
144   * Indicates whether the provided object is equal to this modification.
145   * It will only be considered equal if the object is a modification
146   * with the same modification type and an attribute that is equal to this modification.
147   *
148   * @param  o  The object for which to make the determination.
149   * @return  {@code true} if the provided object is a
150   *          modification that is equal to this modification,
151   *          or {@code false} if not.
152   */
153  @Override
154  public boolean equals(Object o)
155  {
156    if (this == o)
157    {
158      return true;
159    }
160    if (!(o instanceof Modification))
161    {
162      return false;
163    }
164
165    Modification m = (Modification) o;
166    return modificationType == m.modificationType
167        && attribute.equals(m.attribute);
168  }
169
170  /**
171   * Retrieves the hash code for this modification.  The hash code
172   * returned will be the hash code for the attribute included in this modification.
173   *
174   * @return  The hash code for this modification.
175   */
176  @Override
177  public int hashCode()
178  {
179    return attribute.hashCode();
180  }
181
182  /**
183   * Retrieves a one-line string representation of this modification.
184   *
185   * @return  A one-line string representation of this modification.
186   */
187  @Override
188  public String toString()
189  {
190    StringBuilder buffer = new StringBuilder();
191    toString(buffer);
192    return buffer.toString();
193  }
194
195  /**
196   * Appends a one-line representation of this modification to the provided buffer.
197   *
198   * @param  buffer  The buffer to which the information should be appended.
199   */
200  private void toString(StringBuilder buffer)
201  {
202    buffer.append("Modification(").append(modificationType).append(", ").append(attribute);
203  }
204}