|
@@ -9,22 +9,19 @@ import lombok.Data;
|
|
|
*/
|
|
|
@Data
|
|
|
public class Structure {
|
|
|
- /**表头表尾*/
|
|
|
- private boolean isHeadTail=false;
|
|
|
- /**业务数据*/
|
|
|
- private boolean isMeasure=false;
|
|
|
- /**每页固定不变*/
|
|
|
- private boolean isFixed=false;
|
|
|
+ /**上限,下限*/
|
|
|
+ private static final Integer MAX =0b111,MIN =0b000;
|
|
|
+
|
|
|
+ /*状态标识枚举,对应二进制的第几位*/
|
|
|
+ public enum StateFlag {
|
|
|
+ /*表头表尾,测量数据,每页固定内容*/
|
|
|
+ FIXED, MEASURE, HEAD_TAIL;
|
|
|
+ }
|
|
|
/*状态值二进制*/
|
|
|
- private char[] state =new char[]{'0','0','0'};
|
|
|
+ private volatile Integer state =MIN;
|
|
|
public Structure(Integer state) {
|
|
|
if(state!=null) {
|
|
|
- char[] arr = Integer.toBinaryString(state).toCharArray();
|
|
|
- int offset = this.state.length - arr.length;
|
|
|
- System.arraycopy(arr, 0, this.state, offset, arr.length);
|
|
|
- this.isHeadTail = this.state[index(1)] == '1';
|
|
|
- this.isMeasure = this.state[index(2)] == '1';
|
|
|
- this.isFixed = this.state[index(3)] == '1';
|
|
|
+ this.state=Math.min(Math.max(MIN,state),MAX);
|
|
|
}
|
|
|
}
|
|
|
|
|
@@ -32,28 +29,62 @@ public class Structure {
|
|
|
public Integer toInt(){
|
|
|
return Integer.parseInt(String.valueOf(state), 2);
|
|
|
}
|
|
|
- public int index(int n){
|
|
|
- return this.state.length-n;
|
|
|
+
|
|
|
+ public boolean flag(int n){
|
|
|
+ return ((this.state>>n) & 1)!=0;
|
|
|
+ }
|
|
|
+
|
|
|
+
|
|
|
+ public Structure setHeadTail(boolean headTail) {
|
|
|
+ this.state=setBit(order(StateFlag.HEAD_TAIL),headTail);
|
|
|
+ return this;
|
|
|
}
|
|
|
|
|
|
-/* public static void main(String[] args) {
|
|
|
- Structure structure = new Structure();
|
|
|
- structure.setFixed(true);
|
|
|
- System.out.println(structure.toInt());
|
|
|
- }*/
|
|
|
+ public Structure setMeasure(boolean measure) {
|
|
|
+ this.state=setBit(order(StateFlag.MEASURE),measure);
|
|
|
+ return this;
|
|
|
+ }
|
|
|
|
|
|
- public void setHeadTail(boolean headTail) {
|
|
|
- this.state[index(1)]=headTail?'1':'0';
|
|
|
- isHeadTail = headTail;
|
|
|
+ public Structure setFixed(boolean fixed) {
|
|
|
+ this.state=setBit(order(StateFlag.FIXED),fixed);
|
|
|
+ return this;
|
|
|
}
|
|
|
|
|
|
- public void setMeasure(boolean measure) {
|
|
|
- this.state[index(2)]=measure?'1':'0';
|
|
|
- isMeasure = measure;
|
|
|
+ /**表头表尾*/
|
|
|
+ public boolean isHeadTail() {
|
|
|
+ return flag(order(StateFlag.HEAD_TAIL));
|
|
|
+ }
|
|
|
+
|
|
|
+ /**业务数据*/
|
|
|
+ public boolean isMeasure() {
|
|
|
+ return flag(order(StateFlag.MEASURE));
|
|
|
+ }
|
|
|
+
|
|
|
+ /**每页固定不变*/
|
|
|
+ public boolean isFixed() {
|
|
|
+ return flag(order(StateFlag.FIXED));
|
|
|
+ }
|
|
|
+
|
|
|
+ public int order(StateFlag flag) {
|
|
|
+ return StateFlag.values().length-flag.ordinal()-1;
|
|
|
+ }
|
|
|
+
|
|
|
+
|
|
|
+ private int setBit(int i,boolean flag){
|
|
|
+ int mask;
|
|
|
+ if(flag){
|
|
|
+ /* 计算掩码,仅在第i位为1,其余位为0*/
|
|
|
+ mask = 1 << i;
|
|
|
+ return this.state | mask;
|
|
|
+ }else{
|
|
|
+ /* 计算掩码,除了第i位为1,其余位为0;然后取反,得到第i位为0,其余位为1的掩码*/
|
|
|
+ mask = ~(1 << i);
|
|
|
+ return this.state & mask;
|
|
|
+ }
|
|
|
}
|
|
|
|
|
|
- public void setFixed(boolean fixed) {
|
|
|
- this.state[index(3)]=fixed?'1':'0';
|
|
|
- isFixed = fixed;
|
|
|
+ @Override
|
|
|
+ public String toString() {
|
|
|
+ return Integer.toBinaryString(state)+"|"+isFixed()+"~"+isMeasure()+"~"+isHeadTail()+"|"+state;
|
|
|
}
|
|
|
}
|