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