1 57 58 package org.enhydra.apache.xerces.validators.common; 59 60 import org.enhydra.apache.xerces.utils.ImplementationMessages; 61 62 78 class CMStateSet 79 { 80 CMStateSet(int bitCount) throws CMException 84 { 85 fBitCount = bitCount; 87 if (fBitCount < 0) 88 throw new CMException(ImplementationMessages.VAL_CMSI); 89 90 if (fBitCount > 64) 95 { 96 fByteCount = fBitCount / 8; 97 if (fBitCount % 8 != 0) 98 fByteCount++; 99 fByteArray = new byte[fByteCount]; 100 } 101 102 zeroBits(); 104 } 105 106 107 public String toString() 111 { 112 StringBuffer strRet = new StringBuffer (); 113 try 114 { 115 strRet.append("{"); 116 for (int index = 0; index < fBitCount; index++) 117 { 118 if (getBit(index)) 119 strRet.append(" " + index); 120 } 121 strRet.append(" }"); 122 } 123 124 catch(CMException exToCatch) 125 { 126 } 131 return strRet.toString(); 132 } 133 134 135 final void intersection(CMStateSet setToAnd) 139 { 140 if (fBitCount < 65) 141 { 142 fBits1 &= setToAnd.fBits1; 143 fBits2 &= setToAnd.fBits2; 144 } 145 else 146 { 147 for (int index = fByteCount - 1; index >= 0; index--) 148 fByteArray[index] &= setToAnd.fByteArray[index]; 149 } 150 } 151 152 final boolean getBit(int bitToGet) throws CMException 153 { 154 if (bitToGet >= fBitCount) 155 throw new CMException(ImplementationMessages.VAL_CMSI); 156 157 if (fBitCount < 65) 158 { 159 final int mask = (0x1 << (bitToGet % 32)); 160 if (bitToGet < 32) 161 return (fBits1 & mask) != 0; 162 else 163 return (fBits2 & mask) != 0; 164 } 165 else 166 { 167 final byte mask = (byte)(0x1 << (bitToGet % 8)); 169 final int ofs = bitToGet >> 3; 170 171 return ((fByteArray[ofs] & mask) != 0); 173 } 174 } 175 176 final boolean isEmpty() 177 { 178 if (fBitCount < 65) 179 { 180 return ((fBits1 == 0) && (fBits2 == 0)); 181 } 182 else 183 { 184 for (int index = fByteCount - 1; index >= 0; index--) 185 { 186 if (fByteArray[index] != 0) 187 return false; 188 } 189 } 190 return true; 191 } 192 193 final boolean isSameSet(CMStateSet setToCompare) 194 { 195 if (fBitCount != setToCompare.fBitCount) 196 return false; 197 198 if (fBitCount < 65) 199 { 200 return ((fBits1 == setToCompare.fBits1) 201 && (fBits2 == setToCompare.fBits2)); 202 } 203 204 for (int index = fByteCount - 1; index >= 0; index--) 205 { 206 if (fByteArray[index] != setToCompare.fByteArray[index]) 207 return false; 208 } 209 return true; 210 } 211 212 final void union(CMStateSet setToOr) 213 { 214 if (fBitCount < 65) 215 { 216 fBits1 |= setToOr.fBits1; 217 fBits2 |= setToOr.fBits2; 218 } 219 else 220 { 221 for (int index = fByteCount - 1; index >= 0; index--) 222 fByteArray[index] |= setToOr.fByteArray[index]; 223 } 224 } 225 226 final void setBit(int bitToSet) throws CMException 227 { 228 if (bitToSet >= fBitCount) 229 throw new CMException(ImplementationMessages.VAL_CMSI); 230 231 if (fBitCount < 65) 232 { 233 final int mask = (0x1 << (bitToSet % 32)); 234 if (bitToSet < 32) 235 { 236 fBits1 &= ~mask; 237 fBits1 |= mask; 238 } 239 else 240 { 241 fBits2 &= ~mask; 242 fBits2 |= mask; 243 } 244 } 245 else 246 { 247 final byte mask = (byte)(0x1 << (bitToSet % 8)); 249 final int ofs = bitToSet >> 3; 250 251 fByteArray[ofs] &= ~mask; 253 fByteArray[ofs] |= mask; 254 } 255 } 256 257 final void setTo(CMStateSet srcSet) throws CMException 258 { 259 if (fBitCount != srcSet.fBitCount) 261 throw new CMException(ImplementationMessages.VAL_CMSI); 262 263 if (fBitCount < 65) 264 { 265 fBits1 = srcSet.fBits1; 266 fBits2 = srcSet.fBits2; 267 } 268 else 269 { 270 for (int index = fByteCount - 1; index >= 0; index--) 271 fByteArray[index] = srcSet.fByteArray[index]; 272 } 273 } 274 275 final void zeroBits() 276 { 277 if (fBitCount < 65) 278 { 279 fBits1 = 0; 280 fBits2 = 0; 281 } 282 else 283 { 284 for (int index = fByteCount - 1; index >= 0; index--) 285 fByteArray[index] = 0; 286 } 287 } 288 289 290 int fBitCount; 308 int fByteCount; 309 int fBits1; 310 int fBits2; 311 byte[] fByteArray; 312 313 public boolean equals(Object o) { 314 if (!(o instanceof CMStateSet)) return false; 315 return isSameSet((CMStateSet)o); 316 } 317 318 public int hashCode() { 319 if (fBitCount < 65) 320 { 321 return fBits1+ fBits2 * 31; 322 } 323 else 324 { 325 int hash = 0; 326 for (int index = fByteCount - 1; index >= 0; index--) 327 hash = fByteArray[index] + hash * 31; 328 return hash; 329 } 330 } 331 332 }; 333 | Popular Tags |