001    /*
002     * Copyright 2007-2016 UnboundID Corp.
003     * All Rights Reserved.
004     */
005    /*
006     * Copyright (C) 2008-2016 UnboundID Corp.
007     *
008     * This program is free software; you can redistribute it and/or modify
009     * it under the terms of the GNU General Public License (GPLv2 only)
010     * or the terms of the GNU Lesser General Public License (LGPLv2.1 only)
011     * as published by the Free Software Foundation.
012     *
013     * This program is distributed in the hope that it will be useful,
014     * but WITHOUT ANY WARRANTY; without even the implied warranty of
015     * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
016     * GNU General Public License for more details.
017     *
018     * You should have received a copy of the GNU General Public License
019     * along with this program; if not, see <http://www.gnu.org/licenses>.
020     */
021    package com.unboundid.ldap.sdk;
022    
023    
024    
025    import java.io.Serializable;
026    import java.util.HashMap;
027    
028    
029    
030    
031    /**
032     * This class defines a data type for modification type values.  Clients should
033     * generally use one of the {@code ADD}, {@code DELETE}, {@code REPLACE}, or
034     * {@code INCREMENT} values, although it is possible to create a new
035     * modification type with a specified integer value if necessary using the
036     * {@code valueOf(int)} method.  The following modification types are defined:
037     * <UL>
038     *   <LI>{@code ADD} -- Indicates that the provided value(s) should be added to
039     *       the specified attribute in the target entry.  If the attribute does not
040     *       already exist, it will be created.  If it does exist, then the new
041     *       values will be merged added to the existing values.  At least one value
042     *       must be provided with the {@code ADD} modification type, and none of
043     *       those values will be allowed to exist in the entry.</LI>
044     *   <LI>{@code DELETE} -- Indicates that the specified attribute or attribute
045     *       values should be removed from the entry.  If no values are provided,
046     *       then the entire attribute will be removed.  If one or more values are
047     *       given, then only those values will be removed.  If any values are
048     *       provided, then all of those values must exist in the target entry.</LI>
049     *   <LI>{@code REPLACE} -- Indicates that the set of values for the specified
050     *       attribute should be replaced with the provided value(s).  If no values
051     *       are given, then the specified attribute will be removed from the entry
052     *       if it exists, or no change will be made.  If one or more values are
053     *       provided, then those values will replace the existing values if the
054     *       attribute already exists, or a new attribute will be added with those
055     *       values if there was previously no such attribute in the entry.</LI>
056     *   <LI>{@code INCREMENT} -- Indicates that the value of the specified
057     *       attribute should be incremented.  The target entry must have exactly
058     *       one value for the specified attribute and it must be an integer.  The
059     *       modification must include exactly one value, and it must be an integer
060     *       which specifies the amount by which the existing value is to be
061     *       incremented (or decremented, if the provided value is negative).</LI>
062     * </UL>
063     */
064    public final class ModificationType
065           implements Serializable
066    {
067      /**
068       * The integer value for the "add" modification type.
069       */
070      public static final int ADD_INT_VALUE = 0;
071    
072    
073    
074      /**
075       * A predefined add modification type, which indicates that the associated
076       * value(s) should be added to the specified attribute in the target entry.
077       * If the attribute does not already exist, it will be created.  If it does
078       * exist, then the new values will be merged added to the existing values.  At
079       * least one value must be provided with the {@code ADD} modification type,
080       * and none of those values will be allowed to exist in the entry.
081       */
082      public static final ModificationType ADD =
083           new ModificationType("ADD", ADD_INT_VALUE);
084    
085    
086    
087      /**
088       * The integer value for the "delete" modification type.
089       */
090      public static final int DELETE_INT_VALUE = 1;
091    
092    
093    
094      /**
095       * A predefined delete modification type, which indicates that the specified
096       * attribute or attribute values should be removed from the entry.  If no
097       * values are provided, then the entire attribute will be removed.  If one or
098       * more values are given, then only those values will be removed.  If any
099       * values are provided, then all of those values must exist in the target
100       * entry.
101       */
102      public static final ModificationType DELETE =
103           new ModificationType("DELETE", DELETE_INT_VALUE);
104    
105    
106    
107      /**
108       * The integer value for the "replace" modification type.
109       */
110      public static final int REPLACE_INT_VALUE = 2;
111    
112    
113    
114      /**
115       * A predefined replace modification type, which indicates that the set of
116       * values for the specified attribute should be replaced with the provided
117       * value(s).  If no values are given, then the specified attribute will be
118       * removed from the entry if it exists, or no change will be made.  If one or
119       * more values are provided, then those values will replace the existing
120       * values if the attribute already exists, or a new attribute will be added
121       * with those values if there was previously no such attribute in the entry.
122       */
123      public static final ModificationType REPLACE =
124           new ModificationType("REPLACE", REPLACE_INT_VALUE);
125    
126    
127    
128      /**
129       * The integer value for the "increment" modification type.
130       */
131      public static final int INCREMENT_INT_VALUE = 3;
132    
133    
134    
135      /**
136       * A predefined increment modification type, which indicates that the value of
137       * the specified attribute should be incremented.  The target entry must have
138       * exactly one value for the specified attribute and it must be an integer.
139       * The modification must include exactly one value, and it must be an integer
140       * which specifies the amount by which the existing value is to be incremented
141       * (or decremented, if the provided value is negative).
142       */
143      public static final ModificationType INCREMENT =
144           new ModificationType("INCREMENT", INCREMENT_INT_VALUE);
145    
146    
147    
148      /**
149       * The set of result code objects created with undefined int result code
150       * values.
151       */
152      private static final HashMap<Integer,ModificationType> UNDEFINED_MOD_TYPES =
153           new HashMap<Integer,ModificationType>();
154    
155    
156    
157      /**
158       * The serial version UID for this serializable class.
159       */
160      private static final long serialVersionUID = -7863114394728980308L;
161    
162    
163    
164      // The integer value for this modification type.
165      private final int intValue;
166    
167      // The name to use for this modification type.
168      private final String name;
169    
170    
171    
172      /**
173       * Creates a new modification type with the specified integer value.
174       *
175       * @param  intValue  The integer value to use for this modification type.
176       */
177      private ModificationType(final int intValue)
178      {
179        this.intValue = intValue;
180    
181        name = String.valueOf(intValue);
182      }
183    
184    
185    
186      /**
187       * Creates a new modification type with the specified name and integer value.
188       *
189       * @param  name      The name to use for this modification type.
190       * @param  intValue  The integer value to use for this modification type.
191       */
192      private ModificationType(final String name, final int intValue)
193      {
194        this.name     = name;
195        this.intValue = intValue;
196      }
197    
198    
199    
200      /**
201       * Retrieves the name for this modification type.
202       *
203       * @return  The name for this modification type.
204       */
205      public String getName()
206      {
207        return name;
208      }
209    
210    
211    
212      /**
213       * Retrieves the integer value for this modification type.
214       *
215       * @return  The integer value for this modification type.
216       */
217      public int intValue()
218      {
219        return intValue;
220      }
221    
222    
223    
224      /**
225       * Retrieves the modification type with the specified integer value.
226       *
227       * @param  intValue  The integer value for which to retrieve the corresponding
228       *                   modification type.
229       *
230       * @return  The modification type with the specified integer value, or a new
231       *          modification type if the provided value does not match any of the
232       *          predefined modification types.
233       */
234      public static ModificationType valueOf(final int intValue)
235      {
236        switch (intValue)
237        {
238          case 0:
239            return ADD;
240          case 1:
241            return DELETE;
242          case 2:
243            return REPLACE;
244          case 3:
245            return INCREMENT;
246          default:
247            synchronized (UNDEFINED_MOD_TYPES)
248            {
249              ModificationType t = UNDEFINED_MOD_TYPES.get(intValue);
250              if (t == null)
251              {
252                t = new ModificationType(intValue);
253                UNDEFINED_MOD_TYPES.put(intValue, t);
254              }
255    
256              return t;
257            }
258        }
259      }
260    
261    
262    
263      /**
264       * Retrieves the predefined modification type with the specified integer
265       * value.
266       *
267       * @param  intValue  The integer value for which to retrieve the corresponding
268       *                   modification type.
269       *
270       * @return  The modification type with the specified integer value, or
271       *          {@code null} if the provided integer value does not represent a
272       *          defined modification type.
273       */
274      public static ModificationType definedValueOf(final int intValue)
275      {
276        switch (intValue)
277        {
278          case 0:
279            return ADD;
280          case 1:
281            return DELETE;
282          case 2:
283            return REPLACE;
284          case 3:
285            return INCREMENT;
286          default:
287            return null;
288        }
289      }
290    
291    
292    
293      /**
294       * Retrieves an array of all modification types defined in the LDAP SDK.
295       *
296       * @return  An array of all modification types defined in the LDAP SDK.
297       */
298      public static ModificationType[] values()
299      {
300        return new ModificationType[]
301        {
302          ADD,
303          DELETE,
304          REPLACE,
305          INCREMENT
306        };
307      }
308    
309    
310    
311      /**
312       * The hash code for this modification type.
313       *
314       * @return  The hash code for this modification type.
315       */
316      @Override()
317      public int hashCode()
318      {
319        return intValue;
320      }
321    
322    
323    
324      /**
325       * Indicates whether the provided object is equal to this modification type.
326       *
327       * @param  o  The object for which to make the determination.
328       *
329       * @return  {@code true} if the provided object is a modification type that is
330       *          equal to this modification type, or {@code false} if not.
331       */
332      @Override()
333      public boolean equals(final Object o)
334      {
335        if (o == null)
336        {
337          return false;
338        }
339        else if (o == this)
340        {
341          return true;
342        }
343        else if (o instanceof ModificationType)
344        {
345          return (intValue == ((ModificationType) o).intValue);
346        }
347        else
348        {
349          return false;
350        }
351      }
352    
353    
354    
355      /**
356       * Retrieves a string representation of this modification type.
357       *
358       * @return  A string representation of this modification type.
359       */
360      @Override()
361      public String toString()
362      {
363        return name;
364      }
365    }