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.shared.kerberos.flags;
021
022
023import org.apache.directory.api.asn1.util.BitString;
024
025
026/**
027 * An implementation of a BitString for any KerberosFlags. The different values
028 * are stored in an int, as there can't be more than 32 flags (TicketFlag).
029 *
030 * Some basic operations are implemented in this abstract class, like those
031 * manipulating flags.
032 *
033 * @author <a href="mailto:dev@directory.apache.org">Apache Directory Project</a>
034 */
035public abstract class AbstractKerberosFlags extends BitString
036{
037    /**
038     * The maximum size of the BitString as specified for Kerberos flags.
039     */
040    public static final int MAX_SIZE = 32;
041
042    /** The associated value */
043    protected int value;
044
045
046    /**
047     * Standard constructor, which create a BitString containing 32 bits
048     */
049    public AbstractKerberosFlags()
050    {
051        super( MAX_SIZE );
052        value = 0;
053    }
054
055
056    /**
057     * Standard constructor, which create a BitString containing 32 bits
058     *
059     *
060     * @param value The flags to store
061     */
062    public AbstractKerberosFlags( int value )
063    {
064        super( MAX_SIZE );
065
066        setData( value );
067    }
068
069
070    /**
071     * Store the flags contained in the given integer value
072     * @param value The list of flags to set, as a int
073     */
074    public void setData( int value )
075    {
076        byte[] bytes = new byte[5];
077
078        // The first byte contains the number of unused bytes, 0 here as we store 32 bits
079        bytes[0] = 0;
080
081        bytes[1] = ( byte ) ( value >> 24 );
082        bytes[2] = ( byte ) ( ( value >> 16 ) & 0x00FF );
083        bytes[3] = ( byte ) ( ( value >> 8 ) & 0x00FF );
084        bytes[4] = ( byte ) ( value & 0x00FF );
085
086        super.setData( bytes );
087        this.value = value;
088    }
089
090
091    /**
092     * Standard constructor, taking a byte array, 32 bits
093     */
094    public AbstractKerberosFlags( byte[] flags )
095    {
096        super( flags );
097
098        if ( ( flags == null ) || ( flags.length != 5 ) )
099        {
100            throw new IllegalArgumentException( "The given flags is not correct" );
101        }
102
103        value = ( ( flags[1] & 0x00FF ) << 24 ) | ( ( flags[2] & 0x00FF ) << 16 ) | ( ( flags[3] & 0x00FF ) << 8 )
104            | ( 0x00FF & flags[4] );
105    }
106
107
108    /**
109     * Returns the int value associated with the flags
110     */
111    public int getIntValue()
112    {
113        return value;
114    }
115
116
117    /**
118     * Check if a flag is set
119     * @param flags The flags to test
120     * @param flag The flag to check
121     * @return True if the flag is set in the list of flags
122     */
123    public static boolean isFlagSet( int flags, int flag )
124    {
125        return ( flags & ( 1 << ( MAX_SIZE - 1 - flag ) ) ) != 0;
126    }
127
128
129    /**
130     * Check if a flag is set for the actual value
131     *
132     * @param flag The flag to check
133     * @return True if the flag is set in the list of flags
134     */
135    public boolean isFlagSet( KerberosFlag flag )
136    {
137        int mask = 1 << ( MAX_SIZE - 1 - flag.getValue() );
138
139        return ( value & mask ) != 0;
140    }
141
142
143    /**
144     * Check if a flag is set
145     * @param flag The flags to test
146     * @return True if the flag is set in the list of flags
147     */
148    public boolean isFlagSet( int flag )
149    {
150        return ( value & ( 1 << ( MAX_SIZE - 1 - flag ) ) ) != 0;
151    }
152
153
154    /**
155     * Set a flag in a list of flags
156     *
157     * @param flag The flag to set
158     */
159    public void setFlag( KerberosFlag flag )
160    {
161        int pos = MAX_SIZE - 1 - flag.getValue();
162        setBit( flag.getValue() );
163        value |= 1 << pos;
164    }
165
166
167    /**
168     * Set a flag in a list of flags
169     *
170     * @param flag The flag to set
171     */
172    public void setFlag( int flag )
173    {
174        int pos = MAX_SIZE - 1 - flag;
175        setBit( flag );
176        value |= 1 << pos;
177    }
178
179
180    /**
181     * clear a flag in a list of flags
182     *
183     * @param flag The flag to set
184     */
185    public void clearFlag( KerberosFlag flag )
186    {
187        int pos = MAX_SIZE - 1 - flag.getValue();
188        clearBit( flag.getValue() );
189        value &= ~( 1 << pos );
190    }
191
192
193    /**
194     * clear a flag in a list of flags
195     *
196     * @param flag The flag to set
197     */
198    public void clearFlag( int flag )
199    {
200        int pos = MAX_SIZE - 1 - flag;
201        clearBit( flag );
202        value &= ~( 1 << pos );
203    }
204
205
206    @Override
207    public int hashCode()
208    {
209        final int prime = 31;
210        int result = 1;
211        result = prime * result + value;
212        return result;
213    }
214
215
216    /**
217     * {@inheritDoc}
218     */
219    @Override
220    public boolean equals( Object obj )
221    {
222        if ( this == obj )
223        {
224            return true;
225        }
226
227        if ( !( obj instanceof AbstractKerberosFlags ) )
228        {
229            return false;
230        }
231
232        AbstractKerberosFlags other = ( AbstractKerberosFlags ) obj;
233
234        return value == other.value;
235    }
236}