View Javadoc

1   /*** 
2    * 
3    * Copyright 2004 Protique Ltd
4    * 
5    * Licensed under the Apache License, Version 2.0 (the "License"); 
6    * you may not use this file except in compliance with the License. 
7    * You may obtain a copy of the License at 
8    * 
9    * http://www.apache.org/licenses/LICENSE-2.0
10   * 
11   * Unless required by applicable law or agreed to in writing, software
12   * distributed under the License is distributed on an "AS IS" BASIS, 
13   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 
14   * See the License for the specific language governing permissions and 
15   * limitations under the License. 
16   * 
17   **/
18  package org.codehaus.activemq.util;
19  
20  import java.io.DataInput;
21  import java.io.DataOutput;
22  import java.io.IOException;
23  
24  /***
25   * Simple BitArray to enable setting multiple boolean values efficently Used instead of BitSet because BitSet does not
26   * allow for efficent serialization.
27   * Will store up to 64 boolean values
28   * 
29   * @version $Revision: 1.4 $
30   */
31  public class BitArray {
32      static final int LONG_SIZE = 64;
33      static final int INT_SIZE = 32;
34      static final int SHORT_SIZE = 16;
35      static final int BYTE_SIZE = 8;
36      private static final long[] BIT_VALUES = {0x0000000000000001L, 0x0000000000000002L, 0x0000000000000004L,
37                                                0x0000000000000008L, 0x0000000000000010L, 0x0000000000000020L, 0x0000000000000040L, 0x0000000000000080L,
38                                                0x0000000000000100L, 0x0000000000000200L, 0x0000000000000400L, 0x0000000000000800L, 0x0000000000001000L,
39                                                0x0000000000002000L, 0x0000000000004000L, 0x0000000000008000L, 0x0000000000010000L, 0x0000000000020000L,
40                                                0x0000000000040000L, 0x0000000000080000L, 0x0000000000100000L, 0x0000000000200000L, 0x0000000000400000L,
41                                                0x0000000000800000L, 0x0000000001000000L, 0x0000000002000000L, 0x0000000004000000L, 0x0000000008000000L,
42                                                0x0000000010000000L, 0x0000000020000000L, 0x0000000040000000L, 0x0000000080000000L, 0x0000000100000000L,
43                                                0x0000000200000000L, 0x0000000400000000L, 0x0000000800000000L, 0x0000001000000000L, 0x0000002000000000L,
44                                                0x0000004000000000L, 0x0000008000000000L, 0x0000010000000000L, 0x0000020000000000L, 0x0000040000000000L,
45                                                0x0000080000000000L, 0x0000100000000000L, 0x0000200000000000L, 0x0000400000000000L, 0x0000800000000000L,
46                                                0x0001000000000000L, 0x0002000000000000L, 0x0004000000000000L, 0x0008000000000000L, 0x0010000000000000L,
47                                                0x0020000000000000L, 0x0040000000000000L, 0x0080000000000000L, 0x0100000000000000L, 0x0200000000000000L,
48                                                0x0400000000000000L, 0x0800000000000000L, 0x1000000000000000L, 0x2000000000000000L, 0x4000000000000000L,
49                                                0x8000000000000000L};
50      private long bits;
51      private int length;
52  
53      /***
54       * @return the length of bits set
55       */
56      public int length() {
57          return length;
58      }
59  
60      /***
61       * @return the long containing the bits
62       */
63      public long getBits() {
64          return bits;
65      }
66  
67      /***
68       * set the boolean value at the index
69       *
70       * @param index
71       * @param flag
72       * @return the old value held at this index
73       */
74      public boolean set(int index, boolean flag) {
75          length = Math.max(length, index + 1);
76          boolean oldValue = (bits & BIT_VALUES[index]) != 0;
77          if (flag) {
78              bits |= BIT_VALUES[index];
79          }
80          else if (oldValue) {
81              bits &= ~(BIT_VALUES[index]);
82          }
83          return oldValue;
84      }
85  
86      /***
87       * @param index
88       * @return the boolean value at this index
89       */
90      public boolean get(int index) {
91          return (bits & BIT_VALUES[index]) != 0;
92      }
93      
94      /***
95       * reset all the bit values to false
96       */
97      public void reset(){
98          bits = 0;
99      }
100 
101     /***
102      * write the bits to an output stream
103      *
104      * @param dataOut
105      * @throws IOException
106      */
107     public void writeToStream(DataOutput dataOut) throws IOException {
108         dataOut.writeByte(length);
109         if (length <= BYTE_SIZE) {
110             dataOut.writeByte((int) bits);
111         }
112         else if (length <= SHORT_SIZE) {
113             dataOut.writeShort((short) bits);
114         }
115         else if (length <= INT_SIZE) {
116             dataOut.writeInt((int) bits);
117         }
118         else {
119             dataOut.writeLong(bits);
120         }
121     }
122 
123     /***
124      * read the bits from an input stream
125      *
126      * @param dataIn
127      * @throws IOException
128      */
129     public void readFromStream(DataInput dataIn) throws IOException {
130         length = dataIn.readByte();
131         if (length <= BYTE_SIZE) {
132             bits = (long) dataIn.readByte();
133         }
134         else if (length <= SHORT_SIZE) {
135             bits = (long) dataIn.readShort();
136         }
137         else if (length <= INT_SIZE) {
138             bits = (int) dataIn.readInt();
139         }
140         else {
141             bits = dataIn.readLong();
142         }
143     }
144 }