001/*
002 * Copyright 2007-2019 Ping Identity Corporation
003 * All Rights Reserved.
004 */
005/*
006 * Copyright (C) 2008-2019 Ping Identity Corporation
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 */
021package com.unboundid.ldap.sdk.controls;
022
023
024
025import java.util.Collection;
026import java.util.EnumSet;
027import java.util.Set;
028
029import com.unboundid.util.StaticUtils;
030import com.unboundid.util.ThreadSafety;
031import com.unboundid.util.ThreadSafetyLevel;
032
033
034
035/**
036 * This enum defines a set of change types that can be associated with
037 * persistent search operations.  Change types may be used in the
038 * {@link PersistentSearchRequestControl}, as well as in
039 * {@link EntryChangeNotificationControl}s returned in search result entries
040 * as part of a persistent search.
041 */
042@ThreadSafety(level=ThreadSafetyLevel.COMPLETELY_THREADSAFE)
043public enum PersistentSearchChangeType
044{
045  /**
046   * Indicates that the change type is for an add operation.
047   */
048  ADD("add", 1),
049
050
051
052  /**
053   * Indicates that the change type is for a delete operation.
054   */
055  DELETE("delete", 2),
056
057
058
059  /**
060   * Indicates that the change type is for a modify operation.
061   */
062  MODIFY("modify", 4),
063
064
065
066  /**
067   * Indicates that the change type is for a modify DN operation.
068   */
069  MODIFY_DN("moddn", 8);
070
071
072
073  // The numeric value associated with this change type.
074  private final int value;
075
076  // The human-readable name for this change type.
077  private final String name;
078
079
080
081  /**
082   * Creates a new persistent search change type with the provided information.
083   *
084   * @param  name   The human-readable name for this change type.
085   * @param  value  The numeric value associated with this change type.
086   */
087  PersistentSearchChangeType(final String name, final int value)
088  {
089    this.name  = name;
090    this.value = value;
091  }
092
093
094
095  /**
096   * Retrieves the human-readable name for this change type.
097   *
098   * @return  The human-readable name for this change type.
099   */
100  public String getName()
101  {
102    return name;
103  }
104
105
106
107  /**
108   * Retrieves the integer value for this change type.
109   *
110   * @return  The integer value for this change type.
111   */
112  public int intValue()
113  {
114    return value;
115  }
116
117
118
119  /**
120   * Retrieves the persistent search change type with the specified int value.
121   *
122   * @param  intValue  the numeric value associated with the change type.
123   *
124   * @return  The associated change type, or {@code null} if there is no
125   *          persistent search change type with the specified set of values.
126   */
127  public static PersistentSearchChangeType valueOf(final int intValue)
128  {
129    switch (intValue)
130    {
131      case 1:
132        return ADD;
133
134      case 2:
135        return DELETE;
136
137      case 4:
138        return MODIFY;
139
140      case 8:
141        return MODIFY_DN;
142
143      default:
144        return null;
145    }
146  }
147
148
149
150  /**
151   * Retrieves the persistent search change type with the specified name.
152   *
153   * @param  name  The name of the change type for which to retrieve the name.
154   *               It must not be {@code null}.
155   *
156   * @return  The requested persistent search change type, or {@code null} if
157   *          there is no change type with the given name.
158   */
159  public static PersistentSearchChangeType forName(final String name)
160  {
161    switch (StaticUtils.toLowerCase(name))
162    {
163      case "add":
164        return ADD;
165      case "delete":
166      case "del":
167        return DELETE;
168      case "modify":
169      case "mod":
170        return MODIFY;
171      case "modifydn":
172      case "modify-dn":
173      case "modify_dn":
174      case "moddn":
175      case "mod-dn":
176      case "mod_dn":
177      case "modifyrdn":
178      case "modify-rdn":
179      case "modify_rdn":
180      case "modrdn":
181      case "mod-rdn":
182      case "mod_rdn":
183        return MODIFY_DN;
184      default:
185        return null;
186    }
187  }
188
189
190
191  /**
192   * Retrieves a set containing all defined change types.
193   *
194   * @return  A set containing all defined change types.
195   */
196  public static Set<PersistentSearchChangeType> allChangeTypes()
197  {
198    return EnumSet.allOf(PersistentSearchChangeType.class);
199  }
200
201
202
203  /**
204   * Encodes the provided set of change types into an integer value suitable for
205   * use as the change types for the persistent search request control.
206   *
207   * @param  changeTypes  The set of change types to be encoded.
208   *
209   * @return  An integer value containing the encoded representation of the
210   *          change types.
211   */
212  public static int encodeChangeTypes(
213                         final PersistentSearchChangeType... changeTypes)
214  {
215    int changeTypesValue = 0;
216
217    for (final PersistentSearchChangeType changeType : changeTypes)
218    {
219      changeTypesValue |= changeType.intValue();
220    }
221
222    return changeTypesValue;
223  }
224
225
226
227  /**
228   * Encodes the provided set of change types into an integer value suitable for
229   * use as the change types for the persistent search request control.
230   *
231   * @param  changeTypes  The set of change types to be encoded.
232   *
233   * @return  An integer value containing the encoded representation of the
234   *          change types.
235   */
236  public static int encodeChangeTypes(
237       final Collection<PersistentSearchChangeType> changeTypes)
238  {
239    int changeTypesValue = 0;
240
241    for (final PersistentSearchChangeType changeType : changeTypes)
242    {
243      changeTypesValue |= changeType.intValue();
244    }
245
246    return changeTypesValue;
247  }
248
249
250
251  /**
252   * Decodes the provided set of change types from the provided value.
253   *
254   * @param  changeTypes  The int value representing the encoded set of change
255   *                      types.
256   *
257   * @return  A list of the change types encoded in the provided value.
258   */
259  public static Set<PersistentSearchChangeType> decodeChangeTypes(
260                                                      final int changeTypes)
261  {
262    final EnumSet<PersistentSearchChangeType> ctSet =
263         EnumSet.noneOf(PersistentSearchChangeType.class);
264
265    if ((changeTypes & ADD.intValue()) == ADD.intValue())
266    {
267      ctSet.add(ADD);
268    }
269
270    if ((changeTypes & DELETE.intValue()) == DELETE.intValue())
271    {
272      ctSet.add(DELETE);
273    }
274
275    if ((changeTypes & MODIFY.intValue()) == MODIFY.intValue())
276    {
277      ctSet.add(MODIFY);
278    }
279
280    if ((changeTypes & MODIFY_DN.intValue()) == MODIFY_DN.intValue())
281    {
282      ctSet.add(MODIFY_DN);
283    }
284
285    return ctSet;
286  }
287
288
289
290  /**
291   * Retrieves a string representation for this persistent search change type.
292   *
293   * @return  A string representation for this persistent search change type.
294   */
295  @Override()
296  public String toString()
297  {
298    return name;
299  }
300}