001/*
002 * Copyright 2007-2020 Ping Identity Corporation
003 * All Rights Reserved.
004 */
005/*
006 * Copyright 2007-2020 Ping Identity Corporation
007 *
008 * Licensed under the Apache License, Version 2.0 (the "License");
009 * you may not use this file except in compliance with the License.
010 * You may obtain a copy of the License at
011 *
012 *    http://www.apache.org/licenses/LICENSE-2.0
013 *
014 * Unless required by applicable law or agreed to in writing, software
015 * distributed under the License is distributed on an "AS IS" BASIS,
016 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
017 * See the License for the specific language governing permissions and
018 * limitations under the License.
019 */
020/*
021 * Copyright (C) 2008-2020 Ping Identity Corporation
022 *
023 * This program is free software; you can redistribute it and/or modify
024 * it under the terms of the GNU General Public License (GPLv2 only)
025 * or the terms of the GNU Lesser General Public License (LGPLv2.1 only)
026 * as published by the Free Software Foundation.
027 *
028 * This program is distributed in the hope that it will be useful,
029 * but WITHOUT ANY WARRANTY; without even the implied warranty of
030 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
031 * GNU General Public License for more details.
032 *
033 * You should have received a copy of the GNU General Public License
034 * along with this program; if not, see <http://www.gnu.org/licenses>.
035 */
036package com.unboundid.ldap.sdk.controls;
037
038
039
040import java.util.Collection;
041import java.util.EnumSet;
042import java.util.Set;
043
044import com.unboundid.util.StaticUtils;
045import com.unboundid.util.ThreadSafety;
046import com.unboundid.util.ThreadSafetyLevel;
047
048
049
050/**
051 * This enum defines a set of change types that can be associated with
052 * persistent search operations.  Change types may be used in the
053 * {@link PersistentSearchRequestControl}, as well as in
054 * {@link EntryChangeNotificationControl}s returned in search result entries
055 * as part of a persistent search.
056 */
057@ThreadSafety(level=ThreadSafetyLevel.COMPLETELY_THREADSAFE)
058public enum PersistentSearchChangeType
059{
060  /**
061   * Indicates that the change type is for an add operation.
062   */
063  ADD("add", 1),
064
065
066
067  /**
068   * Indicates that the change type is for a delete operation.
069   */
070  DELETE("delete", 2),
071
072
073
074  /**
075   * Indicates that the change type is for a modify operation.
076   */
077  MODIFY("modify", 4),
078
079
080
081  /**
082   * Indicates that the change type is for a modify DN operation.
083   */
084  MODIFY_DN("moddn", 8);
085
086
087
088  // The numeric value associated with this change type.
089  private final int value;
090
091  // The human-readable name for this change type.
092  private final String name;
093
094
095
096  /**
097   * Creates a new persistent search change type with the provided information.
098   *
099   * @param  name   The human-readable name for this change type.
100   * @param  value  The numeric value associated with this change type.
101   */
102  PersistentSearchChangeType(final String name, final int value)
103  {
104    this.name  = name;
105    this.value = value;
106  }
107
108
109
110  /**
111   * Retrieves the human-readable name for this change type.
112   *
113   * @return  The human-readable name for this change type.
114   */
115  public String getName()
116  {
117    return name;
118  }
119
120
121
122  /**
123   * Retrieves the integer value for this change type.
124   *
125   * @return  The integer value for this change type.
126   */
127  public int intValue()
128  {
129    return value;
130  }
131
132
133
134  /**
135   * Retrieves the persistent search change type with the specified int value.
136   *
137   * @param  intValue  the numeric value associated with the change type.
138   *
139   * @return  The associated change type, or {@code null} if there is no
140   *          persistent search change type with the specified set of values.
141   */
142  public static PersistentSearchChangeType valueOf(final int intValue)
143  {
144    switch (intValue)
145    {
146      case 1:
147        return ADD;
148
149      case 2:
150        return DELETE;
151
152      case 4:
153        return MODIFY;
154
155      case 8:
156        return MODIFY_DN;
157
158      default:
159        return null;
160    }
161  }
162
163
164
165  /**
166   * Retrieves the persistent search change type with the specified name.
167   *
168   * @param  name  The name of the change type for which to retrieve the name.
169   *               It must not be {@code null}.
170   *
171   * @return  The requested persistent search change type, or {@code null} if
172   *          there is no change type with the given name.
173   */
174  public static PersistentSearchChangeType forName(final String name)
175  {
176    switch (StaticUtils.toLowerCase(name))
177    {
178      case "add":
179        return ADD;
180      case "delete":
181      case "del":
182        return DELETE;
183      case "modify":
184      case "mod":
185        return MODIFY;
186      case "modifydn":
187      case "modify-dn":
188      case "modify_dn":
189      case "moddn":
190      case "mod-dn":
191      case "mod_dn":
192      case "modifyrdn":
193      case "modify-rdn":
194      case "modify_rdn":
195      case "modrdn":
196      case "mod-rdn":
197      case "mod_rdn":
198        return MODIFY_DN;
199      default:
200        return null;
201    }
202  }
203
204
205
206  /**
207   * Retrieves a set containing all defined change types.
208   *
209   * @return  A set containing all defined change types.
210   */
211  public static Set<PersistentSearchChangeType> allChangeTypes()
212  {
213    return EnumSet.allOf(PersistentSearchChangeType.class);
214  }
215
216
217
218  /**
219   * Encodes the provided set of change types into an integer value suitable for
220   * use as the change types for the persistent search request control.
221   *
222   * @param  changeTypes  The set of change types to be encoded.
223   *
224   * @return  An integer value containing the encoded representation of the
225   *          change types.
226   */
227  public static int encodeChangeTypes(
228                         final PersistentSearchChangeType... changeTypes)
229  {
230    int changeTypesValue = 0;
231
232    for (final PersistentSearchChangeType changeType : changeTypes)
233    {
234      changeTypesValue |= changeType.intValue();
235    }
236
237    return changeTypesValue;
238  }
239
240
241
242  /**
243   * Encodes the provided set of change types into an integer value suitable for
244   * use as the change types for the persistent search request control.
245   *
246   * @param  changeTypes  The set of change types to be encoded.
247   *
248   * @return  An integer value containing the encoded representation of the
249   *          change types.
250   */
251  public static int encodeChangeTypes(
252       final Collection<PersistentSearchChangeType> changeTypes)
253  {
254    int changeTypesValue = 0;
255
256    for (final PersistentSearchChangeType changeType : changeTypes)
257    {
258      changeTypesValue |= changeType.intValue();
259    }
260
261    return changeTypesValue;
262  }
263
264
265
266  /**
267   * Decodes the provided set of change types from the provided value.
268   *
269   * @param  changeTypes  The int value representing the encoded set of change
270   *                      types.
271   *
272   * @return  A list of the change types encoded in the provided value.
273   */
274  public static Set<PersistentSearchChangeType> decodeChangeTypes(
275                                                      final int changeTypes)
276  {
277    final EnumSet<PersistentSearchChangeType> ctSet =
278         EnumSet.noneOf(PersistentSearchChangeType.class);
279
280    if ((changeTypes & ADD.intValue()) == ADD.intValue())
281    {
282      ctSet.add(ADD);
283    }
284
285    if ((changeTypes & DELETE.intValue()) == DELETE.intValue())
286    {
287      ctSet.add(DELETE);
288    }
289
290    if ((changeTypes & MODIFY.intValue()) == MODIFY.intValue())
291    {
292      ctSet.add(MODIFY);
293    }
294
295    if ((changeTypes & MODIFY_DN.intValue()) == MODIFY_DN.intValue())
296    {
297      ctSet.add(MODIFY_DN);
298    }
299
300    return ctSet;
301  }
302
303
304
305  /**
306   * Retrieves a string representation for this persistent search change type.
307   *
308   * @return  A string representation for this persistent search change type.
309   */
310  @Override()
311  public String toString()
312  {
313    return name;
314  }
315}