1 2 17 18 19 package org.apache.poi.util; 20 21 27 28 public class BitField 29 { 30 private final int _mask; 31 private final int _shift_count; 32 33 40 41 public BitField(final int mask) 42 { 43 _mask = mask; 44 int count = 0; 45 int bit_pattern = mask; 46 47 if (bit_pattern != 0) 48 { 49 while ((bit_pattern & 1) == 0) 50 { 51 count++; 52 bit_pattern >>= 1; 53 } 54 } 55 _shift_count = count; 56 } 57 58 70 71 public int getValue(final int holder) 72 { 73 return getRawValue(holder) >> _shift_count; 74 } 75 76 88 89 public short getShortValue(final short holder) 90 { 91 return ( short ) getValue(holder); 92 } 93 94 102 103 public int getRawValue(final int holder) 104 { 105 return (holder & _mask); 106 } 107 108 116 117 public short getShortRawValue(final short holder) 118 { 119 return ( short ) getRawValue(holder); 120 } 121 122 133 134 public boolean isSet(final int holder) 135 { 136 return (holder & _mask) != 0; 137 } 138 139 149 150 public boolean isAllSet(final int holder) 151 { 152 return (holder & _mask) == _mask; 153 } 154 155 165 166 public int setValue(final int holder, final int value) 167 { 168 return (holder & ~_mask) | ((value << _shift_count) & _mask); 169 } 170 171 181 182 public short setShortValue(final short holder, final short value) 183 { 184 return ( short ) setValue(holder, value); 185 } 186 187 196 197 public int clear(final int holder) 198 { 199 return holder & ~_mask; 200 } 201 202 211 212 public short clearShort(final short holder) 213 { 214 return ( short ) clear(holder); 215 } 216 217 226 227 public byte clearByte(final byte holder) 228 { 229 return ( byte ) clear(holder); 230 } 231 232 240 241 public int set(final int holder) 242 { 243 return holder | _mask; 244 } 245 246 254 255 public short setShort(final short holder) 256 { 257 return ( short ) set(holder); 258 } 259 260 268 269 public byte setByte(final byte holder) 270 { 271 return ( byte ) set(holder); 272 } 273 274 284 285 public int setBoolean(final int holder, final boolean flag) 286 { 287 return flag ? set(holder) 288 : clear(holder); 289 } 290 291 301 302 public short setShortBoolean(final short holder, final boolean flag) 303 { 304 return flag ? setShort(holder) 305 : clearShort(holder); 306 } 307 308 318 319 public byte setByteBoolean(final byte holder, final boolean flag) 320 { 321 return flag ? setByte(holder) 322 : clearByte(holder); 323 } 324 } 326 | Popular Tags |