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
010     * trunk/opends/resource/legal-notices/OpenDS.LICENSE
011     * or https://OpenDS.dev.java.net/OpenDS.LICENSE.
012     * See the License for the specific language governing permissions
013     * and limitations under the License.
014     *
015     * When distributing Covered Code, include this CDDL HEADER in each
016     * file and include the License file at
017     * trunk/opends/resource/legal-notices/OpenDS.LICENSE.  If applicable,
018     * add the following below this CDDL HEADER, with the fields enclosed
019     * by brackets "[]" replaced with your own identifying information:
020     *      Portions Copyright [yyyy] [name of copyright owner]
021     *
022     * CDDL HEADER END
023     *
024     *
025     *      Copyright 2006-2008 Sun Microsystems, Inc.
026     */
027    package org.opends.server.types;
028    
029    
030    
031    import static org.opends.server.util.ServerConstants.*;
032    
033    
034    
035    /**
036     * This class defines a data structure for storing and interacting
037     * with a modification that may be requested of an entry in the
038     * Directory Server.
039     */
040    @org.opends.server.types.PublicAPI(
041         stability=org.opends.server.types.StabilityLevel.UNCOMMITTED,
042         mayInstantiate=true,
043         mayExtend=false,
044         mayInvoke=true)
045    public final class Modification
046    {
047      // The attribute for this modification.
048      private Attribute attribute;
049    
050      // Indicates whether this modification was generated by internal
051      // processing and therefore should not be subject to
052      // no-user-modification and related checks.
053      private boolean isInternal;
054    
055      // The modification type for this modification.
056      private ModificationType modificationType;
057    
058    
059    
060      /**
061       * Creates a new modification with the provided information.
062       *
063       * @param  modificationType  The modification type for this
064       *                           modification.
065       * @param  attribute         The attribute for this modification.
066       */
067      public Modification(ModificationType modificationType,
068                          Attribute attribute)
069      {
070        this.modificationType = modificationType;
071        this.attribute        = attribute;
072    
073        isInternal = false;
074      }
075    
076    
077    
078      /**
079       * Creates a new modification with the provided information.
080       *
081       * @param  modificationType  The modification type for this
082       *                           modification.
083       * @param  attribute         The attribute for this modification.
084       * @param  isInternal        Indicates whether this is an internal
085       *                           modification and therefore should not
086       *                           be subject to no-user-modification and
087       *                           related checks.
088       */
089      public Modification(ModificationType modificationType,
090                          Attribute attribute, boolean isInternal)
091      {
092        this.modificationType = modificationType;
093        this.attribute        = attribute;
094        this.isInternal       = isInternal;
095      }
096    
097    
098    
099      /**
100       * Retrieves the modification type for this modification.
101       *
102       * @return  The modification type for this modification.
103       */
104      public ModificationType getModificationType()
105      {
106        return modificationType;
107      }
108    
109    
110    
111      /**
112       * Specifies the modification type for this modification.
113       *
114       * @param  modificationType  The modification type for this
115       *                           modification.
116       */
117      @org.opends.server.types.PublicAPI(
118           stability=org.opends.server.types.StabilityLevel.PRIVATE,
119           mayInstantiate=false,
120           mayExtend=false,
121           mayInvoke=false)
122      public void setModificationType(ModificationType modificationType)
123      {
124        this.modificationType = modificationType;
125      }
126    
127    
128    
129      /**
130       * Retrieves the attribute for this modification.
131       *
132       * @return  The attribute for this modification.
133       */
134      public Attribute getAttribute()
135      {
136        return attribute;
137      }
138    
139    
140    
141      /**
142       * Specifies the attribute for this modification.
143       *
144       * @param  attribute  The attribute for this modification.
145       */
146      @org.opends.server.types.PublicAPI(
147           stability=org.opends.server.types.StabilityLevel.PRIVATE,
148           mayInstantiate=false,
149           mayExtend=false,
150           mayInvoke=false)
151      public void setAttribute(Attribute attribute)
152      {
153        this.attribute = attribute;
154      }
155    
156    
157    
158      /**
159       * Indicates whether this is modification was created by internal
160       * processing and should not be subject to no-user-modification and
161       * related checks.
162       *
163       * @return  <CODE>true</CODE> if this is an internal modification,
164       *          or <CODE>false</CODE> if not.
165       */
166      public boolean isInternal()
167      {
168        return isInternal;
169      }
170    
171    
172    
173      /**
174       * Specifies whether this modification was created by internal
175       * processing and should not be subject to no-user-modification and
176       * related checks.
177       *
178       * @param  isInternal  Specifies whether this modification was
179       *                     created by internal processing and should
180       *                     not be subject to no-user-modification and
181       *                     related checks.
182       */
183      public void setInternal(boolean isInternal)
184      {
185        this.isInternal = isInternal;
186      }
187    
188    
189    
190      /**
191       * Indicates whether the provided object is equal to this
192       * modification.  It will only be considered equal if the object is
193       * a modification with the same modification type and an attribute
194       * that is equal to this modification.
195       *
196       * @param  o  The object for which to make the determination.
197       *
198       * @return  <CODE>true</CODE> if the provided object is a
199       *          modification that is equal to this modification, or
200       *          <CODE>false</CODE> if not.
201       */
202      public boolean equals(Object o)
203      {
204        if (this == o)
205        {
206          return true;
207        }
208    
209        if ((o == null) || (! (o instanceof Modification)))
210        {
211          return false;
212        }
213    
214        Modification m = (Modification) o;
215        if (modificationType != m.modificationType)
216        {
217          return false;
218        }
219    
220        return attribute.equals(m.attribute);
221      }
222    
223    
224    
225      /**
226       * Retrieves the hash code for this modification.  The hash code
227       * returned will be the hash code for the attribute included in this
228       * modification.
229       *
230       * @return  The hash code for this modification.
231       */
232      public int hashCode()
233      {
234        return attribute.hashCode();
235      }
236    
237    
238    
239      /**
240       * Retrieves a one-line string representation of this modification.
241       *
242       * @return  A one-line string representation of this modification.
243       */
244      public String toString()
245      {
246        StringBuilder buffer = new StringBuilder();
247        toString(buffer);
248        return buffer.toString();
249      }
250    
251    
252    
253      /**
254       * Appends a one-line representation of this modification to the
255       * provided buffer.
256       *
257       * @param  buffer  The buffer to which the information should be
258       *                 appended.
259       */
260      public void toString(StringBuilder buffer)
261      {
262        buffer.append("Modification(");
263        buffer.append(modificationType.toString());
264        buffer.append(", ");
265        buffer.append(attribute.toString());
266      }
267    
268    
269    
270      /**
271       * Retrieves a string representation of this modification in LDIF
272       * form.
273       *
274       * @return  A string representation of this modification in LDIF
275       *          form.
276       */
277      public String toLDIF()
278      {
279        StringBuilder buffer = new StringBuilder();
280        toLDIF(buffer);
281        return buffer.toString();
282      }
283    
284    
285    
286      /**
287       * Appends a string representation of this modification in LDIF form
288       * to the provided buffer.
289       *
290       * @param  buffer  The buffer to which the information should be
291       *                 appended.
292       */
293      public void toLDIF(StringBuilder buffer)
294      {
295        buffer.append(modificationType.toString());
296        buffer.append(": ");
297        buffer.append(attribute.getName());
298        buffer.append(EOL);
299    
300        attribute.toLDIF(buffer);
301      }
302    }
303