1 21 22 package org.apache.derby.iapi.util; 23 24 import org.apache.derby.iapi.services.sanity.SanityManager; 25 26 import java.util.BitSet ; 27 28 42 public final class JBitSet 43 { 44 45 private final BitSet bitSet; 46 47 private int size; 48 49 54 public JBitSet(int size) 55 { 56 bitSet = new BitSet (size); 57 this.size = size; 58 } 59 60 68 private JBitSet(BitSet bitSet, int size) 69 { 70 this.bitSet = bitSet; 71 this.size = size; 72 } 73 74 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 87 and(sourceBitSet); 88 or(sourceBitSet); 89 } 90 91 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 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 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 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 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 195 public String toString() 196 { 197 return bitSet.toString(); 198 } 199 200 public boolean equals(Object 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 clone() 216 { 217 return new JBitSet((BitSet ) 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 255 public int size() 256 { 257 return size; 258 } 259 } 260 261 | Popular Tags |