java 不同目录用一个属性存储多个状态值
public class MultipleStatesTest { private long mSettingsPolicy = 0; public static void main(String[] args) { // TODO Auto-generated method stub MultipleStatesTest caseitmeID = new MultipleStatesTest(); caseitmeID.testA(1, false); caseitmeID.testA(2, true); caseitmeID.testA(5, true); caseitmeID.testA(6, false); caseitmeID.testA(16, false); caseitmeID.testA(23, true); caseitmeID.testA(11, true); caseitmeID.testA(76, false); caseitmeID.testA(43, true); caseitmeID.testA(45, false); caseitmeID.testA(56, true); caseitmeID.testA(89, false); caseitmeID.testA(77, false); } public void testA(int index ,boolean visible) { if (visible) { mSettingsPolicy &= ~(1 << index); } else { mSettingsPolicy |= (1 << index); } System.out.print(index +" result ="+ mSettingsPolicy+" ,statue ="); boolean state = (mSettingsPolicy & (1 << index)) == 0; System.out.println(state); } } ----------------输出结果--------------------- 1 result =2 ,statue =false 2 result =2 ,statue =true 5 result =2 ,statue =true 6 result =66 ,statue =false 16 result =65602 ,statue =false 23 result =65602 ,statue =true 11 result =65602 ,statue =true 76 result =69698 ,statue =false 43 result =69698 ,statue =true 45 result =77890 ,statue =false 56 result =77890 ,statue =true 89 result =33632322 ,statue =false 77 result =33632322 ,statue =false
示例代码二
protected void setUserState(int userID, final int statBitIdx) { Integer ustat = this.userStat.get(userID); if(ustat == null){ ustat = 0; } ustat |= (1 << statBitIdx); this.userStat.put(userID, ustat); } protected void clearUserState(int userID, final int statBitIdx) { Integer ustat = this.userStat.get(userID); if(ustat == null){ return ; } ustat = (ustat & (~(1 << statBitIdx))); this.userStat.put(userID, ustat); } protected boolean hasUserState(int userID, final int statBitIdx) { Integer ustat = this.userStat.get(userID); if(ustat == null){ return false; } return (ustat & (1 << statBitIdx)) != 0; }
版权声明:如无特殊标注,文章均为本站原创,转载时请以链接形式注明文章出处。
评论