1 16 package org.apache.commons.lang; 17 18 31 public class BitField { 32 33 private final int _mask; 34 private final int _shift_count; 35 36 43 public BitField(int mask) { 44 _mask = mask; 45 int count = 0; 46 int bit_pattern = mask; 47 48 if (bit_pattern != 0) { 49 while ((bit_pattern & 1) == 0) { 50 count++; 51 bit_pattern >>= 1; 52 } 53 } 54 _shift_count = count; 55 } 56 57 71 public int getValue(int holder) { 72 return getRawValue(holder) >> _shift_count; 73 } 74 75 89 public short getShortValue(short holder) { 90 return (short) getValue(holder); 91 } 92 93 100 public int getRawValue(int holder) { 101 return holder & _mask; 102 } 103 104 111 public short getShortRawValue(short holder) { 112 return (short) getRawValue(holder); 113 } 114 115 128 public boolean isSet(int holder) { 129 return (holder & _mask) != 0; 130 } 131 132 144 public boolean isAllSet(int holder) { 145 return (holder & _mask) == _mask; 146 } 147 148 158 public int setValue(int holder, int value) { 159 return (holder & ~_mask) | ((value << _shift_count) & _mask); 160 } 161 162 172 public short setShortValue(short holder, short value) { 173 return (short) setValue(holder, value); 174 } 175 176 184 public int clear(int holder) { 185 return holder & ~_mask; 186 } 187 188 196 public short clearShort(short holder) { 197 return (short) clear(holder); 198 } 199 200 209 public byte clearByte(byte holder) { 210 return (byte) clear(holder); 211 } 212 213 221 public int set(int holder) { 222 return holder | _mask; 223 } 224 225 233 public short setShort(short holder) { 234 return (short) set(holder); 235 } 236 237 246 public byte setByte(byte holder) { 247 return (byte) set(holder); 248 } 249 250 259 public int setBoolean(int holder, boolean flag) { 260 return flag ? set(holder) : clear(holder); 261 } 262 263 272 public short setShortBoolean(short holder, boolean flag) { 273 return flag ? setShort(holder) : clearShort(holder); 274 } 275 276 285 public byte setByteBoolean(byte holder, boolean flag) { 286 return flag ? setByte(holder) : clearByte(holder); 287 } 288 289 } 290 | Popular Tags |