更有效的方式(使用int
代替byte[]
)需要一个非常简单的自定义类:
@Entity
@Access(AccessType.FIELD)
public class SampleEntity {
@Transient
private IntBitSet isolationLevel = new IntBitSet(0);
public static final int USER_BIT = 0;
public static final int DEVICE_BIT = 1;
// 2, 3, 4, ...
public boolean isUserIsolated() {
return isolationLevel.bitGet(USER_BIT);
}
public boolean isDeviceIsolated() {
return isolationLevel.bitGet(DEVICE_BIT);
}
public void setUserIsolated(boolean b) {
isolationLevel.bitSet(USER_BIT, b);
}
public void setDeviceIsolated(boolean b) {
isolationLevel.bitSet(DEVICE_BIT, b);
}
@Access(AccessType.PROPERTY)
@Column
public int getIsolationLevel() {
return isolationLevel.getValue();
}
public void setIsolationLevel(int isolationLevel) {
this.isolationLevel = new IntBitSet(isolationLevel);
}
private static class IntBitSet {
private int value;
public IntBitSet(int value) {
this.value = value;
}
public int getValue() {
return value;
}
public boolean bitGet(int i) {
return ((value >> i) & 1) == 1;
}
public void bitSet(int i, boolean b) {
if (b) {
bitSet(i);
} else {
bitUnset(i);
}
}
private void bitSet(int i) {
value = value | (1 << i);
}
private void bitUnset(int i) {
value = value & ~(1 << i);
}
}
}