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 *    http://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.model.message.controls;
021
022
023import org.apache.directory.api.i18n.I18n;
024
025
026/**
027 * Enumeration type for entry changes associates with the persistent search
028 * control and the entry change control. Used for the following ASN1
029 * enumeration:
030 * 
031 * <pre>
032 *   changeType ENUMERATED 
033 *   {
034 *       add             (1),
035 *       delete          (2),
036 *       modify          (4),
037 *       modDN           (8)
038 *   }
039 * </pre>
040 * 
041 * @author <a href="mailto:dev@directory.apache.org">Apache Directory Project</a>
042 */
043public enum ChangeType
044{
045    ADD(1),
046
047    DELETE(2),
048
049    MODIFY(4),
050
051    MODDN(8);
052
053    private int value;
054
055
056    /**
057     * 
058     * Creates a new instance of ChangeType.
059     *
060     * @param value The value for the ChangeType.
061     */
062    private ChangeType( int value )
063    {
064        this.value = value;
065    }
066
067
068    /**
069     * @return The int value of the ChangeType
070     */
071    public int getValue()
072    {
073        return value;
074    }
075
076
077    /**
078     * Checks via bitwise AND to see if this ChangeType value is within the
079     * supplied changeTypes.
080     *
081     * @param changeTypes The supplied changeTypes.
082     * @return true, if this ChangeType is present in the supplied changeTypes.
083     */
084    public boolean presentIn( int changeTypes )
085    {
086        return value == ( value & changeTypes );
087    }
088
089
090    /**
091     * Gets the changeType enumeration type for an integer value.
092     * 
093     * @param value the value to get the enumeration for
094     * @return the enueration type for the value if the value is valid
095     * @throws IllegalArgumentException if the value is undefined
096     */
097    public static ChangeType getChangeType( int value )
098    {
099        switch ( value )
100        {
101            case ( 1 ):
102                return ADD;
103            case ( 2 ):
104                return DELETE;
105            case ( 4 ):
106                return MODIFY;
107            case ( 8 ):
108                return MODDN;
109            default:
110                throw new IllegalArgumentException( I18n.err( I18n.ERR_04055, value ) );
111        }
112    }
113}