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.codec.options;
21  
22  
23  import org.apache.directory.shared.kerberos.flags.AbstractKerberosFlags;
24  
25  
26  /**
27   * A base class to manage Kerberos BitSet elements.
28   *
29   * @author <a href="mailto:dev@directory.apache.org">Apache Directory Project</a>
30   */
31  public abstract class Options extends AbstractKerberosFlags
32  {
33      /**
34       * Creates a new BitSet with a specific number of bits.
35       * @param maxSize The number of bits to allocate
36       */
37      protected Options( int maxSize )
38      {
39          super( maxSize );
40      }
41  
42  
43      /**
44       * Returns whether the option at a given index matches the option in this {@link Options}.
45       *
46       * @param options The set of possible options
47       * @param option The Option we are looking for
48       * @return true if two options are the same.
49       */
50      public boolean match( Options options, int option )
51      {
52          return options.get( option ) == this.get( option );
53      }
54  
55  
56      /**
57       * Returns the value of the option at the given index.
58       *
59       * @param index The position in the BitSet for the option we are looking for
60       * @return true if the option at the given index is set.
61       */
62      public boolean get( int index )
63      {
64          if ( ( index < 0 ) || ( index >= size() ) )
65          {
66              throw new ArrayIndexOutOfBoundsException();
67          }
68  
69          return super.getBit( index );
70      }
71  
72  
73      /**
74       * Sets the option at a given index.
75       *
76       * @param index The position of the Option w want to set
77       */
78      public void set( int index )
79      {
80          if ( ( index < 0 ) || ( index >= size() ) )
81          {
82              return;
83          }
84  
85          setBit( index );
86      }
87  
88  
89      /**
90       * Clears (sets false) the option at a given index.
91       *
92       * @param index The position of the Option we want to clear
93       */
94      public void clear( int index )
95      {
96          if ( ( index < 0 ) || ( index >= size() ) )
97          {
98              return;
99          }
100 
101         clearBit( index );
102     }
103 
104 
105     /**
106      * Byte-reversing methods are an anomaly of the BouncyCastle
107      * DERBitString endianness. These methods can be removed if the
108      * Apache Directory Snickers codecs operate differently.
109      *
110      * @return The raw {@link Options} bytes.
111      */
112     public byte[] getBytes()
113     {
114         return super.getData();
115     }
116 
117 
118     /**
119      * Set the array of bytes representing the bits
120      * @param bytes The bytes to store
121      */
122     protected void setBytes( byte[] bytes )
123     {
124         super.setData( bytes );
125     }
126 
127 
128     /**
129      * {@inheritDoc}
130      */
131     @Override
132     public String toString()
133     {
134         return super.toString();
135     }
136 }