View Javadoc
1   /*
2    *  Licensed to the Apache Software Foundation (ASF) under one
3    *  or more contributor license agreements.  See the NOTICE file
4    *  distributed with this work for additional information
5    *  regarding copyright ownership.  The ASF licenses this file
6    *  to you under the Apache License, Version 2.0 (the
7    *  "License"); you may not use this file except in compliance
8    *  with the License.  You may obtain a copy of the License at
9    *
10   *    http://www.apache.org/licenses/LICENSE-2.0
11   *
12   *  Unless required by applicable law or agreed to in writing,
13   *  software distributed under the License is distributed on an
14   *  "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
15   *  KIND, either express or implied.  See the License for the
16   *  specific language governing permissions and limitations
17   *  under the License.
18   *
19   */
20  package org.apache.directory.api.ldap.extras.controls.ad;
21  
22  import java.util.EnumSet;
23  import java.util.Set;
24  
25  /**
26   * The flags used in the AdDirSync response.
27   *
28   * @author <a href="mailto:dev@directory.apache.org">Apache Directory Project</a>
29   */
30  public enum AdDirSyncFlag
31  {
32      /** The Object Security flag */
33      LDAP_DIRSYNC_OBJECT_SECURITY( 0x0001, "Object Security" ),
34  
35      /** The Ancestors First Order flag */
36      LDAP_DIRSYNC_ANCESTORS_FIRST_ORDER( 0x0800, "Ancestors First Order" ),
37      
38      /** The Public Data Only flag */
39      LDAP_DIRSYNC_PUBLIC_DATA_ONLY( 0x2000, "Public Data Only" ),
40      
41      /** The Incremental Values flag */
42      LDAP_DIRSYNC_INCREMENTAL_VALUES( 0x80000000, "Incremental Values" );
43  
44      /** The int value */
45      private int value;
46  
47      /** The string description **/
48      private String description;
49  
50      /** A private constructor that associates a value and description to each flag */
51      AdDirSyncFlag( int value, String description )
52      {
53          this.value = value;
54          this.description = description;
55      }
56  
57  
58      /**
59       * @return The associated value of a given flag
60       */
61      public int getValue()
62      {
63          return value;
64      }
65  
66  
67      /**
68       * @see Object#toString()
69       */
70      @Override
71      public String toString()
72      {
73          return this.description;
74      }
75  
76  
77      /**
78       * Get back the combination of flags associated with a given value
79       * @param value The integer value
80       * @return a set of all flags associated with the integer value
81       */
82      public static Set<AdDirSyncFlag> getFlags( int value )
83      {
84          EnumSet<AdDirSyncFlag> result = EnumSet.noneOf( AdDirSyncFlag.class );
85          for ( AdDirSyncFlag flag : EnumSet.allOf( AdDirSyncFlag.class ) )
86          {
87              if ( ( flag.getValue() & value ) == flag.getValue() )
88              {
89                  result.add( flag );
90              }
91          }
92          return result;
93      }
94  
95      /**
96       * Get back the bitmask (as an integer) associated with the given flags
97       * @param flags The AdDirSync flags
98       * @return a bitmask in integer form associated with the set of flags
99       */
100     public static int getBitmask( Set<AdDirSyncFlag> flags )
101     {
102         int mask = 0;
103         
104         for ( AdDirSyncFlag flag : flags )
105         {
106             mask += flag.getValue();
107         }
108         
109         return mask;
110     }
111 }