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