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.shared.kerberos.flags;
21  
22  
23  import org.apache.directory.api.asn1.util.BitString;
24  
25  
26  /**
27   * An implementation of a BitString for any KerberosFlags. The different values
28   * are stored in an int, as there can't be more than 32 flags (TicketFlag).
29   *
30   * Some basic operations are implemented in this abstract class, like those
31   * manipulating flags.
32   *
33   * @author <a href="mailto:dev@directory.apache.org">Apache Directory Project</a>
34   */
35  public abstract class AbstractKerberosFlags extends BitString
36  {
37      /**
38       * The maximum size of the BitString as specified for Kerberos flags.
39       */
40      public static final int MAX_SIZE = 32;
41  
42      /** The associated value */
43      protected int value;
44  
45  
46      /**
47       * Standard constructor, which create a BitString containing 32 bits
48       */
49      public AbstractKerberosFlags()
50      {
51          super( MAX_SIZE );
52          value = 0;
53      }
54  
55  
56      /**
57       * Standard constructor, which create a BitString containing 32 bits
58       *
59       *
60       * @param value The flags to store
61       */
62      public AbstractKerberosFlags( int value )
63      {
64          super( MAX_SIZE );
65  
66          setData( value );
67      }
68  
69  
70      /**
71       * Store the flags contained in the given integer value
72       * @param value The list of flags to set, as a int
73       */
74      public void setData( int value )
75      {
76          byte[] bytes = new byte[5];
77  
78          // The first byte contains the number of unused bytes, 0 here as we store 32 bits
79          bytes[0] = 0;
80  
81          bytes[1] = ( byte ) ( value >> 24 );
82          bytes[2] = ( byte ) ( ( value >> 16 ) & 0x00FF );
83          bytes[3] = ( byte ) ( ( value >> 8 ) & 0x00FF );
84          bytes[4] = ( byte ) ( value & 0x00FF );
85  
86          super.setData( bytes );
87          this.value = value;
88      }
89  
90  
91      /**
92       * Standard constructor, taking a byte array, 32 bits
93       */
94      public AbstractKerberosFlags( byte[] flags )
95      {
96          super( flags );
97  
98          if ( ( flags == null ) || ( flags.length != 5 ) )
99          {
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../org/apache/directory/shared/kerberos/flags/AbstractKerberosFlags.html#AbstractKerberosFlags">AbstractKerberosFlags other = ( AbstractKerberosFlags ) obj;
233 
234         return value == other.value;
235     }
236 }