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.server.kerberos.shared.messages.value;
21
22
23 import java.util.BitSet;
24
25
26 /**
27 * @author <a href="mailto:dev@directory.apache.org">Apache Directory Project</a>
28 * @version $Rev: 540371 $, $Date: 2007-05-22 02:00:43 +0200 (Di, 22 Mai 2007) $
29 */
30 public abstract class Options
31 {
32 private BitSet options;
33 private int maxSize;
34
35
36 protected Options( int maxSize )
37 {
38 this.maxSize = maxSize;
39 options = new BitSet( 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
47 * @param option
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
60 * @return true if the option at the given index is set.
61 */
62 public boolean get( int index )
63 {
64 return options.get( index );
65 }
66
67
68 /**
69 * Sets the option at a given index.
70 *
71 * @param index
72 */
73 public void set( int index )
74 {
75 options.set( index );
76 }
77
78
79 /**
80 * Clears (sets false) the option at a given index.
81 *
82 * @param index
83 */
84 public void clear( int index )
85 {
86 options.clear( index );
87 }
88
89
90 /**
91 * Byte-reversing methods are an anomaly of the BouncyCastle
92 * DERBitString endianness. Thes methods can be removed if the
93 * Apache Directory Snickers codecs operate differently.
94 *
95 * @return The raw {@link Options} bytes.
96 */
97 public byte[] getBytes()
98 {
99 byte[] bytes = new byte[maxSize / 8];
100
101 for ( int ii = 0; ii < maxSize; ii++ )
102 {
103 if ( options.get( reversePosition( ii ) ) )
104 {
105 bytes[bytes.length - ii / 8 - 1] |= 1 << ( ii % 8 );
106 }
107 }
108 return bytes;
109 }
110
111
112 protected void setBytes( byte[] bytes )
113 {
114 for ( int ii = 0; ii < bytes.length * 8; ii++ )
115 {
116 if ( ( bytes[bytes.length - ii / 8 - 1] & ( 1 << ( ii % 8 ) ) ) > 0 )
117 {
118 options.set( reversePosition( ii ) );
119 }
120 }
121 }
122
123
124 private int reversePosition( int position )
125 {
126 return maxSize - 1 - position;
127 }
128 }