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.codec.options;
021
022
023import org.apache.directory.shared.kerberos.flags.AbstractKerberosFlags;
024
025
026/**
027 * A base class to manage Kerberos BitSet elements.
028 *
029 * @author <a href="mailto:dev@directory.apache.org">Apache Directory Project</a>
030 */
031public abstract class Options extends AbstractKerberosFlags
032{
033    /**
034     * Creates a new BitSet with a specific number of bits.
035     * @param maxSize The number of bits to allocate
036     */
037    protected Options( int maxSize )
038    {
039        super( maxSize );
040    }
041
042
043    /**
044     * Returns whether the option at a given index matches the option in this {@link Options}.
045     *
046     * @param options The set of possible options
047     * @param option The Option we are looking for
048     * @return true if two options are the same.
049     */
050    public boolean match( Options options, int option )
051    {
052        return options.get( option ) == this.get( option );
053    }
054
055
056    /**
057     * Returns the value of the option at the given index.
058     *
059     * @param index The position in the BitSet for the option we are looking for
060     * @return true if the option at the given index is set.
061     */
062    public boolean get( int index )
063    {
064        if ( ( index < 0 ) || ( index >= size() ) )
065        {
066            throw new ArrayIndexOutOfBoundsException();
067        }
068
069        return super.getBit( index );
070    }
071
072
073    /**
074     * Sets the option at a given index.
075     *
076     * @param index The position of the Option w want to set
077     */
078    public void set( int index )
079    {
080        if ( ( index < 0 ) || ( index >= size() ) )
081        {
082            return;
083        }
084
085        setBit( index );
086    }
087
088
089    /**
090     * Clears (sets false) the option at a given index.
091     *
092     * @param index The position of the Option we want to clear
093     */
094    public void clear( int index )
095    {
096        if ( ( index < 0 ) || ( index >= size() ) )
097        {
098            return;
099        }
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}