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