001/*
002 *  Licensed to the Apache Software Foundation (ASF) under one
003 *  or more contributor license agreements.  See the NOTICE file
004 *  distributed with this work for additional information
005 *  regarding copyright ownership.  The ASF licenses this file
006 *  to you under the Apache License, Version 2.0 (the
007 *  "License"); you may not use this file except in compliance
008 *  with the License.  You may obtain a copy of the License at
009 *
010 *    https://www.apache.org/licenses/LICENSE-2.0
011 *
012 *  Unless required by applicable law or agreed to in writing,
013 *  software distributed under the License is distributed on an
014 *  "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
015 *  KIND, either express or implied.  See the License for the
016 *  specific language governing permissions and limitations
017 *  under the License.
018 *
019 */
020package org.apache.directory.api.ldap.extras.controls.ad;
021
022import java.util.EnumSet;
023import java.util.Set;
024
025/**
026 * The flags used in the AdDirSync response.
027 *
028 * @author <a href="mailto:dev@directory.apache.org">Apache Directory Project</a>
029 */
030public enum AdDirSyncResponseFlag
031{
032    /** The Object Security flag */
033    LDAP_DIRSYNC_OBJECT_SECURITY( 0x0001, "Object Security" ),
034
035    /** The Ancestors First Order flag */
036    LDAP_DIRSYNC_ANCESTORS_FIRST_ORDER( 0x0800, "Ancestors First Order" ),
037    
038    /** The Public Data Only flag */
039    LDAP_DIRSYNC_PUBLIC_DATA_ONLY( 0x2000, "Public Data Only" ),
040    
041    /** The Incremental Values flag */
042    LDAP_DIRSYNC_INCREMENTAL_VALUES( 0x80000000, "Incremental Values" );
043
044    /** The int value */
045    private int value;
046
047    /** The string description **/
048    private String description;
049    
050    /** 
051     * A private constructor that associates a value and description to each flag
052     * 
053     * @param value The AdDirSync integer value
054     * @param description The AdDirSync description
055     **/
056    AdDirSyncResponseFlag( int value, String description )
057    {
058        this.value = value;
059        this.description = description;
060    }
061    
062    
063    /**
064     * @return The associated value of a given flag
065     */
066    public int getValue()
067    {
068        return value;
069    }
070    
071    
072    /**
073     * @see Object#toString()
074     */
075    @Override
076    public String toString()
077    {
078        return this.description;
079    }
080
081
082    /**
083     * Get back the combination of flags associated with a given value
084     * @param value The integer value
085     * @return a set of all flags associated with the integer value
086     */
087    public static Set<AdDirSyncResponseFlag> getFlags( int value )
088    {
089        EnumSet<AdDirSyncResponseFlag> result = EnumSet.noneOf( AdDirSyncResponseFlag.class );
090        for ( AdDirSyncResponseFlag flag : EnumSet.allOf( AdDirSyncResponseFlag.class ) )
091        {
092            if ( ( flag.getValue() & value ) == flag.getValue() )
093            {
094                result.add( flag );
095            }
096        }
097        return result;
098    }
099
100
101    /**
102     * Get back the bitmask (as an integer) associated with the given flags
103     * @param flags The AdDirSync flags
104     * @return a bitmask in integer form associated with the set of flags
105     */
106    public static int getBitmask( Set<AdDirSyncResponseFlag> flags )
107    {
108        int mask = 0;
109        
110        for ( AdDirSyncResponseFlag flag : flags )
111        {
112            mask += flag.getValue();
113        }
114        
115        return mask;
116    }
117}