KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > derby > iapi > util > JBitSet


1 /*
2
3    Derby - Class com.ihost.cs.JBitSet
4
5    Licensed to the Apache Software Foundation (ASF) under one or more
6    contributor license agreements. See the NOTICE file distributed with
7    this work for additional information regarding copyright ownership.
8    The ASF licenses this file to you under the Apache License, Version 2.0
9    (the "License"); you may not use this file except in compliance with
10    the License. You may obtain a copy of the License at
11
12       http://www.apache.org/licenses/LICENSE-2.0
13
14    Unless required by applicable law or agreed to in writing, software
15    distributed under the License is distributed on an "AS IS" BASIS,
16    WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
17    See the License for the specific language governing permissions and
18    limitations under the License.
19
20  */

21
22 package org.apache.derby.iapi.util;
23
24 import org.apache.derby.iapi.services.sanity.SanityManager;
25
26 import java.util.BitSet JavaDoc;
27
28 /**
29  * JBitSet is a wrapper class for BitSet. It is a fixed length implementation
30  * which can be extended via the grow() method. It provides additional
31  * methods to manipulate BitSets.
32  * NOTE: JBitSet was driven by the (current and perceived) needs of the
33  * optimizer, but placed in the util package since it is not specific to
34  * query trees..
35  * NOTE: java.util.BitSet is final, so we must provide a wrapper class
36  * which includes a BitSet member in order to extend the functionality.
37  * We want to make it look like JBitSet extends BitSet, so we need to
38  * provide wrapper methods for all of BitSet's methods.
39  *
40  * @author Jerry Brenner
41  */

42 public final class JBitSet
43 {
44     /* The BitSet that we'd like to extend */
45     private final BitSet JavaDoc bitSet;
46     /* Cache size() of bitSet, since accessed a lot */
47     private int size;
48
49     /**
50      * Construct a JBitSet of the specified size.
51      *
52      * @param size The number of bits in the JBitSet.
53      */

54     public JBitSet(int size)
55     {
56         bitSet = new BitSet JavaDoc(size);
57         this.size = size;
58     }
59
60     /**
61      * Construct a JBitSet with the specified bitSet.
62      *
63      * @param bitSet The BitSet.
64      * @param size The size of bitSet.
65      * NOTE: We need to specify the size since the size of a
66      * BitSet is not guaranteed to be the same as JBitSet.size().
67      */

68     private JBitSet(BitSet JavaDoc bitSet, int size)
69     {
70         this.bitSet = bitSet;
71         this.size = size;
72     }
73
74     /**
75      * Set the BitSet to have the exact same bits set as the parameter's BitSet.
76      *
77      * @param sourceBitSet The JBitSet to copy.
78      */

79     public void setTo(JBitSet sourceBitSet)
80     {
81         if (SanityManager.DEBUG)
82         {
83             SanityManager.ASSERT(size == sourceBitSet.size(),
84                                  "JBitSets are expected to be the same size");
85         }
86         /* High reuse solution */
87         and(sourceBitSet);
88         or(sourceBitSet);
89     }
90
91     /**
92      * Test to see if one JBitSet contains another one of
93      * the same size.
94      *
95      * @param jBitSet JBitSet that we want to know if it is
96      * a subset of current JBitSet
97      *
98      * @return boolean Whether or not jBitSet is a subset.
99      */

100     public boolean contains(JBitSet jBitSet)
101     {
102         if (SanityManager.DEBUG)
103         {
104             SanityManager.ASSERT(size == jBitSet.size(),
105                                  "JBitSets are expected to be the same size");
106         }
107         for (int bitIndex = 0; bitIndex < size; bitIndex++)
108         {
109             if (jBitSet.bitSet.get(bitIndex) && ! (bitSet.get(bitIndex)))
110             {
111                 return false;
112             }
113         }
114         return true;
115     }
116
117     /**
118      * See of a JBitSet has exactly 1 bit set.
119      *
120      * @return boolean Whether or not JBitSet has a single bit set.
121      */

122     public boolean hasSingleBitSet()
123     {
124         boolean found = false;
125
126         for (int bitIndex = 0; bitIndex < size; bitIndex++)
127         {
128             if (bitSet.get(bitIndex))
129             {
130                 if (found)
131                 {
132                     return false;
133                 }
134                 else
135                 {
136                     found = true;
137                 }
138             }
139         }
140
141         return found;
142     }
143
144     /**
145      * Get the first set bit (starting at index 0) from a JBitSet.
146      *
147      * @return int Index of first set bit, -1 if none set.
148      */

149     public int getFirstSetBit()
150     {
151         for (int bitIndex = 0; bitIndex < size; bitIndex++)
152         {
153             if (bitSet.get(bitIndex))
154             {
155                 return bitIndex;
156             }
157         }
158
159         return -1;
160     }
161
162     /**
163      * Grow an existing JBitSet to the specified size.
164      *
165      * @param newSize The new size
166      *
167      */

168     public void grow(int newSize)
169     {
170         if (SanityManager.DEBUG)
171         {
172             SanityManager.ASSERT(newSize > size,
173                             "New size is expected to be larger than current size");
174         }
175
176         size = newSize;
177
178     }
179
180     /**
181      * Clear all of the bits in this JBitSet
182      */

183     public void clearAll()
184     {
185         for (int bitIndex = 0; bitIndex < size; bitIndex++)
186         {
187             if (bitSet.get(bitIndex))
188             {
189                 bitSet.clear(bitIndex);
190             }
191         }
192     }
193
194     /* Wrapper methods for BitSet's methods */
195     public String JavaDoc toString()
196     {
197         return bitSet.toString();
198     }
199
200     public boolean equals(Object JavaDoc obj)
201     {
202         if (SanityManager.DEBUG)
203         {
204             SanityManager.ASSERT((obj instanceof JBitSet),
205                                  "obj is expected to be a JBitSet " + obj);
206         }
207         return bitSet.equals(((JBitSet) obj).bitSet);
208     }
209
210     public int hashCode()
211     {
212         return bitSet.hashCode();
213     }
214
215     public Object JavaDoc clone()
216     {
217         return new JBitSet((BitSet JavaDoc) bitSet.clone(), size);
218     }
219
220     public boolean get(int bitIndex)
221     {
222         return bitSet.get(bitIndex);
223     }
224
225     public void set(int bitIndex)
226     {
227         bitSet.set(bitIndex);
228     }
229
230     public void clear(int bitIndex)
231     {
232         bitSet.clear(bitIndex);
233     }
234
235     public void and(JBitSet set)
236     {
237         bitSet.and(set.bitSet);
238     }
239
240     public void or(JBitSet set)
241     {
242         bitSet.or(set.bitSet);
243     }
244
245     public void xor(JBitSet set)
246     {
247         bitSet.xor(set.bitSet);
248     }
249
250     /**
251      * Return the size of bitSet
252      *
253      * @return int Size of bitSet
254      */

255     public int size()
256     {
257         return size;
258     }
259 }
260
261
Popular Tags